刷新SQL Server所有視圖、函數、存儲過程
此腳本用於在刪除或添加字段時刷新相關視圖,並檢查視圖、函數、存儲過程有效性。
[SQL]代碼
--視圖、存儲過程、函數名稱
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;
SQL Server修改表結構后批量更新所有視圖
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
SQL SERVER得到表、視圖、存儲過程、觸發器、字段的信息
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' --所有表
SQL SERVER得到某個數據庫下面所有表字段的相信信息或者某個表的相信信息
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
獲取指定SQLServer數據庫所有表及視圖的字段列表及類型、長度
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
