SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Generate DDL script to create table and all constraints

By VPSD Gupta,

Sometimes you quickly need to generate SQL Statements within your SPs or your front-end code to manipulate data in various ways. This SP generates all the SQL Statements of an existing table in a database to create a table, create Primary Key, create various Foreign Keys, create check constraints and create Defaults on every column of a table.

Use the SP by passing the following parameters:

EXEC usp_Create_Table_DDLs @sTable_Name = 'MY_TABLE', @Create_Table_Ind = 1, @PK_Ind = 1, @FK_Ind = 1, @Check_Ind = 1, @Default_Ind = 1

By default all the indicator variables are set to 1 but you can change that to 0 if you don't need the SQL for that.

Total article views: 2490 | Views in the last 30 days: 2
Related Articles

Default parameters changing when new database is created

Default parameters when database is created with Studio


Creating a default user

Is there a way of defaulting a gived SQL login to every database created?


Default database

What? The default database is one of the options when creating a login in SQL Server. This is the...


User Default Schema Problem

User creates objects in dbo other than the default schema


Default Constraint Error

Getting an error while creating a default constraint dynamically