zoukankan      html  css  js  c++  java
  • 查找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
    执行查询出来的结果
     
  • 相关阅读:
    php学习笔记
    附加题-重构的读后总结
    附加题-stack的理解
    (转)php的扩展和嵌入--php的生命周期与变量详述
    homework-09
    html学习笔记之position
    homework-06
    homework-08
    在windows下使用git需要反复输入用户名和密码的问题
    windows命令行编码与nodejs编码格式冲突的解决方式
  • 原文地址:https://www.cnblogs.com/LuoEast/p/8351498.html
Copyright © 2011-2022 走看看