Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Jul 30 '12 at 2:13

This question came from our site for professional and enthusiast programmers.

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.