output-clause

SQL Server: Multiple Output Clauses

喜欢而已 提交于 2019-12-23 09:38:11
问题 I have two tables, Table_1 and Table_2 . Table_1 has columns PK (autoincrementing int ) and Value ( nchar(10) ). Table_2 has FK ( int ), Key ( nchar(10) ) and Value ( nchar(10) ). That is to say, Table_1 is a table of data and Table_2 is a key-value store where one row in Table_1 may correspond to 0, 1 or more keys and values in Table_2 . I'd like to write code that programmatically builds up a query that inserts one row into Table_1 and a variable number of rows into Table_2 using the

@@IDENTITY, SCOPE_IDENTITY(), OUTPUT and other methods of retrieving last identity

岁酱吖の 提交于 2019-12-17 04:45:30
问题 I have seen various methods used when retrieving the value of a primary key identity field after insert. declare @t table ( id int identity primary key, somecol datetime default getdate() ) insert into @t default values select SCOPE_IDENTITY() --returns 1 select @@IDENTITY --returns 1 Returning a table of identities following insert: Create Table #Testing ( id int identity, somedate datetime default getdate() ) insert into #Testing output inserted.* default values What method is proper or

SQL Server OUTPUT clause

折月煮酒 提交于 2019-12-06 01:17:00
问题 I am a little stuck with why I can not seem to get the 'new identity' of the inserted row with the statement below. SCOPE_IDENTITY() just returns null. declare @WorkRequestQueueID int declare @LastException nvarchar(MAX) set @WorkRequestQueueID = 1 set @LastException = 'test' set nocount off DELETE dbo.WorkRequestQueue OUTPUT DELETED.MessageEnvelope, DELETED.Attempts, @LastException, GetUtcdate(), -- WorkItemPoisened datetime DELETED.WorkItemReceived_UTC INTO dbo.FaildMessages FROM dbo

SQL Server OUTPUT clause

淺唱寂寞╮ 提交于 2019-12-04 06:55:31
I am a little stuck with why I can not seem to get the 'new identity' of the inserted row with the statement below. SCOPE_IDENTITY() just returns null. declare @WorkRequestQueueID int declare @LastException nvarchar(MAX) set @WorkRequestQueueID = 1 set @LastException = 'test' set nocount off DELETE dbo.WorkRequestQueue OUTPUT DELETED.MessageEnvelope, DELETED.Attempts, @LastException, GetUtcdate(), -- WorkItemPoisened datetime DELETED.WorkItemReceived_UTC INTO dbo.FaildMessages FROM dbo.WorkRequestQueue WHERE WorkRequestQueue.ID = @WorkRequestQueueID IF @@ROWCOUNT = 0 RAISERROR ('Record not

Output to Temporary Table in SQL Server 2005

こ雲淡風輕ζ 提交于 2019-12-01 06:07:33
I am trying to use the OUTPUT clause inside a stored procedure to output to a temporary table the values of an indentity column after an INSERT . CREATE TABLE #Test ( ID INT ) INSERT INTO [TableB] OUTPUT INSERTED.ID #Test SELECT * FROM [TableA] However, when I execute this procedure SQL Server shows me the results in a table (correctly) called Test but if I write SELECT * FROM #Test as the next statement in the stored procedure it shows me nothing. How can I efectively accomplish this? I think you're missing an INTO - try this: CREATE TABLE #Test(ID INT) INSERT INTO [TableB] OUTPUT INSERTED.ID

Output to Temporary Table in SQL Server 2005

纵饮孤独 提交于 2019-12-01 03:17:16
问题 I am trying to use the OUTPUT clause inside a stored procedure to output to a temporary table the values of an indentity column after an INSERT . CREATE TABLE #Test ( ID INT ) INSERT INTO [TableB] OUTPUT INSERTED.ID #Test SELECT * FROM [TableA] However, when I execute this procedure SQL Server shows me the results in a table (correctly) called Test but if I write SELECT * FROM #Test as the next statement in the stored procedure it shows me nothing. How can I efectively accomplish this? 回答1: I

@@IDENTITY, SCOPE_IDENTITY(), OUTPUT and other methods of retrieving last identity

二次信任 提交于 2019-11-26 21:30:08
I have seen various methods used when retrieving the value of a primary key identity field after insert. declare @t table ( id int identity primary key, somecol datetime default getdate() ) insert into @t default values select SCOPE_IDENTITY() --returns 1 select @@IDENTITY --returns 1 Returning a table of identities following insert: Create Table #Testing ( id int identity, somedate datetime default getdate() ) insert into #Testing output inserted.* default values What method is proper or better? Is the OUTPUT method scope-safe? The second code snippet was borrowed from SQL in the Wild It