时间:2021-07-01 10:21:17 帮助过:61人阅读
此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。 无 --视图、存储过程、函数名称DECLARE @NAME NVARCHAR(255);--局部游标DECLARE @CUR CURSOR --自动修改未上状态为旷课SET @CUR=CURSOR SCROLL DYNAMIC FOR SELECT NAME FROM
此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。
<无>--视图、存储过程、函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR SCROLL DYNAMIC FOR SELECT NAME FROM DBO.SYSOBJECTS WHERE NAME NOT IN ('SYSCONSTRAINTS','SYSSEGMENTS') AND ( OBJECTPROPERTY(ID, N'IsView') = 1 --视图 OR OBJECTPROPERTY(ID,N'IsProcedure') = 1 --存储过程 OR OBJECTPROPERTY(ID,N'IsScalarFunction') = 1 --标量函数 OR OBJECTPROPERTY(ID,N'IsTableFunction') = 1 --标题函数 OR OBJECTPROPERTY(ID,N'IsInlineFunction') = 1 --内联函数 ); OPEN @CUR; FETCH NEXT FROM @CUR INTO @NAME WHILE (@@FETCH_STATUS=0) BEGIN DECLARE @OldText NVARCHAR(MAX); DECLARE @NewText NVARCHAR(MAX); --读取创建脚本,当脚本超长时分成多条记录时合并 SELECT @OldText=@OldText + CHAR(10) + CHAR(13) + RTRIM(TEXT) FROM SYSCOMMENTS WHERE ID = OBJECT_ID(@NAME); --将创建脚本替换为更新脚本 SET @NewText=REPLACE(@OldText,N'CREATE VIEW',N'ALTER VIEW'); SET @NewText=REPLACE(@NewText,N'CREATE PROCEDURE',N'ALTER PROCEDURE'); SET @NewText=REPLACE(@NewText,N'CREATE FUNCTION',N'ALTER FUNCTION'); BEGIN TRY EXEC(@NewText); END TRY BEGIN CATCH PRINT N'---------------------------------------------------------------------------'; PRINT @NAME + N' : ' + ERROR_MESSAGE(); --PRINT @OldText; PRINT N'---------------------------------------------------------------------------'; END CATCH FETCH NEXT FROM @CUR INTO @NAME END CLOSE @CUR; DEALLOCATE @CUR;
CREATE PROCEDURE RefreshAllView AS DECLARE MyCursor CURSOR FOR select Name from dbo.sysobjects where OBJECTPROPERTY(id, N'IsView') = 1 and (not name in ('sysconstraints','syssegments')) DECLARE @name varchar(40) OPEN MyCursor FETCH NEXT FROM MyCursor INTO @name WHILE (@@fetch_status <> -1) BEGIN IF (@@fetch_status <> -2) begin exec sp_refreshview @name end FETCH NEXT FROM MyCursor INTO @name END CLOSE MyCursor DEALLOCATE MyCursor
select name from sysobjects where xtype='TR' --所有触发器 select name from sysobjects where xtype='P' --所有存储过程 select name from sysobjects where xtype='V' --所有视图 select name from sysobjects where xtype='U' --所有表
SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.name, 标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ))) then '√' else '' end, 类型=b.name, 占用字节数=a.length, 长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), 允许空=case when a.isnullable=1 then '√'else '' end, 默认值=isnull(e.text,''), 字段说明=isnull(g.[value],'') FROM syscolumns a left join systypes b on a.xusertype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0 --where d.name='course' --如果只查询指定表,加上此条件 order by a.id,a.colorder
Select o.Name As ObjectsName , c.name As ColumnsName , t.name As ColumnsType , c.length As ColumnsLength From SysObjects As o , SysColumns As c , SysTypes As t Where o.type in ('u','v') And o.id = c.id And c.xtype = t.xtype Order By o.name , c.name , t.name , c.Length