时间:2021-07-01 10:21:17 帮助过:5人阅读
配置文件table.sql
set nocount on declare @tablenames varchar(max), @tablename varchar(max) begin select @tablenames =‘‘ /*定义游标*/ declare table_sql cursor for SELECT name FROM sysobjects where xtype=‘U‘ order by name open table_sql fetch next from table_sql into @tablename while @@FETCH_STATUS=0 begin set @tablenames = @tablenames+@tablename+‘,‘ --print ‘---1-->‘+@tablenames -- EXEC sp_gettext ‘Address_Base‘ fetch next from table_sql into @tablename end close table_sql deallocate table_sql EXEC sp_gettext @tablenames end
配置文件:usp.sql
set nocount on declare @tablenames varchar(max), @tablename varchar(max) begin select @tablenames =‘‘ /*定义游标*/ declare table_sql cursor for SELECT name FROM sysobjects where xtype=‘P‘ order by name open table_sql fetch next from table_sql into @tablename while @@FETCH_STATUS=0 begin set @tablenames = @tablenames+@tablename+‘,‘ --print ‘---1-->‘+@tablenames -- EXEC sp_gettext ‘Address_Base‘ fetch next from table_sql into @tablename end close table_sql deallocate table_sql EXEC sp_gettext @tablenames end
配置文件:HD_ZBMX_HZ.sql
set nocount on declare @tablenames varchar(max), @tablename varchar(max) begin exec UspOutputData ‘HD_ZBMX_HZ‘ end
对应库里需要部署的存储和函数:
sp_gettext
if exists (select 1 from sysobjects where id = object_id(‘sp_gettext‘) and type = ‘P‘) drop proc sp_gettext go create procedure [sp_gettext] @name VARCHAR(max) = NULL , @identity BIT = 1 , @index TINYINT = 2 -- 0不创建索引 1不创建表 2创建索引 , @new BIT =0 as /* [版本号]1.0.0.0.0 [创建时间]2019.09.10 [作者] [版权] [描述] [功能说明] 获取建表语句,存储语句 [参数说明] [返回值] [结果集、排序] [调用的usp] sp_gettext ‘DIM_KSXX,DIM_YYXX‘ sp_gettext ‘usp_dim_ksxx‘ [调用实例] [修改记录] */ SET ARITHABORT ON; SET CONCAT_NULL_YIELDS_NULL ON; SET QUOTED_IDENTIFIER ON; SET ANSI_NULLS ON; SET ANSI_PADDING ON; SET ANSI_WARNINGS ON; SET NUMERIC_ROUNDABORT OFF; DECLARE @crlf CHAR(2); SET @crlf = CHAR(13) + CHAR(10); DECLARE @objid INT; DECLARE @results TABLE (definition NVARCHAR(max)) DECLARE @objects TABLE (id VARCHAR(100), type CHAR(2)); WITH db1(dbname) AS (SELECT VALUE AS dbname FROM Split(@name,‘,‘)), db2 AS (SELECT --CASE WHEN CHARINDEX(‘.‘, dbname) = 0 AND CHARINDEX(‘[‘, dbname) = 0 THEN ‘[cn9c080].‘ + QUOTENAME(dbname) -- ELSE dbname -- END AS dbname CASE WHEN o.[object_id] IS NULL AND tt.[name] IS NULL THEN ‘%‘ ELSE LTRIM(ISNULL(o.[object_id],tt.[type_table_object_id])) END AS object_id, CASE [db1].[dbname] WHEN ‘tables‘ THEN ‘U‘ WHEN ‘procs‘ THEN ‘P‘ else o.[type] END type FROM db1 left JOIN sys.[objects] o ON (PARSENAME(db1.[dbname],1) = o.[name] OR OBJECT_ID(‘[cn9c080].‘ + QUOTENAME(dbname)) = o.[object_id]) LEFT JOIN sys.table_types tt ON db1.dbname=tt.NAME ) INSERT INTO @objects SELECT * FROM db2 ; WITH ColumnDefs AS (SELECT TableObj = c.[object_id], ColSeq = c.column_id, ColumnDef = QUOTENAME(c.name) + ‘ ‘ + CASE WHEN c.is_computed = 1 THEN ‘as ‘ + COALESCE(k.[definition], ‘‘) + CASE WHEN k.is_persisted = 1 THEN ‘ PERSISTED‘ + CASE WHEN k.is_nullable = 0 THEN ‘ NOT NULL‘ ELSE ‘‘ END ELSE ‘‘ END ELSE DataType + CASE WHEN DataType IN ( ‘decimal‘, ‘numeric‘) THEN ‘(‘ + CAST(c.precision AS VARCHAR(10)) + CASE WHEN c.scale <> 0 THEN ‘,‘ + CAST(c.scale AS VARCHAR(10)) ELSE ‘‘ END + ‘)‘ WHEN DataType IN (‘char‘, ‘varchar‘, ‘nchar‘, ‘nvarchar‘, ‘binary‘, ‘varbinary‘) THEN ‘(‘ + CASE WHEN c.max_length = -1 THEN ‘max‘ ELSE CASE WHEN DataType IN ( ‘nchar‘, ‘nvarchar‘) THEN CAST(c.max_length / 2 AS VARCHAR(10)) ELSE CAST(c.max_length AS VARCHAR(10)) END END + ‘)‘ WHEN DataType = ‘float‘ AND c.precision <> 53 THEN ‘(‘ + CAST(c.precision AS VARCHAR(10)) + ‘)‘ WHEN DataType IN (‘time‘, ‘datetime2‘, ‘datetimeoffset‘) AND c.scale <> 7 THEN ‘(‘ + CAST(c.scale AS VARCHAR(10)) + ‘)‘ ELSE ‘‘ END END + CASE WHEN c.is_identity = 1 AND @identity = 1 THEN ‘ IDENTITY(‘ + CAST(IDENT_SEED(QUOTENAME(OBJECT_SCHEMA_NAME(c.[object_id])) + ‘.‘ + QUOTENAME(OBJECT_NAME(c.[object_id]))) AS VARCHAR(30)) + ‘,‘ + CAST(IDENT_INCR(QUOTENAME(OBJECT_SCHEMA_NAME(c.[object_id])) + ‘.‘ + QUOTENAME(OBJECT_NAME(c.[object_id]))) AS VARCHAR(30)) + ‘)‘ ELSE ‘‘ END + CASE WHEN c.is_rowguidcol = 1 THEN ‘ ROWGUIDCOL‘ ELSE ‘‘ END + CASE WHEN c.xml_collection_id > 0 THEN ‘ (CONTENT ‘ + QUOTENAME(SCHEMA_NAME(x.schema_id)) + ‘.‘ + QUOTENAME(x.name) + ‘)‘ ELSE ‘‘ END + CASE WHEN c.is_computed = 0 AND UserDefinedFlag = 0 THEN CASE WHEN c.collation_name <> CAST(DATABASEPROPERTYEX(DB_NAME(), ‘collation‘) AS NVARCHAR(128)) THEN ‘ COLLATE ‘ + c.collation_name ELSE ‘‘ END ELSE ‘‘ END + CASE WHEN c.is_computed = 0 THEN CASE WHEN c.is_nullable = 0 THEN ‘ NOT‘ ELSE ‘‘ END + ‘ NULL‘ ELSE ‘‘ END + CASE WHEN c.default_object_id > 0 AND ISNULL(@new,0) = 0 THEN ‘ CONSTRAINT ‘ + QUOTENAME(d.name) + ‘ DEFAULT ‘ + COALESCE(d.[definition], ‘‘) WHEN c.default_object_id > 0 AND ISNULL(@new,0) = 1 THEN ‘ DEFAULT ‘ + COALESCE(d.[definition], ‘‘) ELSE ‘‘ END FROM sys.columns c CROSS APPLY (SELECT DataType = TYPE_NAME(c.user_type_id) , UserDefinedFlag = CASE WHEN c.system_type_id = c.user_type_id THEN 0 ELSE 1 END) F1 LEFT JOIN sys.default_constraints d ON c.default_object_id = d.[object_id] LEFT JOIN sys.computed_columns k ON c.[object_id] = k.[object_id] AND c.column_id = k.column_id LEFT JOIN sys.xml_schema_collections x ON c.xml_collection_id = x.xml_collection_id), IndexDefs AS (SELECT TableObj = i.[object_id], IxName = QUOTENAME(i.name+CASE WHEN @new=1 THEN ‘_‘+LEFT(NEWID(),4) ELSE ‘‘ end), IxPKFlag = i.is_primary_key, IxType = CASE WHEN i.is_primary_key = 1 THEN ‘PRIMARY KEY ‘ WHEN i.is_unique = 1 THEN ‘UNIQUE ‘ ELSE ‘‘ END + LOWER(type_desc), IxDef = ‘(‘ + IxColList + ‘)‘ + COALESCE(‘ INCLUDE (‘ + IxInclList + ‘)‘, ‘‘), IxOpts = IxOptList FROM sys.indexes i LEFT JOIN sys.stats s ON i.index_id = s.stats_id AND i.[object_id] = s.[object_id] CROSS APPLY (SELECT STUFF((SELECT CASE WHEN i.is_padded = 1 THEN ‘, PAD_INDEX=ON‘ ELSE ‘‘ END + CASE WHEN i.fill_factor <> 0 THEN ‘, FILLFACTOR=‘ + CAST(i.fill_factor AS VARCHAR(10)) ELSE ‘‘ END + CASE WHEN i.ignore_dup_key = 1 THEN ‘, IGNORE_DUP_KEY=ON‘ ELSE ‘‘ END + CASE WHEN s.no_recompute = 1 THEN ‘, STATISTICS_RECOMPUTE=ON‘ ELSE ‘‘ END + CASE WHEN i.allow_row_locks = 0 THEN ‘, ALLOW_ROW_LOCKS=OFF‘ ELSE ‘‘ END + CASE WHEN i.allow_page_locks = 0 THEN ‘, ALLOW_PAGE_LOCKS=OFF‘ ELSE ‘‘ END), 1, 2, ‘‘)) F_IxOpts (IxOptList) CROSS APPLY (SELECT STUFF((SELECT ‘,‘ + QUOTENAME(c.name) + CASE WHEN ic.is_descending_key = 1 AND i.type <> 3 THEN ‘ DESC‘ WHEN ic.is_descending_key = 0 AND i.type <> 3 THEN ‘ ASC‘ ELSE ‘‘ END FROM sys.index_columns ic JOIN sys.columns c ON ic.[object_id] = c.[object_id] AND ic.column_id = c.column_id WHERE ic.[object_id] = i.[object_id] AND ic.index_id = i.index_id AND ic.is_included_column = 0 ORDER BY ic.key_ordinal FOR XML PATH(‘‘) , TYPE).value(‘.‘, ‘nvarchar(max)‘), 1, 1, ‘‘)) F_IxCols (IxColList) CROSS APPLY (SELECT STUFF((SELECT ‘,‘ + QUOTENAME(c.name) FROM sys.index_columns ic JOIN sys.columns c ON ic.[object_id] = c.[object_id] AND ic.column_id = c.column_id WHERE ic.[object_id] = i.[object_id] AND ic.index_id = i.index_id AND ic.is_included_column = 1 ORDER BY ic.key_ordinal FOR XML PATH(‘‘) , TYPE).value(‘.‘, ‘nvarchar(max)‘),