Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 best way to Update and Select the same value?

Author  Topic 

mutlyp
Starting Member

20 Posts

Posted - 2012-06-11 : 21:53:55
I have an Interger value in a table. I want that value to be a unique number for each session when using the application.
So what I wold like is to be able to Select the value from the table, then save that value then update the value + 1 back into the table then fianlly return the saved value.
So It would be like:
@Num = Select (Update tableA set Num = Num + 1)
Return @Num
Hopes this makes since. Please let me know if anythng like this is possible.
Thank you

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-12 : 04:15:55
update tbl set Num = Num+1, @Num = Num + 1
return @Num



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-06-12 : 04:45:48
[code]DECLARE @Sample TABLE
(
Num INT PRIMARY KEY
);

INSERT @Sample
VALUES (55);

DECLARE @Num INT;

UPDATE @Sample
SET @Num = Num = Num + 1;

SELECT @Num;[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -