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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章