sql insert inserted id 1

sql insert inserted id

-- Never use @@identity or scope_identity()
-- they can not always be relied upon

DECLARE @Ids_tbl TABLE ([id] INT); -- Requires table. use variable for scope
INSERT INTO [sometable] ([ColA],[ColB])
OUTPUT INSERTED.ID INTO @Ids_tbl(id)
VALUES ('valA','valB');

SELECT [id] FROM @Ids_tbl; -- <-- Id(s) in here

Here is what the above code is Doing:
1. Declare a table variable to hold the inserted ids
2. Insert the values into the table
3. Output the inserted ids into the table variable
4. Select the ids from the table variable

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or a table.

The table variable is required because the output clause can only be used with a table variable or

Similar Posts