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.
| Author |
Topic |
|
new_developer
Starting Member
18 Posts |
Posted - 2011-01-06 : 06:26:59
|
hi i have one table call it Session have one columncall it SessionIDi want insert one record indide itlike thisinsert into Session(SessionID) values(SessionID + 1) but it give me errorany one can modify to me this statementthanks |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-01-06 : 06:41:51
|
quote: Originally posted by new_developer hi i have one table call it Session have one columncall it SessionIDi want insert one record indide itlike thisinsert into Session(SessionID) values(SessionID + 1) but it give me errorany one can modify to me this statementthanks
The following will work:INSERT INTO [Session]SELECT MAX(SessionID) + 1FROM [Session] WITH (UPDLOCK, SERIALIZABLE) You may want to post what you are actually doing as code like this is not usually a good idea.[Edit]: If SessionId is an IDENTITY then use the following:INSERT INTO [Session] DEFAULT VALUES |
 |
|
|
new_developer
Starting Member
18 Posts |
Posted - 2011-01-06 : 09:08:47
|
| friend ok now i add one record but i want to retrive same record insidethe statement can u tell me how can i retrivethe record i inserted itthanks |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-01-06 : 10:15:24
|
| You can only do that inside of a trigger on the table.JimEveryday I learn something that somebody else already knew |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-01-06 : 11:47:54
|
quote: Originally posted by new_developer friend ok now i add one record but i want to retrive same record insidethe statement can u tell me how can i retrivethe record i inserted itthanks
IN SQL2008 use the OUTPUT clause:DECLARE @output TABLE( SessionID bigint NOT NULL)INSERT INTO [Session]OUTPUT inserted.SessionID INTO @outputSELECT MAX(SessionID) + 1FROM [Session] WITH (UPDLOCK, SERIALIZABLE)SELECT *FROM @output |
 |
|
|
|
|
|