首页 > 数据库技术 > 详细

SQLServer 查询view中是否包含某个关键字

时间:2019-08-28 11:56:46      阅读:117      评论:0      收藏:0      [点我收藏+]

在数据库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

SQLServer 查询view中是否包含某个关键字

原文:https://www.cnblogs.com/grj1046/p/FindFromViewContent.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!