zoukankan      html  css  js  c++  java
  • Sqlserver 2008:sp_msforeachdb 坑爹的错误陷阱

    不多说。看代码

    -- 一般的写法,可能错误 1
    exec sp_msforeachdb
    '
    if ''?'' like ''edb_a_____''
    begin
        if not exists
        (
            select *
            from ?.dbo.sysobjects a inner join ?.dbo.syscolumns b on a.id=b.id
            where a.name=''WFPUSER_T1480'' and b.name=''TC1364''
        )
        begin
            raiserror(''?'',14,1)
        end
        else
            print ''ok''
    end
    
    '
    
    -- 一般的写法,可能错误 2
    exec sp_msforeachdb
    '
    use ?
    
    if ''?'' like ''edb_a_____''
    begin
        if not exists
        (
            select *
            from sysobjects a inner join syscolumns b on a.id=b.id
            where a.name=''WFPUSER_T1480'' and b.name=''TC1364''
        )
        begin
            raiserror(''?'',14,1)
        end
        else
            print ''ok''
    end
    
    '

    确保正确的写法:

    -- 对比 0
    exec sp_msforeachdb
    '
    
    if ''?'' like ''edb_a_____''
    begin
        if not exists
        (
            select *
            from [?].dbo.sysobjects a inner join [?].dbo.syscolumns b on a.id=b.id
            where a.name=''WFPUSER_T1480'' and b.name=''TC1364''
        )
        begin
            raiserror(''?'',14,1)
        end
        else
            print ''ok''
    end
    
    '
    
    -- 对比 1 
    exec sp_msforeachdb
    '
    if ''?'' like ''edb_a_____''
    begin
        if not exists
        (
            select *
            from [?]..sysobjects a inner join [?]..syscolumns b on a.id=b.id
            where a.name=''WFPUSER_T1480'' and b.name=''TC1364''
        )
        begin
            raiserror(''?'',14,1)
        end
        else
            print ''ok''
    end
    
    '
    
    -- 对比 2 
    exec sp_msforeachdb
    '
    use [?]
    
    if ''?'' like ''edb_a_____''
    begin
    
        if not exists
        (
            select *
            from dbo.sysobjects a inner join dbo.syscolumns b on a.id=b.id
            where a.name=''WFPUSER_T1480'' and b.name=''TB0079''
        )
        begin
            raiserror(''?'',14,1)
        end
        else
            print ''ok''
            
    end
    
    '
    
    -- 对比 3
    exec sp_msforeachdb
    '
    use [?]
    
    if ''?'' like ''edb_a_____''
    begin
    
        if not exists
        (
            select *
            from sysobjects a inner join syscolumns b on a.id=b.id
            where a.name=''WFPUSER_T1480'' and b.name=''TB0079''
        )
        begin
            raiserror(''?'',14,1)
        end
        else
            print ''ok''
            
    end
    
    '
  • 相关阅读:
    程序员的成长
    存储系统的基本数据结构之一: 跳表 (SkipList)
    【机器学习-斯坦福】学习笔记3
    TCP/IP入门(3) --传输层
    2015华为暑期实习(北京)面试经验
    C++面试中关于sizeof问题总结
    KMP详解
    hihoCoder #1014 : Trie树
    Trie树的创建、插入、查询的实现
    Trie树(c++实现)
  • 原文地址:https://www.cnblogs.com/jinzhenshui/p/2605163.html
Copyright © 2011-2022 走看看