我的公司使用来自众多客户的数据,并忽略记录我们的数据库表和字段所代表的内容。为了解决这个问题,我编写了一些存储过程,这些存储过程似乎只适用于他们所在的数据库。我想在服务器上有一个存储过程的实例,可以在其所有数据库上使用,但无法弄清楚如何实现这一点。下面是程序:SQL存储过程 - 从多个数据库执行
步骤1 - sp_GetTableDocumentation
Create Procedure sp_GetTableDocumentation(@TableName SYSNAME)
AS
SELECT
@TableName AS [Table Name]
,'' AS [Column Name]
,CONVERT(NVARCHAR(MAX), ISNULL(D.value, '')) AS [Description]
FROM sys.Tables AS T
OUTER APPLY (SELECT TOP 1 * FROM ::fn_listextendedproperty('Description', 'SCHEMA', 'dbo', 'TABLE', @TableName, NULL, NULL)) AS D
WHERE T.Name = @TableName
UNION ALL
SELECT
@TableName AS [Table Name]
,C.Name AS [Column Name]
,CONVERT(NVARCHAR(MAX), ISNULL(D.value, '')) AS [Description]
FROM sys.Tables AS T
INNER JOIN sys.Columns AS C ON T.Object_id = C.Object_id
OUTER APPLY (SELECT TOP 1 * FROM ::fn_listextendedproperty('Description', 'SCHEMA', 'dbo', 'TABLE', @TableName, 'COLUMN', C.Name)) AS D
WHERE T.Name = @TableName
GO
步骤2 - sp_SetTableDocumentation
Create 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',@tableName
GO
程序3 - sp_SetTableDescription
Create 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',@tableName
GO
步骤4 - sp_SetColumnDescription
CREATE PROCEDURE sp_SetColumnDescription (
@schemaName SYSNAME
,@tableName SYSNAME
,@columnName SYSNAME
,@description SQL_VARIANT
)
AS
IF EXISTS (
SELECT 1
FROM fn_listextendedproperty('Description', 'SCHEMA', @schemaName, 'TABLE', @tableName, 'COLUMN', @columnName)
)
EXEC sp_DropExtendedProperty 'Description','SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnName
IF (NOT @description IS NULL) AND (NOT @description = '')
EXEC sp_AddExtendedProperty 'Description',@description,'SCHEMA',@schemaName,'TABLE',@tableName,'COLUMN',@columnName
GO
感谢
对于用户定义的db对象,'sp_'命名约定是不好的做法 – lad2025
这里是一篇文章,介绍为什么sp_是不好的做法。 http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix –
你将不得不实现动态sql来使其成为一组通用的过程,以便您可以定义它将针对哪个数据库执行。它可以完成,但会是一个相当大的工作,因为除了实际使它与动态SQL功能,它也需要从SQL注入安全。 –