SQL Server - Rewrite trigger to avoid cursor based approach -
If I have the table test with two columns num1 and < Em> Num2 and the following trigger that increases the number 2 on the sum of num1:
DECLARE @ PROC_NEWNUM1 VARCHAR (10) DECLARE @ NEWNUM2 Numerical (20) for selection Select my_Cursor cursor INSERTED from num1; Next to MY_Cursor FETCH @ PROC_NEWNUM1 WHILE @@ FETCH_STATUS = 0 Select SELECT @ NEWNUM2 = MAX (num2) if the @ NEWNUM2 is empty, then start @ NEWNUM2 = 0 set @ NEWNUM2 = @ NEWNUM2 + 1 update test set Number 2 = @ NEWNUM2 WHERE number1 = @ PROC_NEWNUM1 FETCH Close my PROGRAM_NEWNUM1 next to my matrices my_Cursor DEALLOCATE my_Cursor
Do any of the above write using a set based approach The way?
(If anyone wants to know why I am doing so, here's the background):
Row_Number (only Squilie 2005 only) Solution without the temporary table using:
SELECT @ MAXNUM2 = MAX (num2) test if the @ MAXNUM2 is started SELECT @ MAXNUM2 = 0 END UPDATE TEST SET num2 = MAXNUM2 + SubQuery Select (number 1, ROW_NUMBER (), from R. (Submitted as R on serial number 1) SubQuery Inner Job Test SubQuery.num1 = TEST.num1
< Previous> Create a self-growth table starting from
DE Clair @ MAXNUM2 numerical (20) - first 1Defin @tmp (ANIN int identity (1,1), pINN worker (10)) @tmp (PINN ) Include INSERT select number 1; - Now offset in SELECT @ MAXNUM2 = MAX (num2) Test if the @ MAXNUM2 is zero SET @ MAXNUM2 = 0 END - Set up Update Test num2 = @ MAXNUM2 + Test Enam from Anonymous @tump on @tm PInsNum = TEST.num1
Note: I was not able to test it, there may be typo.
In addition, I'm sure ROWNUMBER by using a non-floating table solution, but I'm too lazy to see the syntax. But you can use it as a guide to get an answer, instead of using the floating table, add numbers 1 to N to use ROWNUMBER and offset (@ MaxNum 2).
Comments
Post a Comment