1

I want to write a script for generate insert into script.

I have a NewJob table. It has some rows. I want to generate insert into statements using script like this:

INSERT [Computer A].[newjob] ([Id], [Newjob], [jobNo]) VALUES (1, N'A', 4)
INSERT [Computer A].[newjob] ([Id], [Newjob], [jobNo]) VALUES (2, N'b', 8)
INSERT [Computer A].[newjob] ([Id], [Newjob], [jobNo]) VALUES (3, N'c', 12)

Can I do this ?

2 Answers 2

2

You can do this using a select statement and some basic string manipulation. The Select statement will produce an insert statement for each row returned.

SELECT 'INSERT INTO [Computer A].[newjob] ([Id]) VALUES (' + Id + ')'
FROM [Computer A].[newjob]

I have only included one parameter here, but you can fill in the rest by adding the columns you want to add

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

2 Comments

Are you sure you want 2 inserts?
I think the from is missing a [ too
1

I think the best way is to make use Script option from SSMS

SSMS-->Right click on database-->Tasks-->Generate scripts-->Choose the table-->Advanced

In the option Types of data to script, choose Data only

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.