Get Insert query with already inserted data from a table based On certain Conditions

1.7k Views Asked by At

I have a table named sampletable

and it has 10000 rows and i want to generate script (Insert query with inserted data) for this table based on certain conditions.

That is i need the insert query with data for certain conditions

like select * from sampletable where id=10 something like that

The select query will results 100 rows only so i need to get only this 100 rows as insert query statement with inserted values

I had tried

Sql Management Studio Generate Script methods but it results all rows. I don't know how to do this please help me to solve this .

My purpose is i need to take backup of the data (Only the Specific condition data not all)

1

There are 1 best solutions below

1
Andomar On BEST ANSWER

You can generate an insert query by building a string, for example:

select  'insert into dbo.YourTable (str_col, int_col) values (' +
        isnull('''' + strcol + '''', 'NULL') + ', ' +
        isnull(cast(int_col as varchar(max)), 'NULL') + ');'
from    SampleTable
where   id=10 

If it's a one-off job, you can use Tasks->Export Data and friends instead of manually crafting SQL inserts.