0

Hi i am using SQL SERVER 2008 R2, can someone please help me to generate a script like create, alter without using UI.

1
  • what all objects in sql server you need to generate script? Aug 28, 2012 at 12:20

4 Answers 4

5

Stored procedures, views, functions etc. can all be scripted from sys.sql_modules as long as they're not encrypted:

SELECT definition 
  FROM sys.sql_modules
  WHERE [object_id] = OBJECT_ID(N'dbo.object_name');

Or if you want to script multiple:

SELECT definition + CHAR(13) + CHAR(10) + 'GO'
  FROM sys.sql_modules
  WHERE OBJECT_NAME([object_id]) IN (N'name1', N'name2', ...);

Or all:

SELECT '--' + QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))
  + '.' + QUOTENAME(OBJECT_NAME([object_id]))
  + CHAR(13) + CHAR(10) + definition 
  + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10)
FROM sys.sql_modules
WHERE definition IS NOT NULL;

(Of course these are all doomed if you run them in Management Studio and any exceeds the max length of an output string there, ~8K in results to text. But it sounds like you want to consume these elsewhere.)

Note that this won't script the SET settings that were in force at the time the object was created, but you could extend this query to include settings like ANSI_NULLS and QUOTED_IDENTIFIER - which you can get from the same view.

Tables are a little trickier. If you generate the script in SSMS while profiler is running, you will see that it does this through a slew of queries and constructs the create table script within the code (in other words you can't sniff it out). It can be quite complex depending on what options you're using for your table, whether you need to script all foreign keys and dependent objects, etc. For this I would prefer the SMO method highlighted in podiluska's answer.

If you're already using SSMS then I don't understand the purpose of NOT using the generate scripts menu items. You can do so for multiple objects by using Object Explorer Details instead of Object Explorer, if the singleton approach is the problem:

enter image description here

3

You can use the Scripter class in SQL Management Objects (SMO) to do this.

eg: http://www.mssqltips.com/sqlservertip/1833/generate-scripts-for-database-objects-with-smo-for-sql-server/

4
1

Try this:

Create a sproc with following steps.

1.First get all the table names for which you need create table script.

2.loop through each table and get the below info:

  select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE  from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tablename'

3.Now in the loop itself try to dynamically populate the create table script.

2
  • 1
    There is so much more to a table than the list of columns. Aug 28, 2012 at 12:32
  • 1
    yeah he can join with other system tales to fetch the info like constraints,keys etc Aug 28, 2012 at 12:33
0

I wrote an open source command line utility named SchemaZen that does this. It's much faster than scripting from management studio and it's output is more version control friendly. It supports scripting both schema and data.

To generate scripts run:

schemazen.exe script --server localhost --database db --scriptDir c:\somedir

Then to recreate the database from scripts run:

schemazen.exe create --server localhost --database db --scriptDir c:\somedir
2
  • This is a nice tool--- very fast, clean and simple--- but it still has limitations for production use. Would be great with some fixes and enhancements by you or other contributors.
    – Martin_W
    Jun 5, 2014 at 18:43
  • @Martin_ATS Please submit feature requests here github.com/sethreno/schemazen/issues/new
    – Seth Reno
    Jul 16, 2015 at 14:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.