Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Declaring variable for current record
Message
 
General information
Forum:
Visual FoxPro
Category:
Client/server
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00995317
Message ID:
00995330
Views:
20
Thanks Sergey!,

With your code I found some stuff in BOL

"deleted and inserted are logical (conceptual) tables. They are structurally similar to the table on which the trigger is defined, that is, the table on which the user action is attempted, and hold the old values or new values of the rows that may be changed by the user action. For example, to retrieve all values in the deleted table, use:"

And create the following from yours:
CREATE TRIGGER SETDEFAULT ON dbo.ss_Description
FOR INSERT, UPDATE
AS
DECLARE @llDefault bit

IF UPDATE(lDefault) 
BEGIN
  SELECT @llDefault = lDefault  FROM Inserted 
  IF @llDefault = 1
    BEGIN
      UPDATE ss_Description SET lDefault = 0 
      WHERE lDefault = 1 AND cFilter = (SELECT cFilter FROM Inserted)
      AND ID <> (SELECT ID FROM Inserted)
    END  
END
The buzz word I was looking for is INSERTED (This is where my current record is stored)


>See sample working code below. It handles attempts to set multiple IsDefault to 1 and recursion.
>Table name is TestTr. I used column name 'IsDefault' because DEFAULT is reserved word.
IF EXISTS (SELECT name    FROM   sysobjects
>	   WHERE  name = N'trig_test' AND type = 'TR')
>    DROP TRIGGER trig_test
>GO
>CREATE TRIGGER trig_test
>ON dbo.TestTr
>FOR INSERT, UPDATE
>AS
>DECLARE @UpdateCount int
>IF UPDATE(IsDefault)
>BEGIN
>	SELECT @UpdateCount = COUNT(*) FROM Inserted
>		WHERE IsDefault > 0
>	IF @UpdateCount > 1
>	BEGIN
>		RAISERROR ('Only one item can be IsDefault', 16, 1)
>		ROLLBACK TRANSACTION
>	END ELSE
>	--
>	IF @UpdateCount = 1
>	BEGIN
>		UPDATE TestTr SET IsDefault = 0
>			WHERE IsDefault = 1
>				AND id <> (SELECT id FROM Inserted)
>	END
>END
>
>
>>
>>How would you solve this situation?
>
>>Table Test
>>
>>ID       DESCRIPTION   DEFAULT
>>1        Apple         0
>>2        Banana        0
>>3        Carrot        0
>>4        Citrus        1
>
>
>>When updating record ID = 1 (Apple) and assigning DEFAULT = 1
>>
>>I wanted an UPDATE trigger to reset all other records with DEFAULT = 1 (Citrus) Except the one I'm updating (Apple).
Luc Nadeau
lnadeau@neova.ca

"the theory, it is when all is known and that nothing works. The practice, it is when all works and that nobody knows why." - Albert Einstein (Nobel of physique 1921)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform