SQLServer 查詢view中是否包含某個關鍵字

在數據庫view的創建中,會遇到一些跨數據庫的view腳本,但是在將view更新到production的時候可能忘記更改database name,導致出現一些問題。
以下腳本可以檢查出包含某個關鍵字的view name,只需要修改where objectText like '%word%'條件即可

--type(v,fn,p)
select name,ROW_NUMBER()over(order by name) As Number into #tempObjects from sys.objects where type='V'
--
declare @rowindex int
declare @rowcount int
--
select @rowcount=count(*) from #tempObjects
set @rowindex=1
--
create table #tempFindObjectText(objectText varchar(max))
create table #tempFindObject(objectName varchar(500))
--
declare @objectName varchar(500)
--
while @rowindex<=@rowcount
begin
   select @objectName=name from #tempObjects where Number=@rowindex
   --
   truncate table #tempFindObjectText
   --
   insert into #tempFindObjectText(objectText)
   execute sp_helptext @objectName
   --
   if exists(select top 1 * from #tempFindObjectText where objectText like '%word%')
   begin
      Insert Into #tempFindObject(objectName)Values(@objectName)
   end
   --
   set @rowindex=@rowindex+1
end
--
select * from #tempFindObject
--
truncate table #tempObjects
truncate table #tempFindObjectText
truncate table #tempFindObject
--
drop table #tempObjects
drop table #tempFindObjectText
drop table #tempFindObject
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章