Insert Results of Query Into Table
-
Posted on June 18, 2009 by Derek Dieter
There are a few ways to insert query results into another table. The most common way is to use the standard insert statement. This would insert query results into a table that already contains data. [cc lang=”sql”] INSERT INTO dbo.Users ( Username ,FirstName ,LastName ,IsSuperUser ,AffiliateId ,Email ,DisplayName ,UpdatePassword ) SELECT Username ,FirstName ,LastName ,IsSuperUser ,AffiliateId ,Email ,DisplayName = DisplayName + ‘ SuperUser’ ,UpdatePassword FROM dbo.NewUsers nu WHERE nu.IsSuperUser = 1 [/cc] This way is most efficient if you are inserting records into a new table. It will automatically create the table for you and copy the datatypes from the existing table and all the rows: [cc lang=”sql”] SELECT * INTO dbo.EventLogCopy FROM dbo.EventLog [/cc]
Continue reading ...