{"id":15002875,"url":"https://github.com/eitanblumin/sp_generatetableddlscript","last_synced_at":"2025-10-30T09:31:13.357Z","repository":{"id":72813848,"uuid":"181880253","full_name":"EitanBlumin/sp_GenerateTableDDLScript","owner":"EitanBlumin","description":"This procedure can be used to generate a CREATE TABLE script for a given table in SQL Server","archived":false,"fork":false,"pushed_at":"2021-12-06T07:00:40.000Z","size":31,"stargazers_count":5,"open_issues_count":13,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T07:41:24.704Z","etag":null,"topics":["ddl","ddl-scripts","microsoft-sql-server","mssql","mssqlserver","sql-server","tsql","tsql-stored-procedures"],"latest_commit_sha":null,"homepage":"http://git.eitanblumin.com/sp_GenerateTableDDLScript/","language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EitanBlumin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-17T11:47:21.000Z","updated_at":"2024-04-16T15:49:54.000Z","dependencies_parsed_at":"2023-02-26T17:15:29.420Z","dependency_job_id":null,"html_url":"https://github.com/EitanBlumin/sp_GenerateTableDDLScript","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EitanBlumin%2Fsp_GenerateTableDDLScript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EitanBlumin%2Fsp_GenerateTableDDLScript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EitanBlumin%2Fsp_GenerateTableDDLScript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EitanBlumin%2Fsp_GenerateTableDDLScript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EitanBlumin","download_url":"https://codeload.github.com/EitanBlumin/sp_GenerateTableDDLScript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238950485,"owners_count":19557533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ddl","ddl-scripts","microsoft-sql-server","mssql","mssqlserver","sql-server","tsql","tsql-stored-procedures"],"created_at":"2024-09-24T18:53:32.068Z","updated_at":"2025-10-30T09:31:08.050Z","avatar_url":"https://github.com/EitanBlumin.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sp_GenerateTableDDLScript\n\nThis stored procedure can be used to generate a CREATE TABLE script in T-SQL for a given table in Microsoft SQL Server.\n\n[Releases Download Page](https://github.com/EitanBlumin/sp_GenerateTableDDLScript/releases)\n\n## Download \u0026 Installation\n\nYou may install this procedure in your SQL Server instance by downloading (or copy-and-pasting) the script [sp_GenerateTableDDLScript.sql](https://raw.githubusercontent.com/EitanBlumin/sp_GenerateTableDDLScript/master/sp_GenerateTableDDLScript.sql) and running it in the master database, it will also use the following command to turn it into a system stored procedure, thus making it usable anywhere in the instance:\n\n`EXECUTE sp_MS_marksystemobject 'sp_GenerateTableDDLScript'`\n\n## Syntax\n\n```\nEXEC sp_GenerateTableDDLScript\n\t    [ @TableName = ] 'TableName'\n\t[ , [ @NewTableName = ] 'NewTableName ]\n\t[ , [ @Result = ] @Result OUTPUT ]\n\t[ , [ @IncludeDefaults = ] 1 | 0 ]\n\t[ , [ @IncludeCheckConstraints = ] 1 | 0 ]\n\t[ , [ @IncludeForeignKeys = ] 1 | 0 ]\n\t[ , [ @IncludeIndexes = ] 1 | 0 ]\n\t[ , [ @IncludePrimaryKey = ] 1 | 0 ]\n\t[ , [ @IncludeIdentity = ] 1 | 0 ]\n\t[ , [ @IncludeUniqueIndexes = ] 1 | 0 ]\n\t[ , [ @IncludeComputedColumns = ] 1 | 0 ]\n\t[ , [ @UseSystemDataTypes = ] 1 | 0 ]\n\t[ , [ @ConstraintsNameAppend = ] 'ConstraintsNameAppend' ]\n\t[ , [ @Verbose = ] 1 | 0 ]\n```\n\n## Arguments\n\n`[ @TableName = ] 'TableName'`\n\n Is the name of the source table. This parameter is mandatory and is of type **SYSNAME**. If the table's schema is not default (dbo), then please specify the schema name as well, as part of the parameter.\n \n`[ @NewTableName = ] 'NewTableName'`\n\n Is the name of the new (target) table. It is of type **SYSNAME** with a default of NULL. You may also include database and schema as part of the name. If not specified, same name as source table will be used.\n \n`[ @Result = ] @Result OUTPUT`\n\n Is an output textual parameter of type **NVARCHAR(MAX)** that will contain the result TSQL command for creating the table.\n \n`[ @IncludeDefaults = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include default constraints.\n \n`[ @IncludeCheckConstraints = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include check constraints.\n \n`[ @IncludeForeignKeys = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include foreign key constraints.\n \n`[ @IncludeIndexes = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include indexes.\n \n`[ @IncludePrimaryKey = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include primary key constraints.\n \n`[ @IncludeIdentity = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include identity property.\n \n`[ @IncludeUniqueIndexes = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include unique index constraints.\n \n`[ @IncludeComputedColumns = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 1. Sets whether to include computed columns (if not, they will also be automatically ignored by constraints and indexes).\n \n`[ @UseSystemDataTypes = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 0. Sets whether to use system data type names instead of user data type names.\n \n`[ @ConstraintsNameAppend = ] 'ConstraintsNameAppend'`\n\n Is a parameter of type **SYSNAME** with a default of '' (empty string). This is an optional text string to append to constraint names, in order to avoid the duplicate object name exception. This is useful when creating the new table within the same database.\n \n`[ @Verbose = ] 1 | 0`\n\n Is a parameter of type **BIT** with a default of 0 (false). This is an optional parameter which, when set to 1 (true), will cause the procedure to print informative messages, and also output a result set containing the discovered table field metadata.\n\n## Examples\n\n**A. Creating a table in an archive database, without foreign keys and identity property:**\n\n```\nDECLARE @CMD NVARCHAR(MAX)\nEXEC sp_GenerateTableDDLScript 'Sales.OrderDetails', 'ArchiveDB.Sales.OrderDetails', @CMD OUTPUT, @IncludeForeignKeys = 0, @IncludeIdentity = 0\nSELECT @CMD\n```\n\n**B. Duplicating a table within the same database:**\n\n```\nDECLARE @CMD NVARCHAR(MAX)\nEXEC sp_GenerateTableDDLScript 'Sales.OrderDetails', 'Sales.OrderDetails_New', @CMD OUTPUT, @ConstraintsNameAppend = '_New'\nSELECT @CMD\n```\n\n**C. Duplicating a table as a temporary table, without computed columns:**\n\n```\nDECLARE @CMD NVARCHAR(MAX)\nEXEC sp_GenerateTableDDLScript 'Sales.OrderDetails', '#temp_OrderDetails', @CMD OUTPUT, @ConstraintsNameAppend = '_Temp', @IncludeComputedColumns = 0\nSELECT @CMD\n```\n\n## Remarks\n\n- The **source table must exist**, otherwise an exception will be raised.\n- The script **does not check whether the target table already exists**,\n  it falls on you to make sure that it doesn't before running the result script.\n- The script does not check whether constraint names already exist,\n  it falls on you to use the `@ConstraintsNameAppend` parameter to generate unique names.\n- The script (at the moment) does NOT support the following ([more info at the issues page](https://github.com/EitanBlumin/sp_GenerateTableDDLScript/issues)):\n\t- Column Sets\n\t- Collations different from Database Default\n\t- Filestream columns\n\t- Sparse columns\n\t- Not for replication property\n\t- XML document collections\n\t- Rule objects\n\t- Non-default Filegroups\n\t- In-Memory tables\n\n## Supported Versions\n\nAt this time, this script was tested on the following Microsoft SQL Server versions:\n\n- 2014\n- 2016\n- 2017\n\nIf you wish to contribute by testing on additional versions, you're more than welcome to do so, and submit your results to our [GitHub Issues page](https://github.com/EitanBlumin/sp_GenerateTableDDLScript/issues).\n\n## License\n\nThe sp_GenerateTableDDLScript procedure is licensed under the Mozilla Public License 2.0.\n\nFor more info [Click Here](https://github.com/EitanBlumin/sp_GenerateTableDDLScript/blob/master/LICENSE)\n\n## Contribution \u0026 Issue Submission\n\nThis is an open-source project, and therefore you may freely submit your own issue and pull requests, if you wish to contribute.\n\nAny contribution is welcome.\n\nYou may view the currently opened issues at the [GitHub Issues page](https://github.com/EitanBlumin/sp_GenerateTableDDLScript/issues).\n\n## Acknowledgements\n\nThe script is mainly based off of the sp_ScriptTable stored procedure originally published by Tim Chapman in this URL:\n\n[https://www.techrepublic.com/blog/the-enterprise-cloud/script-table-definitions-using-tsql/](https://www.techrepublic.com/blog/the-enterprise-cloud/script-table-definitions-using-tsql/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feitanblumin%2Fsp_generatetableddlscript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feitanblumin%2Fsp_generatetableddlscript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feitanblumin%2Fsp_generatetableddlscript/lists"}