Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Select results into Insert script - SQL Server [closed]

I have SQL Server 2008, SQL Server Management Studio.

I need to select data from Table1 in database1. Then I have to edit some values in the results and insert values into Table1 in database2.

Or let me put it other way.

How can I convert the data in one table into insert script.

like image 259
Captain Comic Avatar asked Dec 24 '10 12:12

Captain Comic


People also ask

How do I convert a SQL query to a SQL SELECT script?

Convert a database query (or stored procedure) result, which may contain multiple data sets, into a sql select script. Convert-QueryDataToSQL -ServerInstance Server1 -Database Database -Query 'SELECT * FROM [Database]. [schema].

How to generate a script from select statement result set?

Your select statement result set has been inserted into the table ( [tbl_Department_Sample]). Now, you just need to generate the script (data only) of the table ( [tbl_Department_Sample]) using Generate Script feature in SQL Server 2012 and above.

How to create an INSERT statement from a select result?

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. Hope it will help. I had a similar problem, but I needed to be able to create an INSERT statement from a query (with filters etc.)

How to generate a select script with column name and value?

There are multiple ways via SQL Server Management Studio (SSMS) or dynamic SQL that we can generate a SELECT script that includes both the column name and the column value. This is useful to port data to other databases or to just have the data in a script that can be used to repopulate a table as needed.


2 Answers

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].
like image 129
KT. Avatar answered Sep 24 '22 02:09

KT.


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!

like image 29
tember Avatar answered Sep 24 '22 02:09

tember