CREATE PROCEDURE [dbo].[usp_CreateTable_DDL] ( @sTable_Name SYSNAME, @Create_Table_Ind BIT = 1, @PK_Ind BIT = 1, @FK_Ind BIT = 1, @Check_Ind BIT = 1, @Default_Ind BIT = 1 ) AS BEGIN SET NOCOUNT ON DECLARE @Schema_Name SYSNAME, @UniqueConstraints BIT = 1, @sStr VARCHAR(MAX) SELECT @Schema_Name = SCHEMA_NAME(schema_id) FROM sys.objects WHERE name = @sTable_Name IF EXISTS (SELECT 1 FROM tempdb..sysobjects WHERE ID = OBJECT_ID('tempdb..#PKObjectID')) DROP TABLE #PKObjectID IF EXISTS (SELECT 1 FROM tempdb..sysobjects WHERE ID = OBJECT_ID('tempdb..#Uniques')) DROP TABLE #Uniques IF EXISTS (SELECT 1 FROM tempdb..sysobjects WHERE ID = OBJECT_ID('tempdb..#Constraints')) DROP TABLE #Constraints IF EXISTS (SELECT 1 FROM tempdb..sysobjects WHERE ID = OBJECT_ID('tempdb..#ShowFields')) DROP TABLE #ShowFields CREATE TABLE #Constraints (ID INT IDENTITY, Constraint_Type VARCHAR(100), SQL VARCHAR(8000), Constraint_Name SYSNAME DEFAULT '') -- Create table IF @Create_Table_Ind = 1 BEGIN SELECT FieldID = IDENTITY(INT,1,1), DatabaseName = DB_NAME(), TableOwner = TABLE_SCHEMA, TableName = TABLE_NAME, FieldName = COLUMN_NAME, ColumnPosition = CAST(ORDINAL_POSITION AS INT), ColumnDefaultValue = COLUMN_DEFAULT, ColumnDefaultName = dobj.name, IsNullable = CASE WHEN c.IS_NULLABLE = 'YES' THEN 1 ELSE 0 END, DataType = DATA_TYPE, MaxLength = CAST(CHARACTER_MAXIMUM_LENGTH AS INT), NumericPrecision = CAST(NUMERIC_PRECISION AS INT), NumericScale = CAST(NUMERIC_SCALE AS INT), DomainName = DOMAIN_NAME, FieldListingName = COLUMN_NAME + ',', FieldDefinition = '', IdentityColumn = CASE WHEN ic.object_id IS NULL THEN 0 ELSE 1 END, IdentitySeed = CAST(ISNULL(ic.seed_value,0) AS INT), IdentityIncrement = CAST(ISNULL(ic.increment_value,0) AS INT), IsCharColumn = CASE WHEN DATA_TYPE NOT IN ('TEXT') AND st.collation_name IS NOT NULL THEN 1 ELSE 0 END INTO #ShowFields FROM INFORMATION_SCHEMA.COLUMNS c JOIN sys.columns sc ON c.TABLE_NAME = OBJECT_NAME(sc.object_id) AND c.COLUMN_NAME = sc.Name LEFT JOIN sys.identity_columns ic ON c.TABLE_NAME = OBJECT_NAME(ic.object_id) AND c.COLUMN_NAME = ic.Name JOIN sys.types st ON COALESCE(c.DOMAIN_NAME,c.DATA_TYPE) = st.name LEFT OUTER JOIN sys.objects dobj ON dobj.object_id = sc.default_object_id AND dobj.type = 'D' WHERE c.TABLE_NAME = @sTable_Name ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION SELECT @sStr = 'IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = object_id('+'N'''+ '[dbo]' + '.' + QUOTENAME(@sTable_Name) + ''')'+ ' AND OBJECTPROPERTY(id, N'''+'IsUserTable'+''')'+'= 1)' + char(13) + char(10) + 'DROP TABLE ' + '[dbo]' + '.' + QUOTENAME(@sTable_Name) + char(13) + char(10) + 'CREATE TABLE ' + '[dbo]' + '.' + QUOTENAME(@sTable_Name) + '(' SELECT @sStr = @sStr + CHAR(10) + CHAR(9) + QUOTENAME(FieldName) + ' ' + CASE WHEN DomainName IS NOT NULL THEN DomainName + CASE WHEN IsNullable = 1 THEN ' NULL ' ELSE ' NOT NULL ' END ELSE UPPER(DataType) + CASE WHEN IsCharColumn = 1 OR DataType IN ('Varbinary') THEN '(' + CASE WHEN MaxLength = -1 THEN 'MAX' ELSE CAST(MaxLength AS VARCHAR(10)) END + ')' ELSE '' END + CASE WHEN IdentityColumn = 1 THEN ' IDENTITY(' + CAST(IdentitySeed AS VARCHAR(5))+ ',' + CAST(IdentityIncrement AS VARCHAR(5)) + ')' ELSE '' END + CASE WHEN IsNullable = 1 THEN ' NULL ' ELSE ' NOT NULL ' END --+ CASE WHEN ColumnDefaultName IS NOT NULL AND @IncludeConstraints = 1 THEN 'CONSTRAINT [' + ColumnDefaultName + '] DEFAULT' + UPPER(ColumnDefaultValue) ELSE '' END END + CASE WHEN FieldID = (SELECT MAX(FieldID) FROM #ShowFields) THEN '' ELSE ',' END FROM #ShowFields SELECT @sStr = @sStr + ')' INSERT INTO #Constraints (Constraint_Type, SQL,Constraint_Name) VALUES ('CREATE_TABLE', @sStr,QUOTENAME(@Schema_Name) + '.' + QUOTENAME(@sTable_Name)) END IF @PK_Ind = 1 BEGIN -- Get Object ID of the PK SELECT DISTINCT ObjectID = cco.object_id INTO #PKObjectID FROM sys.key_constraints cco JOIN sys.index_columns cc ON cco.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id WHERE OBJECT_NAME(parent_object_id) = @sTable_Name AND i.type = 1 AND is_primary_key = 1 -- Get Object ID of the Uniques SELECT DISTINCT ObjectID = cco.object_id INTO #Uniques FROM sys.key_constraints cco JOIN sys.index_columns cc ON cco.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id WHERE OBJECT_NAME(parent_object_id) = @sTable_Name AND i.type = 2 AND is_primary_key = 0 AND is_unique_constraint = 1 INSERT INTO #Constraints (Constraint_Type,Constraint_Name,SQL) SELECT 'PK_UNIQUE_CONSTRAINT', Constraint_Name = cco.name, [PK_UNIQUE_CONSTRAINTS] = ISNULL('ALTER TABLE ' + QUOTENAME(s.name) + '.' + QUOTENAME(@sTable_Name) + ' ADD CONSTRAINT ' + QUOTENAME(cco.name ) + CASE type WHEN 'PK' THEN ' PRIMARY KEY ' + CASE WHEN pk.ObjectID IS NULL THEN 'NONCLUSTERED ' ELSE 'CLUSTERED ' END WHEN 'UQ' THEN ' UNIQUE ' END + CASE WHEN u.ObjectID IS NOT NULL THEN ' NONCLUSTERED ' ELSE '' END + '(' + REVERSE(SUBSTRING(REVERSE(( SELECT c.name + + CASE WHEN cc.is_descending_key = 1 THEN ' DESC' ELSE ' ASC' END + ',' FROM sys.key_constraints ccok LEFT JOIN sys.index_columns cc ON ccok.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id LEFT JOIN sys.columns c ON cc.object_id = c.object_id AND cc.column_id = c.column_id LEFT JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id WHERE i.object_id = ccok.parent_object_id AND ccok.object_id = cco.object_id FOR XML PATH('') ) ), 2, 8000)) + ')','') FROM sys.key_constraints cco INNER JOIN sys.schemas s ON cco.schema_id = s.schema_id LEFT JOIN #PKObjectID pk ON cco.object_id = pk.ObjectID LEFT JOIN #Uniques u ON cco.object_id = u.objectID WHERE OBJECT_NAME(cco.parent_object_id) = @sTable_Name AND (type = 'PK' OR type = CASE WHEN @UniqueConstraints = 1 THEN 'UQ' ELSE 'PK' END ) END IF @FK_Ind = 1 BEGIN PRINT 'Creating SQL for FK Constraints ...' INSERT INTO #Constraints (Constraint_Type, Constraint_Name,SQL) SELECT 'FK_CONSTRAINT', Constraint_Name=a.Name, [FK_CONSTRAINTS] = 'ALTER TABLE ' + QUOTENAME(s.name) + '.' + QUOTENAME(o.name) + ' WITH CHECK ADD CONSTRAINT ' + QUOTENAME(a.name) + ' FOREIGN KEY (' + a.ParentColumns + ') REFERENCES ' + QUOTENAME(a.ReferencedSchema) +'.' + QUOTENAME(a.ReferencedObject) + '(' + a.ReferencedColumns + ')' FROM ( SELECT fk.OBJECT_ID as object_id, ReferencedSchema = SCHEMA_NAME(o.Schema_ID), ReferencedObject = OBJECT_NAME(fk.referenced_object_id), ParentObject = OBJECT_NAME(fk.parent_object_id), Name = fk.name, ParentColumns = REVERSE(SUBSTRING(REVERSE(( SELECT cp.name + ',' FROM sys.foreign_key_columns fkc JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id WHERE fkc.constraint_object_id = fk.object_id FOR XML PATH('') ) ), 2, 8000)), ReferencedColumns = REVERSE(SUBSTRING(REVERSE(( SELECT cr.name + ',' FROM sys.foreign_key_columns fkc JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id WHERE fkc.constraint_object_id = fk.object_id FOR XML PATH('') ) ), 2, 8000)) FROM sys.foreign_keys fk INNER JOIN sys.objects o ON fk.referenced_object_id = o.object_id ) a INNER JOIN sys.objects co ON a.object_id = co.object_id INNER JOIN sys.objects o ON co.parent_object_id = o.object_id INNER JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE a.ParentObject = @sTable_Name ORDER BY a.name END IF @Check_Ind = 1 BEGIN -- Create check constraints for all the columns of a table INSERT INTO #Constraints (Constraint_Type,SQL) SELECT 'CHECK_CONSTRAINT', [CHECK_CONSTRAINTS] = 'ALTER TABLE ' + QUOTENAME(s.name) + '.' + QUOTENAME(o.name) + ' WITH CHECK ADD CHECK ' + cc.definition + ';' FROM sys.check_constraints cc INNER JOIN sys.objects co ON cc.object_id = co.object_id INNER JOIN sys.objects o ON co.parent_object_id = o.object_id INNER JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE OBJECT_NAME(cc.parent_object_id) = @sTable_Name ORDER BY o.name END IF @Default_Ind = 1 BEGIN -- Create defaults for all the columns of a table INSERT INTO #Constraints (Constraint_Type,SQL) SELECT 'DEFAULT_CONSTRAINT', [DEFAULT_CONSTRAINTS] = 'ALTER TABLE ' + QUOTENAME(s.name) + '.' + QUOTENAME(o.name) + ' ADD DEFAULT ' + sc.text + ' FOR ' + c.name FROM syscomments sc INNER JOIN syscolumns c ON sc.id = c.cdefault INNER JOIN sys.objects o ON c.id = o.object_id INNER JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE sc.id IN ( SELECT cdefault FROM syscolumns WHERE id = OBJECT_ID(@Schema_Name + '.' + @sTable_Name) AND cdefault > 0 ) ORDER BY c.name END SELECT * FROM #Constraints WHERE (1 = 1 OR Constraint_Type = CASE WHEN @Default_Ind = 1 THEN 'DEFAULT_CONSTRAINT' ELSE '' END OR Constraint_Type = CASE WHEN @Check_Ind = 1 THEN 'CHECK_CONSTRAINT' ELSE '' END OR Constraint_Type = CASE WHEN @PK_Ind = 1 THEN 'PK_UNIQUE_CONSTRAINT' ELSE '' END OR Constraint_Type = CASE WHEN @FK_Ind = 1 THEN 'FK_CONSTRAINT' ELSE '' END ) ORDER BY ID END GO --EXEC usp_CreateTable_DDL "表名称"
相关推荐
标题提到的“Excel根据表格,批量生成sqlserver语句,生成建表语句,自行到数据库中执行”,就是一种利用Excel宏自动化生成SQL Server建表语句的方法。这种方法适用于已有数据结构清晰的Excel表格,通过特定的规则...
假设我们有一个表结构模板,如一个包含所有表定义的元数据表,我们可以遍历这个元数据表,为每个表名生成相应的CREATE TABLE语句。以下是一个简单的示例: ```sql DECLARE @TableName NVARCHAR(128) DECLARE ...
在这里,你可以根据自己的需求来调整针对SQL Server 2008的脚本生成规则。 接着,你需要创建或选择一个现有的数据模型。这可以通过双击已有的数据模型或在空白处新建一个来完成。在保存并进入编辑页面后,我们将...
在SQL数据库管理和日常办公中,有时我们需要将存储在SQL数据库中的数据导出到Microsoft Word文档中,以便于报告、分析或共享。这个过程涉及到数据库查询、数据处理和文档生成技术。下面将详细介绍如何实现这一操作。...
标题"PowerDesigner通过excel生成sql脚本.rar"揭示了这个压缩包中的主要内容,即利用PowerDesigner结合Excel模板文件和VB(Visual Basic)脚本来自动化生成SQL建表语句。这种方法通常用于批量处理大量表的设计,提高...
- 变量可以用来存储动态生成的表名或其他SQL语句中需要动态变化的部分。 3. **数据类型与约束**: - 在示例中的表结构中包含了多种数据类型,如`int`、`varchar`等。 - 表结构中还包含了约束,如`NOT NULL`、`...
以下是一种适用于PowerDesigner 15.1版本,针对SQL Server 2008数据库的脚本示例: ```vbscript ' 选择当前数据库模型 Select -EditCurrentDbms-Script-Objects-Column-ColumnComment ' 常量定义 Dim Value As ...
在生成 SQL 脚本时,PowerDesigner 还可以根据用户的设置生成相应的索引、视图、存储过程等数据库对象。 数据库设计导出 HTML 文档 PowerDesigner 的反向工程功能还可以将数据库设计导出为 HTML 文档,以便于对...
第17 天使用SQL 来生成SQL 语句351 目标351 使用SQL 来生成SQL 语句的目的351 几个SQL*PLUS 命令352 SET ECHO ON/OFF353 SET FEEDBACK ON/OFF353 SET HEADING ON/OFF 353 SPOOL FILENAME/OFF353 START FILENAME354 ...
3. **建表语句生成**:工具能够自动生成各表的创建语句,这些语句可以用于在其他环境中重建相同的数据库结构。 4. **视图和存储过程的处理**:除了基本的表结构,工具可能还会处理数据库中的视图和存储过程,提供...
在SQL的世界里,视图(View)是一种虚拟表,它是由SELECT语句构成的查询结果集,虽然在数据库中没有实际存储数据,但可以像操作真实表一样进行查询和更新。视图是数据库设计中的一个重要工具,它可以帮助我们简化...
- 存储过程是一组预编译的SQL语句集合。 - 它可以接受输入参数,也可以返回结果集。 - 存储过程提高了代码重用性和执行效率。 ##### 6.2 第二章节:存储过程多参数、返回值 - 多参数的存储过程可以接收多个输入参数...
SQL允许创建存储过程,其中可能包含`CREATE TABLE`语句,以实现更复杂的建表逻辑。 13. **视图**: 虽然不是直接创建表,但`CREATE VIEW`语句可以创建虚拟表,基于一个或多个表的查询结果。 14. **临时表**: ...
### SQL自学PDF知识点详解 #### 一、SQL简介与历史 - **SQL简史:** SQL(Structured Query Language,结构化查询语言)是一种用于...- **SQL对视图的处理过程:** 视图本身并不存储数据,而是存储了一个查询语句。
- **命令**: `db2 -td@ -vf <存储过程文件路径>.db2` (编译存储过程) 和 `db2 call <存储过程名>` (调用存储过程) - **功能**: 编译并调用存储过程。 通过上述详细的命令解释, 可以看到DB2提供了丰富的工具和命令来...