0

I'm creating a database and all of my tables using SQL Server Express through SQL scripts. Now, I want to insert some records in tables like States (all states names), city (all city names).

How do I make a SQL script for this? I want to add this code to my sqlscripts after creating the database. Alternatively, is there a way to restore database tables by sql scripts?

Thomas Stringer
42.5k9 gold badges120 silver badges155 bronze badges
asked Jul 27, 2012 at 12:38

1 Answer 1

1

If you have a script that generates your database and all of the objects, you can just append to the end whatever DML you are looking to accomplish. For instance, if you generated a script for your entire database that contains all of the DDL, just open up that file and at the end of it put something like this:

insert into States
(
 Col1,
 Col2,
 Col3,
 -- etc...
 ColN
)
values
(
 "Col1Val",
 "Col2Val",
 "Col3Val",
 "ColNVal"
),
(
 "Col1Val2",
 "Col2Val2",
 "Col3Val2",
 "ColNVal2"
)
insert into City
(
 -- your column list
)
values
(
 -- your values
)

Likewise, you can just have a separate script file and use SQLCMD to run your DDL script and then have a separate script containing your INSERTs and run that script afterwards. Here is a link on how to do this with SQLCMD.

answered Jul 30, 2012 at 3:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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