19

I'm trying to make a generated script of my data (I mean, all the INSERT INTO commands). Because access permissions, I can't do a SET IDENTITY_INSERT TABLE OFF and ON (I'm using the user application in Staging)

So, there is a way to make this script in SQL Server Manager and avoid the field with the primary key? I set to false all properties (primary, unique, etc), but the script is still sending this field (For e.g., RecID 1, 2, 3, etc).

I'm using SQL Server 2012.

My configuration for the script:

enter image description here

Results I get:

SET IDENTITY_INSERT -TABLE- ON 
INSERT INTO TABLE (ID,Field1) VALUES (1,'value')

Any solution (except for removing it with Notepad++) is appreciated.

2 Answers 2

16

A bit of a work around, but sometimes useful quick and dirty way of doing these things:

SELECT 'INSERT INTO TABLE (Field1) VALUES (''' + Field1 + ''')' FROM TABLE

The result set will be a row for each insert statement for every row in the table TABLE. The INSERT statement is generated from concatenating the INSERT statement text with the values in Field1.

Sign up to request clarification or add additional context in comments.

4 Comments

OMG, it was so easy that I never thought about it! I was thinking in make some SP with fetch cursor and etc. LOL. Thanks a lot.
It comes in handy in many situations when you need to transform data and don't have regex handy... it has saved me several times over the years!
and what about nulls? I've some records with null (smalldatetime field)
@LeandroBardelli I missed this years ago, but have a look at the COALESCE function to map your NULLs to a string "NULL".
8

There is another way to do this which is a bit more automatic and a lot less faffy when you have a lot of columns of different data types; given a table T:

(
  ID int identity,
  C1 nvarchar(255),
  C2 datetime
  ...
)

...select everything except the identity column into a new table:

select C1, C2, ... into InterimTable from T

Then:

  1. Run the Generate Scripts wizard on InterimTable.
  2. Use whatever tool you have to search the SQL for InterimTable and replace with T
  3. Run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.