Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Inserting and selecting at the same time
Message
De
23/09/2013 10:19:07
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Desktop
Divers
Thread ID:
01583879
Message ID:
01583884
Vues:
72
This message has been marked as the solution to the initial question of the thread.
Try this example:
create table NameTest (IDValue int identity, Name varchar(50))
create table IDValues (IDValue int)
go


insert into NameTest  output Inserted.IDValue  into IDValues 
   values ('Steve Goff'), ('Katy Goff'), ('Kevin Goff')

select * from IDValues
Note what's happening here...the OUTPUT statement lets you "tap into" the state of the rows as they are inserted....you can OUTPUT the results of an INSERT and even OUTPUT them into a table.

If you don't use the INTO, and just OUTPUT Inserted.IDValue, it will send the value(s) you inserted directly as a result set.

So this lets you get immediate feedback on what was inserted. Let me know if that helps...

(Note....if you're only ever looking to get one value based on your identity key...you can also use SCOPE_IDENTITY...the nice thing about OUTPUT is that it allows you to bring back other calculated columns from the original INSERT)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform