sudhAnsu63 sudhAnsu63 - 6 months ago 30
SQL Question

Generate insert script for selected records?

I have a table with the following data:

Pk_Id ProductName Fk_CompanyId Price
------------------------------------------------------
1 AMX 1 10.00
2 ABC 1 11.00
3 APEX 1 12.00
4 AMX 1 10.00
5 ABC 1 11.00
6 APEX 1 12.00
7 AMX 2 10.00
8 ABC 2 11.00
9 APEX 2 12.00


I want to generate Insert script for migrating records whose Fk_CompanyId is 1.

There is an insert script option to generate script for all records but I want to filter some records to migrate to another database.

Answer

If you are using the SQL Management Studio, you can right click your DB name and select Tasks > Import/Export data and follow the wizard.
one of the steps is called "Specify Table Copy or Query" where there is an option to write a query to specify the data to transfer, so you can simply specify the following query:

select * from [Table] where Fk_CompanyId = 1
Comments