代码之家  ›  专栏  ›  技术社区  ›  Jesse

引用SQL的命名实例时出现内部SQL Server错误

  •  0
  • Jesse  · 技术社区  · 17 年前

    我正在尝试使用链接服务器编写一些从源数据库到目标数据库的插入脚本。我们第一次使用第二个命名的SQL实例,当我运行以下代码时,它会给我一个“内部SQL服务器错误”。如果删除命名实例周围的括号,我将不再获得内部SQL Server错误,但确实会得到一个“靠近'.''错误的语法”。任何帮助都非常感谢。

    declare @listname as varchar(80) 
    declare @sourcedb as sysname
    declare @targetdb as sysname
    
    set @listname = 'List(r) Stuff - Planning'
    
    set @sourcedb = '[server1.domain.com\server1sql2005].database.dbo.'
    set @targetdb = 'server2.database.dbo.'
    
    
    print @listname
    
    exec ('if not exists (select * from ' + @targetdb + 'listmatchingheader where listname = ''' + @listname + ''') 
    begin
        insert ' + @targetdb + 'listmatchingheader (listname, userdefinedname1, userdefinedname2, userdefinedname3, siterecnum, dolplistnum, listdate, regdate, customlist, footer) 
            select 
                mlmh.listname, 
                mlmh.userdefinedname1, 
                mlmh.userdefinedname2, 
                mlmh.userdefinedname3, 
                mlmh.siterecnum, 
                mlmh.dolplistnum, 
                mlmh.listdate, 
                mlmh.regdate, 
                mlmh.customlist, 
                mlmh.footer
            from ' + @sourcedb + 'listmatchingheader mlmh 
            where mlmh.listname = ''' + @listname + ''' 
        end
        else
        begin
            update ' + @targetdb + 'listmatchingheader set  
                listname = mlmh.listname, 
                userdefinedname1 = mlmh.userdefinedname1, 
                userdefinedname2 = mlmh.userdefinedname2, 
                userdefinedname3 = mlmh.userdefinedname3, 
                siterecnum = mlmh.siterecnum, 
                dolplistnum = mlmh.dolplistnum, 
                listdate = mlmh.listdate, 
                regdate = mlmh.regdate, 
                customlist = mlmh.customlist,
                footer = mlmh.footer 
            from ' + @targetdb + 'listmatchingheader lmh 
            inner join ' + @sourcedb + 'listmatchingheader mlmh on lmh.listname = mlmh.listname 
            where mlmh.listname = ''' + @listname + ''' 
        end
        ')
    
    exec ('
        delete ' + @targetdb + 'listmatching 
        from ' + @targetdb + 'listmatching lm 
        inner join ' + @targetdb + 'listmatchingheader lmh on lm.listrecnum = lmh.listrecnum 
        where lmh.listname = ''' + @listname + ''' 
        ')
    
    exec ('
        delete ' + @targetdb + 'listitemcass 
        from ' + @targetdb + 'listitemcass lic 
        where not exists (select * from ' + @targetdb + 'listmatching where listitemrecnum = lic.listitemrecnum) 
        ')
    
    exec ('
        delete ' + @targetdb + 'listitemingreds 
        from ' + @targetdb + 'listitemingreds lii 
        where not exists (select * from ' + @targetdb + 'listmatching where listitemrecnum = lii.listitemrecnum) 
        ')
    
    exec ('
        insert ' + @targetdb + 'listmatching (listrecnum, cas, ingredient, userdefineddata1, userdefineddata2, userdefineddata3) 
        select 
            lmh.listrecnum, 
            mlm.cas, 
            mlm.ingredient, 
            mlm.userdefineddata1, 
            mlm.userdefineddata2, 
            mlm.userdefineddata3 
        from ' + @sourcedb + 'listmatching mlm 
        inner join ' + @sourcedb + 'listmatchingheader mlmh on mlm.listrecnum = mlmh.listrecnum and mlmh.listname = ''' + @listname + ''' 
        inner join ' + @targetdb + 'listmatchingheader lmh on mlmh.listname = lmh.listname 
        ')
    
    exec ('
        insert ' + @targetdb + 'listitemcass (listitemrecnum, cas, abstrue) 
        select 
            lm.listitemrecnum, 
            mlic.cas, 
            mlic.abstrue 
        from ' + @sourcedb + 'listitemcass mlic 
        inner join ' + @sourcedb + 'listmatching mlm on mlic.listitemrecnum = mlm.listitemrecnum 
        inner join ' + @sourcedb + 'listmatchingheader mlmh on mlm.listrecnum = mlmh.listrecnum and mlmh.listname = ''' + @listname + ''' 
        inner join ' + @targetdb + 'listmatchingheader lmh on mlmh.listname = lmh.listname 
        inner join ' + @targetdb + 'listmatching lm on lmh.listrecnum = lm.listrecnum and mlm.cas = lm.cas and mlm.ingredient = lm.ingredient and mlm.userdefineddata1 = lm.userdefineddata1 and mlm.userdefineddata2 = lm.userdefineddata2 and mlm.userdefineddata3 = lm.userdefineddata3 
        ')
    
    exec ('
        insert ' + @targetdb + 'listitemingreds (listitemrecnum, ingredienttext, abstrue) 
        select 
            lm.listitemrecnum, 
            mlii.ingredienttext, 
            mlii.abstrue 
        from ' + @sourcedb + 'listitemingreds mlii 
        inner join ' + @sourcedb + 'listmatching mlm on mlii.listitemrecnum = mlm.listitemrecnum 
        inner join ' + @sourcedb + 'listmatchingheader mlmh on mlm.listrecnum = mlmh.listrecnum and mlmh.listname = ''' + @listname + ''' 
        inner join ' + @targetdb + 'listmatchingheader lmh on mlmh.listname = lmh.listname 
        inner join ' + @targetdb + 'listmatching lm on lmh.listrecnum = lm.listrecnum and mlm.cas = lm.cas and mlm.ingredient = lm.ingredient and mlm.userdefineddata1 = lm.userdefineddata1 and mlm.userdefineddata2 = lm.userdefineddata2 and mlm.userdefineddata3 = lm.userdefineddata3')
    
    2 回复  |  直到 17 年前
        1
  •  0
  •   Raj More    17 年前

    请检查@sourcedb和@targetdb变量中是否正确写入了名称。

    要获取注册为链接服务器的名称列表,请使用以下代码

    sp_linkedservers
    

    Read more on the procedure

        2
  •  0
  •   Jesse    17 年前

    结果发现我做了一些愚蠢的事情,因为我实际上是从一个sql2000实例运行查询,而我的源数据库在一个sql2005实例上,显然这不太好。当我从2005实例运行查询时,它运行得很好。谢谢你的所有意见,很抱歉到处跑…

    推荐文章