回答

收藏

SQL存储过程-从多个数据库执行

技术问答 技术问答 172 人阅读 | 0 人回复 | 2023-09-12

我公司使用来自许多客户的数据,而忽略了记录数据库的表和字段所代表的内容。为了解决这个问题,我编写了一些存储过程,这似乎只对他们所依赖的数据库有效。我认为服务器上有一个存储过程的例子,可以用于所有数据库,但我不知道如何实现。步骤如下:7 p2 m/ m/ P* I4 t- ]4 y- c
过程1-sp_GetTableDocumentationCreate Procedure sp_GetTableDocumentation(@TableName SYSNAME)ASSELECT    @TableName AS [Table Name]   , AS [Column Name]    ,CONVERT(NVARCHAR(MAX),ISNULL(D.value,'')) AS [Description]FROM sys.Tables AS TOUTER APPLY (SELECT TOP 1 * FROM ::fn_listextendedproperty('Description','SCHEMA','dbo','TABLE',@TableName,NULL,NULL)) AS DWHERE T.Name = @TableNameUNION ALLSELECT    @TableName AS [Table Name]    ,C.Name AS [Column Name]    ,CONVERT(NVARCHAR(MAX),ISNULL(D.value,'')) AS [Description]FROM sys.Tables AS TINNER JOIN sys.Columns AS C ON T.Object_id = C.Object_idOUTER APPLY (SELECT TOP 1 * FROM ::fn_listextendedproperty('Description','SCHEMA','dbo','TABLE',@TableName,'COLUMN',C.Name)) AS DWHERE T.Name = @TableNameGO过程2-sp_SetTableDocumentationCreate Procedure sp_SetTableDescription(  @schemaName sysname  ,@tableName sysname  ,@description sql_variant)As    If Exists (      Select 1       From fn_listextendedproperty('Description','SCHEMA',@schemaName,'TABLE',@tableName,NULL,NULL)    )        exec sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableName         If (Not @description Is Null) And (Not @description =    exec sp_AddExtendedProperty 'Description',@description,'SCHEMA',@schemaName,'TABLE',@tableNameGO过程3-sp_SetTableDescriptionCreate Procedure sp_SetTableDescription(  @schemaName sysname  ,@tableName sysname  ,@description sql_variant)AsIf Exists  Select 1     From fn_listextendedproperty('Description','SCHEMA',@schemaName,'TABLE',@tableName,NULL,NULL))    exec sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableNameIf (Not @description Is Null) And (Not @description = '')exec sp_AddExtendedProperty 'Description',@description,'SCHEMA',@schemaName,'TABLE',@tableNameGO过程4-sp_SetColumnDescriptionCREATE PROCEDURE sp_SetColumnDescription  @schemaName SYSNAME    ,@tableName SYSNAME    ,@columnName SYSNAME    ,@description SQL_VARIANT    )ASIF EXISTS      SELECT                 FROM fn_listextendedproperty('Description','SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnName)        )    EXEC sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnNameIF (NOT @description IS NULL) AND (NOT @description =    EXEC sp_AddExtendedProperty 'Description',@description,'SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnNameGO谢谢3 S0 L- ]8 f) F' W; ^* y
                                                               
5 F/ {* e. B/ g) H/ @    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则