Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Automating Creation Scripts

Author  Topic 

MattC
Starting Member

9 Posts

Posted - 2012-10-09 : 07:43:07
Hi,

I have a (very long) list of table names. For each of these tables I need to generate an SQL creation script and save that script to a file.

Since I have hundreds of these to do I would like to automate the process.

Is there a simple way to accomplish this? Any suggestions are appreciated.

Thanks,
Matt

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-09 : 07:47:45
Depending on the details of your requirement, couple of possibilities:

1. Use code such as the one on this page: http://scriptsqlconfig.codeplex.com/ (Thanks to KH for pointing to this link in another thread. That thread also may be of interest to you: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=179540)

2. In SSMS, in object explorer, right-click on the database name and Tasks -> Generate Scripts and follow through the wizard.
Go to Top of Page

MattC
Starting Member

9 Posts

Posted - 2012-10-10 : 10:32:05
Thanks for the help. Unfortunately the codecomplex program would not run on my pc and the other thread was interesting but not quite what I need.

Let me restate the question in a simpler form - that way I'll have a better chance of understanding the answer. :)

Given a table name how can I use SQL to produce a creation script for that table?

Many Thanks,
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-10 : 11:54:33
If generating the scripts using the menu option in object explorer is not a possiblility, the next best thing I can think of is to sue a powershell script. But, that comes with its own issues - nonetheless you can give it a try.

In the script below, you can change the first four lines and run it.

If you want to script all the tables, just remove the Where-Object filter on the line before the foreach loop.

If you have never run powershell scripts before on the server, you will need to change the execution policy from a powershell window using:
set-executionpolicy remotesigned

I tested this code on my local box with 3 tables and it works. This is my own unsophisticated script with very little error checking; but it is tailored to what you described.
$servername = '(local)';  # Your ServerName
$databasename = 'YourDatabaseNameHere'; # Your Databasename
$OutputFolderName = 'C:\Temp'; # Folder where you want the scripts to be written to
$InputTableList = 'C:\Temp\tables.txt' # File that lists the tables that you want to script

$tableList = Get-Content $InputTableList;

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null;
$server = new-object Microsoft.SqlServer.Management.Smo.Server($servername);
$database = $server.databases[$databasename];
$tables = $database.tables | Where-Object { $tableList -contains ($_.name) }
foreach ($table in $tables)
{
$table.script() | out-file ([IO.Path]::Combine($OutputFolderName,$table.name+'.sql'));
}
Go to Top of Page

MattC
Starting Member

9 Posts

Posted - 2012-10-12 : 13:18:32
Many thanks for your thoughtful response. I need to take some time and look into PowerShell scripts.
Go to Top of Page
   

- Advertisement -