156

I have SQL Server 2008, SQL Server Management Studio.

I need to select data from a table in one database and insert into another table in another database.

How can I convert the returned results from my select into INSERT INTO ...?

Clarification from comments: While I believe this could be solved by a INSERT INTO SELECT or SELECT INTO, I do need to generate INSERT INTO ....

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Captain Comic
  • 15,744
  • 43
  • 110
  • 148
  • Are the databases on the same server? If so you can insert from one into the other. Insert into database2.dbo.myTable select data from database1.dbo.anOtherTable – u07ch Dec 24 '10 at 13:05
  • Yes both databases are on the same server. But i need script. I will give this scripts to our database guy. – Captain Comic Dec 24 '10 at 13:07
  • It would help to clarify the question. Will any approach work or do you need an insert script you can hand off to your dba? – greg Nov 05 '13 at 22:44

18 Answers18

219

Here is another method, which may be easier than installing plugins or external tools in some situations:

  • Do a select [whatever you need]INTO temp.table_namefrom [... etc ...].
  • Right-click on the database in the Object Explorer => Tasks => Generate Scripts
  • Select temp.table_name in the "Choose Objects" screen, click Next.
  • In the "Specify how scripts should be saved" screen:
    • Click Advanced, find the "Types of data to Script" property, select "Data only", close the advanced properties.
    • Select "Save to new query window" (unless you have thousands of records).
  • Click Next, wait for the job to complete, observe the resulting INSERT statements appear in a new query window.
  • Use Find & Replace to change all [temp.table_name] to [your_table_name].
  • drop table [temp.table_name].
KT.
  • 10,815
  • 4
  • 47
  • 71
  • Using INTO temp.table1 generates the following error: The specified schema name "temp" either does not exist or you do not have permission to use it. I am using SQL 2012. – Mark Oct 20 '17 at 10:19
  • 4
    `temp` here refers to any schema you have access to. You can either create one (`create schema temp`) and then remove it when you are done, or (better) simply use whatever schema you are currently working in. – KT. Oct 21 '17 at 08:26
  • 1
    Thank you for this, i knew there must be a way! By the way, you can skip creating the temp table if you just need the whole data from the table you want to script. – FAB Oct 12 '18 at 11:00
  • I had to use this to import data into an Azure Database. Import it into a non-azure database then extract as SQL inserts. – m12lrpv Oct 16 '18 at 22:29
  • 6
    It still amazes me that SSMS hasn't added this ability 'view output as insert script' on the result sets.. but the above approach works, although it creates individual insert commands instead of doing a proper bulk insert (say 1,000 items per insert) like it should . This can take forever to run on really large result sets – Traderhut Games Sep 18 '19 at 18:15
97

In SSMS:

  • Right click on the database > Tasks > Generate Scripts

  • Next

  • Select "Select specific database objects" and check the table you want scripted, Next

  • Click Advanced > in the list of options, scroll down to the bottom and look for the "Types of data to script" and change it to "Data Only" > OK

  • Select "Save to new query window" > Next > Next > Finish

All 180 rows now written as 180 insert statements!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tember
  • 1,418
  • 13
  • 32
  • 32
    Useful for few rows. _Not useful_: If a table has 20000 rows, and I want only 20 inserts for 20 rows. – Kiquenet Jan 31 '17 at 10:54
  • 3
    @Kiquenet - You could first create a View that selects your desired 20 rows, then follow these steps to create the insert scripts based on the View. You could then do a mass find and replace to change the View name back to whatever Table name you need to insert into. – tember Feb 02 '17 at 19:09
  • @tember It does not work on View like you say, but it DOES in Visual Studio: first you create a view with required selection in sql studio, then open Visual Studio, connect to sql server and generate script on that created view. – Sergii Jun 06 '18 at 10:50
  • Thank you for that info. Though dumping the entire table is an overkill, it solved my problem. – paulz Jul 19 '19 at 01:04
55

Native method:

for example if you have table

Users(Id, name)

You can do this:

select 'insert into Table values(Id=' + Id + ', name=' + name + ')' from Users
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
  • You can do it this way, but if you have more than a few tables it gets to be a bit of typing. There are sps written and findable on google that do this for you, or, if you have VS2012 you can have VS do it for you. See my answer below. – greg Feb 19 '14 at 21:25
  • 1
    Useful for tables with few fields. If a table has 50, 60, 100 columns not useful. Better _generation SQL code_ – Kiquenet Jan 31 '17 at 10:52
  • Thanks for this tip, this is something I was looking for :-) – Helen Neely Apr 29 '22 at 09:04
39

1- Explanation of Scripts

A)Syntax for inserting data in table is as below

Insert into table(col1,col2,col3,col4,col5)    

             -- To achieve this part i    
             --have used below variable
                ------@CSV_COLUMN-------

values(Col1 data in quote, Col2..quote,..Col5..quote)

-- To achieve this part 
-- i.e column data in 
--quote i have used 
--below variable
----@QUOTED_DATA---

C)To get above data from existing table we have to write the select query in such way that the output will be in form of as above scripts

D)Then Finally i have Concatenated above variable to create final script that's will generate insert script on execution

E)

@TEXT='SELECT ''INSERT INTO
 '+@TABLE_NAME+'('+@CSV_COLUMN+')VALUES('''+'+'+SUBSTRING(@QUOTED_DATA,1,LEN(@QUOTED_DATA)-5)+'+'+''')'''+' Insert_Scripts FROM '+@TABLE_NAME + @FILTER_CONDITION

F)And Finally Executed the above query EXECUTE(TEXT)

G)QUOTENAME() function is used to wrap column data inside quote

H)ISNULL is used because if any row has NULL data for any column the query fails and return NULL thats why to avoid that i have used ISNULL

I)And created the sp sp_generate_insertscripts for same

1- Just put the table name for which you want insert script

2- Filter condition if you want specific results

----------Final Procedure To generate Script------

CREATE PROCEDURE sp_generate_insertscripts
(
    @TABLE_NAME VARCHAR(MAX),
    @FILTER_CONDITION VARCHAR(MAX)=''
)
AS
BEGIN

SET NOCOUNT ON

DECLARE @CSV_COLUMN VARCHAR(MAX),
        @QUOTED_DATA VARCHAR(MAX),
        @TEXT VARCHAR(MAX)

SELECT @CSV_COLUMN=STUFF
(
    (
     SELECT ',['+ NAME +']' FROM sys.all_columns 
     WHERE OBJECT_ID=OBJECT_ID(@TABLE_NAME) AND 
     is_identity!=1 FOR XML PATH('')
    ),1,1,''
)

SELECT @QUOTED_DATA=STUFF
(
    (
     SELECT ' ISNULL(QUOTENAME('+NAME+','+QUOTENAME('''','''''')+'),'+'''NULL'''+')+'','''+'+' FROM sys.all_columns 
     WHERE OBJECT_ID=OBJECT_ID(@TABLE_NAME) AND 
     is_identity!=1 FOR XML PATH('')
    ),1,1,''
)

SELECT @TEXT='SELECT ''INSERT INTO '+@TABLE_NAME+'('+@CSV_COLUMN+')VALUES('''+'+'+SUBSTRING(@QUOTED_DATA,1,LEN(@QUOTED_DATA)-5)+'+'+''')'''+' Insert_Scripts FROM '+@TABLE_NAME + @FILTER_CONDITION

--SELECT @CSV_COLUMN AS CSV_COLUMN,@QUOTED_DATA AS QUOTED_DATA,@TEXT TEXT

EXECUTE (@TEXT)

SET NOCOUNT OFF

END
DineshDB
  • 5,998
  • 7
  • 33
  • 49
Param Yadav
  • 861
  • 8
  • 6
  • You done an amazing job... – DineshDB Jan 08 '18 at 09:41
  • This stored procedure is a sweet solution. I suggest a few small changes. At the end of the longest line, change `+ @FILTER` to `+ IIF(@FILTER='','',' WHERE ' + @FILTER)` so that you can pass a filter without including the word `WHERE`. Another change was to add `AND system_type_id <> 189 AND is_computed=0` within the WHERE of the two SELECT queries to ignore *timestamp* and *computed* columns. It would be easy to add additional exclusions for other specialty column types. – Ben Sep 30 '20 at 22:50
38

SSMS Toolpack (which is FREE as in beer) has a variety of great features - including generating INSERT statements from tables.

Update: for SQL Server Management Studio 2012 (and newer), SSMS Toolpack is no longer free, but requires a modest licensing fee.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • +1 @marc_s I did a lot work generating such insert scripts unsing T-SQL generator scripts and now you tell me that there is a tool. – bernd_k Dec 24 '10 at 13:22
  • Be careful when the tables store **money** data. Those data type can store more decimal places than are normally displayed or in the scripts generated by SSMS Toolpack – bernd_k Dec 26 '10 at 18:12
  • 16
    This is no longer free. http://www.ssmstoolspack.com/Licensing – greg Nov 05 '13 at 22:34
  • @Greg: thanks for pointing that out. Note: that licensing only applies to SQL Server Management Studio **2012** and newer - for earlier versions, the toolpack is still free. – marc_s Nov 06 '13 at 05:57
  • 4
    Alternative to ***SSMS Toolpack*** ? – Kiquenet Jan 31 '17 at 09:53
  • @Kiquenet Alternative would be to use JetBrains DataGrip. With it you can simply highlight the results grid, copy it to the clipboard, and then paste it as INSERT statements. Why, for Pete's sake, SSMS cannot do this is beyond me. Unfortunately, I am current stuck with SSMS and am trying to find the same functionality somewhere. – donvnielsen Mar 12 '21 at 20:57
27

It's possible to do via Visual Studio SQL Server Object Explorer.

You can click "View Data" from context menu for necessary table, filter results and save result as script.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
BotanMan
  • 1,357
  • 12
  • 25
  • 2
    Your post is correct and easiest way using SQL Server Data Tools in Visual Studio , [here](https://dotnetvibes.com/2016/04/24/generating-sql-insert-scripts-in-just-2-clicks/) and [here](http://stackoverflow.com/a/36426678/2218697) is post which explains how **generate insert statement for first 1000 rows** hope helps. – Shaiju T Nov 22 '16 at 07:32
9

Using visual studio, do the following

Create a project of type SQL Server-->SQL Server Database Project

open the sql server explorer CTL-\ , CTL-S

add a SQL Server by right clicking on the SQL SERVER icon. Selcet ADD NEW SERVER

navigate down to the table you are interested in

right click--> VIEW DATA

Click the top left cell to highlight everything (ctl-A doesnt seem to work)

Right Click -->SCript

This is fabulous. I have tried everything listed above over the years. I know there is a tool out there that will do this and much more, cant think of the name of it. But it is very expensive.

Good luck. I just figured this out. Have not tested it extensively w/ text fields etc, but it looks like it gets you a long ways down the road.

Greg

greg
  • 1,673
  • 1
  • 17
  • 30
  • 1
    This doesn't convert select results, it simply scripts out the table. – Eric K Feb 18 '14 at 20:19
  • @QuantumDynamix - the original question is clarified at the bottom by 'How can I convert the data in one table into a script.' Below is a sample of my approach. It is exactly what the user asked. In fact, it is the only solution I see that actually produces a script of inserts, as the user asks. Why the downvote? What does 'doesnt convert select results' mean? SET IDENTITY_INSERT [PMDB].[BUAttr] ON INSERT INTO [PMDB].[BUAttr] ([id], [BU], [Name], [Value]) VALUES (1, N'ALASKA', N'GeoCenter Lat', N'67.07213') – greg Feb 19 '14 at 21:20
  • this along with creating the temp tables, did it for me – harishr Nov 22 '17 at 02:23
5

Create a separate table using into statement For example

Select * into Test_123 from [dbo].[Employee] where Name like '%Test%'

Go to the Database Right Click the Database Click on Generate Script Select your table Select advanace option and select the Attribute "Data Only" Select the file "open in new query"

Sql will generate script for you

KMR
  • 101
  • 2
  • 3
4

This is a more versatile solution (that can do a little more than the question asks), and can be used in a query window without having to create a new stored proc - useful in production databases for instance where you don't have write access.

To use the code, please modify according to the in line comments which explain its usage. You can then just run this query in a query window and it will print the INSERT statements you require.

SET NOCOUNT ON

-- Set the ID you wish to filter on here
DECLARE @id AS INT = 123

DECLARE @tables TABLE (Name NVARCHAR(128), IdField NVARCHAR(128), IdInsert BIT, Excluded NVARCHAR(128))

-- Add any tables you wish to generate INSERT statements for here. The fields are as thus:
 -- Name: Your table name
 -- IdField: The field on which to filter the dataset
 -- IdInsert: If the primary key field is to be included in the INSERT statement
 -- Excluded: Any fields you do not wish to include in the INSERT statement
INSERT INTO @tables (Name, IdField, IdInsert, Excluded) VALUES ('MyTable1', 'Id', 0, 'Created,Modified')
INSERT INTO @tables (Name, IdField, IdInsert, Excluded) VALUES ('MyTable2', 'Id', 1, 'Created,Modified')

DECLARE @numberTypes TABLE (sysId TINYINT)

-- This will ensure INT and BIT types are not surrounded with quotes in the
-- resultant INSERT statement, but you may need to add more (from sys.types)
INSERT @numberTypes(SysId) VALUES(56),(104)

DECLARE @rows INT = (SELECT COUNT(*) FROM @tables)
DECLARE @cnt INT = 1

DECLARE @results TABLE (Sql NVARCHAR(4000))

WHILE @cnt <= @rows
BEGIN
    DECLARE @tablename AS NVARCHAR(128)
    DECLARE @idField AS NVARCHAR(128)
    DECLARE @idInsert AS BIT
    DECLARE @excluded AS NVARCHAR(128)
    SELECT
        @tablename = Name,
        @idField = IdField,
        @idInsert = IdInsert,
        @excluded = Excluded
    FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RowId FROM @tables) t WHERE t.RowId = @cnt

    DECLARE @excludedFields TABLE (FieldName NVARCHAR(128))
    DECLARE @xml AS XML = CAST(('<X>' + REPLACE(@excluded, ',', '</X><X>') + '</X>') AS XML)
    INSERT INTO @excludedFields SELECT N.value('.', 'NVARCHAR(128)') FROM @xml.nodes('X') AS T(N)

    DECLARE @setIdentity NVARCHAR(128) = 'SET IDENTITY_INSERT ' + @tablename

    DECLARE @execsql AS NVARCHAR(4000) = 'SELECT ''' + CASE WHEN @idInsert = 1 THEN @setIdentity + ' ON' + CHAR(13) ELSE '' END + 'INSERT INTO ' + @tablename + ' ('
    SELECT @execsql = @execsql +
    STUFF
    (
        (
            SELECT CASE WHEN NOT EXISTS(SELECT * FROM @excludedFields WHERE FieldName = name) THEN ', ' + name ELSE '' END
            FROM sys.columns
            WHERE object_id = OBJECT_ID('dbo.' + @tablename)
            FOR XML PATH('')
        ), 1, 2, ''
    ) +
    ')' + CHAR(13) + 'VALUES (' +
    STUFF
    (
        (
            SELECT
                CASE WHEN NOT EXISTS(SELECT * FROM @excludedFields WHERE FieldName = name) THEN
                    ''', '' + ISNULL(' +
                    CASE WHEN EXISTS(SELECT * FROM @numberTypes WHERE SysId = system_type_id) THEN '' ELSE ''''''''' + ' END +
                    'CAST(' + name + ' AS VARCHAR)' +
                    CASE WHEN EXISTS(SELECT * FROM @numberTypes WHERE SysId = system_type_id) THEN '' ELSE ' + ''''''''' END +
                    ', ''NULL'') + '
                ELSE ''
                END
            FROM sys.columns
            WHERE object_id = OBJECT_ID('dbo.' + @tablename)
            FOR XML PATH('')
        ), 1, 3, ''
    ) +
    ''')' + CASE WHEN @idInsert = 1 THEN CHAR(13) + @setIdentity + ' OFF' ELSE '' END +
    ''' FROM ' + @tablename + ' WHERE ' + @idField + ' = ' + CAST(@id AS VARCHAR)

    INSERT @results EXEC (@execsql)
    DELETE @excludedFields
    SET @cnt = @cnt + 1
END

DECLARE cur CURSOR FOR SELECT Sql FROM @results
OPEN cur

DECLARE @sql NVARCHAR(4000)
FETCH NEXT FROM cur INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT @sql
    FETCH NEXT FROM cur INTO @sql
END

CLOSE cur
DEALLOCATE cur
Neo
  • 4,145
  • 6
  • 53
  • 76
3

I created the following procedure:

if object_id('tool.create_insert', 'P') is null
begin
  exec('create procedure tool.create_insert as');
end;
go

alter procedure tool.create_insert(@schema    varchar(200) = 'dbo',
                                   @table     varchar(200),
                                   @where     varchar(max) = null,
                                   @top       int = null,
                                   @insert    varchar(max) output)
as
begin
  declare @insert_fields varchar(max),
          @select        varchar(max),
          @error         varchar(500),
          @query         varchar(max);

  declare @values table(description varchar(max));

  set nocount on;

  -- Get columns
  select @insert_fields = isnull(@insert_fields + ', ', '') + c.name,
         @select = case type_name(c.system_type_id)
                      when 'varchar' then isnull(@select + ' + '', '' + ', '') + ' isnull('''''''' + cast(' + c.name + ' as varchar) + '''''''', ''null'')'
                      when 'datetime' then isnull(@select + ' + '', '' + ', '') + ' isnull('''''''' + convert(varchar, ' + c.name + ', 121) + '''''''', ''null'')'
                      else isnull(@select + ' + '', '' + ', '') + 'isnull(cast(' + c.name + ' as varchar), ''null'')'
                    end
    from sys.columns c with(nolock)
         inner join sys.tables t with(nolock) on t.object_id = c.object_id
         inner join sys.schemas s with(nolock) on s.schema_id = t.schema_id
   where s.name = @schema
     and t.name = @table;

  -- If there's no columns...
  if @insert_fields is null or @select is null
  begin
    set @error = 'There''s no ' + @schema + '.' + @table + ' inside the target database.';
    raiserror(@error, 16, 1);
    return;
  end;

  set @insert_fields = 'insert into ' + @schema + '.' + @table + '(' + @insert_fields + ')';

  if isnull(@where, '') <> '' and charindex('where', ltrim(rtrim(@where))) < 1
  begin
    set @where = 'where ' + @where;
  end
  else
  begin
    set @where = '';
  end;

  set @query = 'select ' + isnull('top(' + cast(@top as varchar) + ')', '') + @select + ' from ' + @schema + '.' + @table + ' with (nolock) ' + @where;

  insert into @values(description)
  exec(@query);

  set @insert = isnull(@insert + char(10), '') + '--' + upper(@schema + '.' + @table);

  select @insert = @insert + char(10) + @insert_fields + char(10) + 'values(' + v.description + ');' + char(10) + 'go' + char(10)
    from @values v
   where isnull(v.description, '') <> '';
end;
go

Then you can use it that way:

declare @insert varchar(max),
        @part   varchar(max),
        @start  int,
        @end    int;

set @start = 1;

exec tool.create_insert @schema = 'dbo',
                        @table = 'customer',
                        @where  = 'id = 1',
                        @insert = @insert output;

-- Print one line to avoid the maximum 8000 characters problem
while len(@insert) > 0
begin
  set @end = charindex(char(10), @insert);

  if @end = 0
  begin
    set @end = len(@insert) + 1;
  end;

  print substring(@insert, @start, @end - 1);
  set @insert = substring(@insert, @end + 1, len(@insert) - @end + 1);
end;

The output would be something like that:

--DBO.CUSTOMER
insert into dbo.customer(id, name, type)
values(1, 'CUSTOMER NAME', 'F');
go

If you just want to get a range of rows, use the @top parameter as bellow:

declare @insert varchar(max),
        @part   varchar(max),
        @start  int,
        @end    int;

set @start = 1;

exec tool.create_insert @schema = 'dbo',
                        @table = 'customer',
                        @top    = 100,
                        @insert = @insert output;

-- Print one line to avoid the maximum 8000 characters problem
while len(@insert) > 0
begin
  set @end = charindex(char(10), @insert);

  if @end = 0
  begin
    set @end = len(@insert) + 1;
  end;

  print substring(@insert, @start, @end - 1);
  set @insert = substring(@insert, @end + 1, len(@insert) - @end + 1);
end;
Sorack
  • 319
  • 1
  • 5
  • 18
  • It helps but it truncated this field "6C2A94737A456C405FDCF793DFA6E661:BqRc/DpUIe27" to just "6C2A94737A456C405FDCF793DFA6E6" – Adam Mendoza Nov 20 '18 at 09:03
3

You can Choose 'Result to File' option in SSMS and export your select result to file and make your changes in result file and finally using BCP - Bulk copy you can insert in table 1 in database 2.

I think for bulk insert you have to convert .rpt file to .csv file

Hope it will help.

Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
2

I had a similar problem, but I needed to be able to create an INSERT statement from a query (with filters etc.)

So I created following procedure:

CREATE PROCEDURE dbo.ConvertQueryToInsert (@input NVARCHAR(max), @target NVARCHAR(max)) AS BEGIN

    DECLARE @fields NVARCHAR(max);
    DECLARE @select NVARCHAR(max);

    -- Get the defintion from sys.columns and assemble a string with the fields/transformations for the dynamic query
    SELECT
        @fields = COALESCE(@fields + ', ', '') + '[' + name +']', 
        @select = COALESCE(@select + ', ', '') + ''''''' + ISNULL(CAST([' + name + '] AS NVARCHAR(max)), ''NULL'')+'''''''
    FROM tempdb.sys.columns 
    WHERE [object_id] = OBJECT_ID(N'tempdb..'+@input);

    -- Run the a dynamic query with the fields from @select into a new temp table
    CREATE TABLE #ConvertQueryToInsertTemp (strings nvarchar(max))
    DECLARE @stmt NVARCHAR(max) = 'INSERT INTO #ConvertQueryToInsertTemp SELECT '''+ @select + ''' AS [strings] FROM '+@input
    exec sp_executesql @stmt

    -- Output the final insert statement 
    SELECT 'INSERT INTO ' + @target + ' (' + @fields + ') VALUES (' + REPLACE(strings, '''NULL''', 'NULL') +')' FROM #ConvertQueryToInsertTemp

    -- Clean up temp tables
    DROP TABLE #ConvertQueryToInsertTemp
    SET @stmt = 'DROP TABLE ' + @input
    exec sp_executesql @stmt
END

You can then use it by writing the output of your query into a temp table and running the procedure:

-- Example table
CREATE TABLE Dummy (Id INT, Comment NVARCHAR(50), TimeStamp DATETIME)
INSERT INTO Dummy VALUES (1 , 'Foo', GetDate()), (2, 'Bar', GetDate()), (3, 'Foo Bar', GetDate())

-- Run query and procedure
SELECT * INTO #TempTableForConvert FROM Dummy WHERE Id < 3
EXEC dbo.ConvertQueryToInsert '#TempTableForConvert', 'dbo.Dummy'

Note: This procedure only casts the values to a string which can cause the data to look a bit different. With DATETIME for example the seconds will be lost.

JollyBrackets
  • 551
  • 5
  • 9
1

You can Use Sql Server Integration Service Packages specifically designed for Import and Export operation.

VS has a package for developing these packages if your fully install Sql Server.

Integration Services in Business Intelligence Development Studio

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
1

I think its also possible with adhoc queries you can export result to excel file and then import that file into your datatable object or use it as it is and then import the excel file into the second database have a look at this link this can help u alot.

http://vscontrols.blogspot.com/2010/09/import-and-export-excel-to-sql-server.html

JSJ
  • 5,653
  • 3
  • 25
  • 32
0

If you are using Oracle (or configure the application to the SQL Server) then Oracle SQL Developer does this for you. choose 'unload' for a table and follow the options through (untick DDL if you don't want all the table create stuff).

Liam
  • 92
  • 9
0

I found this SMSMS Boost addon, which is free and does exactly this among other things. You can right click on the results and select Script data as.

DAud
  • 41
  • 1
  • 6
0

You can use this Q2C.SSMSPlugin, which is free and open source. You can right click and select "Execute Query To Command... -> Query To Insert...". Enjoy)

Sergey Petrov
  • 121
  • 1
  • 4
-1

You can use an INSERT INTO SELECT statement, to insert the results of a select query into a table. http://www.w3schools.com/sql/sql_insert_into_select.asp

Example:

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country
FROM Suppliers
WHERE Country='Germany'
Protector one
  • 6,926
  • 5
  • 62
  • 86
  • 1
    This has nothing to do with an result from an SELECT statement converting to INSERT statements This is just plain INSERT statement. – Niels Lucas Apr 02 '20 at 08:56
  • Is it not functionally equivalent? – Protector one Apr 05 '20 at 11:22
  • 1
    Sorry for the very late response. I did not notice an notification. But this will not work if we want to insert the results in a total different environment. Let's say we have a test server with a test database and we have an production server with the production database. These 2 are 100% separated. So we can't use your query for this scenario. I do agree that it inserts records based on a result. Its just that most people that are searching this question have an scenario that are close to my scenario. (Or think about a database/table template script that would be used on different servers) – Niels Lucas Jun 14 '21 at 13:50