Skip to content
 

Insert Results of Query Into Table

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.

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

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:

SELECT *
INTO dbo.EventLogCopy
FROM dbo.EventLog

Related Posts:

Ask a question or post a comment