question

Mohan avatar image
Mohan asked

how to take insert script by using t-sql

Hi,

I have few tables in my database, they consist of static data ( C- Data) ... for every time when i am cleaning up my db ... i am cleaning up the whole db ... including the static data tables also ... but i need to have a script for insert with the values of the existing data of those tables ...so that i can run that script at any time ...

Any help is highly appreciatable

t-sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image
Fatherjack answered

Here is how the INSERT statement works http://msdn.microsoft.com/en-US/library/ms174335%28v=SQL.90%29.aspx. Without better information on what you want to do its difficult to help you any further at the moment.


EDIT: Having seen Matts answer I have re-read the question and I now understand you want to be able to (re-)insert data

CREATE TABLE [dbo].[DataTest]
    (
      [name] [varchar](255) NULL,
      [location] [varchar](255) NULL
    )
ON  [PRIMARY]

GO

INSERT  INTO [dbo].[DataTest] ( [name], [location] )
VALUES  ( 'Matt', -- name - varchar(255)
          'London'  -- location - varchar(255)
          )

INSERT  INTO [dbo].[DataTest] ( [name], [location] )
VALUES  (
          'Jonathan', -- name - varchar(255)
          'Cornwall'  -- location - varchar(255)              
        )

INSERT  INTO [dbo].[DataTest] ( [name], [location] )
VALUES  ( 'Brad', -- name - varchar(255)
          'Hawaii'  -- location - varchar(255)
          )

-- This bit will give you an insert for every row          
DECLARE @TabName VARCHAR(200)

SET @TabName = 'DataTest'

SELECT  'INSERT INTO ' + @TabName + ' SELECT ''' + name + ''',' + ''''
        + location + ''''
FROM    [dbo].[DataTest] AS ct

This is however very rough as it only works for certain data types and would need a lot of manual work to make it reliable. A better result could be found from a tool such as those recommended by Matt or something like SQL Data Compare from www.red-gate.com

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image
Matt Whitfield answered

There are a lot of tools that can generate insert statements for a table's data for you. My SQL Server IDE is one of them. I believe it's also in SSMS Tools pack.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
ah, reading your answer has put the question into a different context for me. Hi Ho Hi Ho, its off to Edit we go ...
0 Likes 0 ·
Grant Fritchey avatar image
Grant Fritchey answered

If I understand what you're doing, this is a pretty common problem. Here is an example TSQL script that can be used to generate INSERT statatements based off a table. There are other examples if you just look around a bit.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image
Blackhawk-17 answered

Another workaround would be to put your static data on read-only file-groups and then you could just restore them after a cleanup.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.