Batch update SQL table describes the information

- in accordance with known batch updates the database table table shows descriptive information, create a stored procedure, call
/ ****** Object: StoredProcedure [dbo] [UpTableDescription] Script Date: 09/09/2019 10:30:11. ****** /
the SET the ANSI_NULLS the ON
the GO
the SET the ON the QUOTED_IDENTIFIER
the GO
the CREATE proc [the dbo]. [UpTableDescription]
the AS
the BEGIN
the DECLARE @tbname NVARCHAR (100)
the DECLARE @tbDescription NVARCHAR (max)
the DECLARE @ tbDescription1 NVARCHAR (max)
the DECLARE @sqlstr NVARCHAR (max)
the DECLARE @CTableName NVARCHAR (max)
the DECLARE @DE NVARCHAR (max)

DECLARE user_cur CURSOR FOR - query the system tables
SELECT * FROM (SELECT distinct
table name = case when a.colorder = 1 then d.name else '' end,
table illustrates = case when a.colorder = 1 then convert (nvarchar (max ), ISNULL (f.value, '')) the else '' End
the FROM
the syscolumns A
Inner a.id the Join the sysobjects ON = D = d.xtype d.id and 'the U-' and d.name <> 'the dtproperties'
left the JOIN F = ON d.id f.major_id sys.extended_properties and f.minor_id = 0) TB
the WHERE table <> '' aND table IS NOT NULL aND tb. table Description = ''

the OPEN user_cur
the FETCH the FROM user_cur the INTO @tbname the NEXT , @ tbDescription - circulation system tables
the WHILE @@ fetch_status <> -. 1
the BEGIN
. --1 erp determined whether this data dictionary information is
the SET CTableName @ = ''
the SET DE @ = ''
SET @tbDescription1=@tbDescription
SET @sqlstr=' SELECT DISTINCT @mt=[CTableName],@DE=[DE] FROM dbo.T_1
where Etablename='''+@tbname +''''
exec sp_executesql @sqlstr,N'@mt nvarchar(max) output,@DE nvarchar(max) output',@CTableName OUTPUT,@DE OUTPUT

IF @tbname<>''
BEGIN
IF @DE<>'' --erp数据字典存在此说明信息
SET @DE=@CTableName+':'+ @DE
ELSE
SET @DE=@CTableName
END
IF @tbDescription<>'' ---1.系统表注释存在
BEGIN
SET @sqlstr='EXEC sys.sp_dropextendedproperty @name=N''MS_Description'' ,
@level0type=N''SCHEMA'',
@level0name=N''dbo'',
@level1type=N''TABLE'',
@level1name=N'''+@tbname+''''
EXEC(@sqlstr)
SET @tbDescription=''
END

IF @tbDescription='' ---1.系统表注释不存在
BEGIN
SET @sqlstr='EXEC sys.sp_addextendedproperty @name=''MS_Description'',
@value = '''+@DE+''' ,
@level0type = ''SCHEMA'',
@level0name = N''dbo'',
@level1type = N''TABLE'',
@level1name = '''+@tbname+''''
EXEC(@sqlstr)
END
ELSE
BEGIN
SET @sqlstr='EXEC sys.sp_updateextendedproperty @name=''MS_Description'',
@value = '''+@DE+''' ,
@level0type = ''SCHEMA'',
@level0name = N''dbo'',
@level1type = N''TABLE'',
@level1name = '''+@tbname+''''
EXEC(@sqlstr)
END

FETCH NEXT FROM user_cur INTO @tbname,@tbDescription
END
CLOSE user_cur
DEALLOCATE user_cur


END

--EXEC UpTableDescription call a stored procedure 

 

Guess you like

Origin www.cnblogs.com/zhou0811/p/11495406.html
Recommended