查找SQL 存储过程、触发器、视图!

ALTER proc [dbo].[SP_SQL](@ObjectName sysname)  
as  
set nocount on ;  
declare @Print nvarchar(max)----@Print用nvarchar  
if exists(select 1 from syscomments  where ID=object_id(@ObjectName) and encrypted=1)  
    begin  
         Print N'对象已加密!'  
         return  
    end  
  
if coalesce(object_id(@ObjectName,N'P'),object_id(@ObjectName,N'FN'),object_id(@ObjectName,N'IF'),object_id(@ObjectName,N'TF'),object_id(@ObjectName,N'TR'),object_id(@ObjectName,N'V')) is  null  
    begin  
        Print N'对象只针对函数、存储过程、触发器、视图!'  
        return   
    end  
--print 'Use '+db_Name()  
--print 'Go'  
print 'if object_ID('+quotename(case when charindex(']',@ObjectName)=0 then '['+replace(rtrim(@ObjectName),'.','].[')+']' else @ObjectName end ,'''')+') is not null'  
print char(9)+'Drop '+case  when object_id(@ObjectName,N'P') is not null then 'Procedure ' when Coalesce(object_id(@ObjectName,N'FN'),object_id(@ObjectName,N'IF'),object_id(@ObjectName,N'TF')) is not null then 'Function ' when object_id(@ObjectName,N'TR') is not null then 'Trigger ' else 'View 'end +case when charindex(']',@ObjectName)=0 then '['+replace(rtrim(@ObjectName),'.','].[')+']' else @ObjectName end  
Print 'Go'  
 
declare @T table(Col nvarchar(max))  
insert @T select object_definition(object_id(@ObjectName)) +char(13)+char(10) while (select max(Col) from @T)>''  
    begin  
        select top 1 @Print=left(Col,charindex(char(13)+char(10),Col)-1) from @T  
        print @Print  
        update @T set Col=stuff(Col,1,charindex(char(13)+char(10),Col)+1,'')  
    end  
print 'Go' 
 
 
SET NOCOUNT ON;
DROP TABLE #TEMP
SELECT NAME INTO #TEMP FROM SYS.objects WHERE type='TR'
DECLARE @Count INT =0,
@CurrentNo INT = 1,
@Name sysname

SELECT @Count = COUNT(1) FROM #TEMP
WHILE @CurrentNo<=@Count
BEGIN
    SELECT @Name = name FROM #TEMP
PRINT 'EXEC SP_SQL '''+@Name+''''
SET @CurrentNo = @CurrentNo+1
DELETE #TEMP WHERE name=@Name
END
执行查询出来的结果
 
posted @   LuoCore  阅读(251)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示