views:

96

answers:

1

SSMS allows you to right-click a table and "Script Table as"->"Select to". I thought that almost everything in SSMS was done through the SMO scripting engine, but I can't seem to find a way to do this through SMO short of looping through columns and generating the script myself.

Is my Google-Fu weak, or are people just not using SMO for something like this? I couldn't find any example scripts for this anywhere, but it seems like it would be a common need.

+1  A: 

It looks like the Script() function of a table can do that:

Server server = new Server(".");
Database northwind = server.Databases["Northwind"];
Table categories = northwind.Tables["Categories"];
StringCollection script = categories.Script();
string[] scriptArray = new string[script.Count];
script.CopyTo(scriptArray, 0);

Now scriptArray will contain a list of SQL commands, the first entry of the string array will be set ansi_nulls on. See this blog post.

Andomar
Thanks, but that generates a creation script. I'm looking for a select script
Tom H.
Can't find a way to generate a select script. But that should be easy to implement?
Andomar