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

时间:2009-09-03 15:50:36

标签: sql sql-server sql-server-2005 tsql

我正在尝试使用链接服务器将一些插入从源数据库编写到目标数据库。我们第一次使用SQL的第二个命名实例,当我运行以下代码时,它给了我一个“内部SQL Server错误”。如果我删除命名实例周围的括号,我不再得到内部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 个答案:

答案 0 :(得分:0)

检查名称是否正确写入@SourceDB和@TargetDB变量。

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

sp_linkedservers

Read more on the procedure

答案 1 :(得分:0)

所以事实证明我做了一些愚蠢的事情,因为我实际上是从SQL2000实例运行查询,而我的源数据库是在SQL2005实例上,显然这并不好。当我从2005实例运行我的查询时,它运行得很好。感谢所有的投入,并为周围的事情感到抱歉...