all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

If statement in a trigger


Re: If statement in a trigger Alex Kuznetsov
9/11/2007 10:40:38 AM
sql server programming: [quoted text, click to view]

There is no need to write a cursor to detect overlaps, it is easy to
accomplish using set-based logic.
Re: If statement in a trigger Shiju Samuel
9/11/2007 10:47:40 AM
[quoted text, click to view]
IF ((@var1 is not null) or (@var2 is not null))

Also don't use cursor.
-
Shiju

[quoted text, click to view]

Re: If statement in a trigger TheSQLGuru
9/11/2007 1:28:10 PM
It isn't a matter of "can't". It is simply that cursors are HORRIBLE from a
performance standpoint and should be avoided if at all possible. They
almost always ARE avoidable, btw. I didn't go into your code in-depth, but
on first blush I would say that your logic can be rewritten to use set-based
logic.

Oh, and if you DO use cursors, they should almost always be defined as
FAST_FORWARD, which is optimized by the sql server engine.

Oh, and if you cannot have duplicates in your UNION set, you should use
UNION ALL, which avoids a sort/distinct operation under the covers - again
leading to lower performance.

--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

Re: If statement in a trigger Roy Harvey
9/11/2007 2:35:35 PM
First, do not test for NULL using =, <> or != as they don't work. Use
IS NULL or IS NOT NULL.

[quoted text, click to view]

IF @var1 IS NOT NULL or @var2 IS NOT NULL

Second, your trigger code can not work if more than one row was
affected. Triggers fire once per command, not once per row. If more
than one row is INSERTed then the inserted table will have more than
one row. The following will not work in that case.

[quoted text, click to view]

Third, as has been said by others there doesn't appear to be anything
here that requires a cursor. I have not gone to the trouble of
sorting out exactly what the code is trying to do, but here is
something that I hope will give you something to think about.

SELECT *
FROM Table1 as A
WHERE EXISTS
(SELECT * FROM Table2 as B
WHERE A.Customer = B.Customer
AND A.TranDate BETWEEN B.StartDate and B.EndDate)

The idea with SQL is to think of sets operations rather than
row-at-a-time processing.

Roy Harvey
Beacon Falls, CT

On Tue, 11 Sep 2007 19:35:43 +0300, "Tamer Ibrahim"
[quoted text, click to view]
If statement in a trigger Tamer Ibrahim
9/11/2007 7:35:43 PM
Hi,

I have If condition that evaluate always to ture and I can't figure out why
.. Any help will be most appeciated.

I have placed a comment next to the If statement mentioned ...



ALTER TRIGGER TriggerInsertHotel

ON dbo.Hotel_Res

FOR INSERT /*, UPDATE DELETE */

AS

/* IF UPDATE () ...*/

Declare @File_ID varchar(10),@var1 varchar(10),@var2 varchar(10),

@From_Date datetime,

@ExistedFrom_Date datetime,

@To_Date datetime,

@ExistedTo_Date datetime,

@Pax_No smallint,

@Reserved_NO smallint,

@ExistedReserved_NO smallint


select @File_ID = [File_ID], @From_Date = From_Date, @To_Date = To_Date ,
@Reserved_NO = (Adults + Kids1 + Kids2)

from inserted

Select @Pax_No = Pax_No

from Files_Info

where [File_ID] = @File_ID

set @var1=null

set @var2=null

select @var1=[File_ID] from Hotel_Res Where [File_ID] = @File_ID

select @var2=[File_ID] from NCRS_Res Where [File_ID] = @File_ID

IF ((@var1 != null) or (@var2 != null))
//This if condition always evaluate to ture !!!

Begin

Declare OverLap_Cursor1 Cursor

for

Select From_Date, To_Date , (Adults + Kids1 + Kids2) from Hotel_Res where
[File_ID] = @File_ID

Union

Select From_Date, To_Date , (Adults + Kids1 + Kids2) from NCRS_Res where
[File_ID] = @File_ID

Open OverLap_Cursor1

fetch next

from OverLap_Cursor1

into @ExistedFrom_Date, @ExistedTo_Date, @ExistedReserved_NO

While (@@FETCH_STATUS = 0)

Begin

If ( (@From_Date > @ExistedFrom_Date and @From_Date < @ExistedTo_Date) or
(@To_Date > @ExistedFrom_Date and @To_Date < @ExistedTo_Date) or
(@Reserved_NO > (@Pax_No - @ExistedReserved_NO)))

Begin

raiserror('Date overlap or reservation pax number violation.', 10, 1)

rollback tran

Return

End



fetch next

from OverLap_Cursor1

into @ExistedFrom_Date, @ExistedTo_Date, @ExistedReserved_NO

End

close OverLap_Cursor1

deallocate OverLap_Cursor1

End

Return


Re: If statement in a trigger Tamer Ibrahim
9/11/2007 8:15:42 PM
Why can't I use a cursor ? I need them to loop over the rows returned from
the two tables resulting from the union of the select statements ....

[quoted text, click to view]

Re: If statement in a trigger Tamer Ibrahim
9/11/2007 8:24:46 PM
I'm using this technics to detect date overlapping between dates recorded in
two tables. What set-based logic I can use instead ?

[quoted text, click to view]

Re: If statement in a trigger Tamer Ibrahim
9/14/2007 12:00:00 AM
Hi,
During the last two days, I tried to figure out how I can replace my cursor
with the select staement you gave me but I failed !

[quoted text, click to view]

Re: If statement in a trigger ML
9/14/2007 2:48:01 AM
If you post table DDL, sample data and expected results, someone can help you
design a set-based solution.

See this article for details:
http://www.aspfaq.com/etiquette.asp?id=5006


ML

---
Matija Lah, SQL Server MVP
AddThis Social Bookmark Button