sqlException(0x80131904)名称' ...'不是有效的标识符

时间:2014-04-22 16:59:25

标签: sql sql-server-2008 stored-procedures

我有一个基于它需要访问的数据库的动态存储过程。从程序运行该过程会给我一个错误:

  

System.Data.SqlClient.SqlException(0x80131904):名称“...”不是有效的标识符.....

当我从SSMS运行存储过程时,我得到相同的错误,但是如果我打印而不是exec,那么手动运行代码,它完全正常。

declare @sql as varchar(8000)
if @Provider=''
    set @sql = '
    update  ' + @LinkServer + '.dbo.[billing header] set [Bill Primary]=1 where Billing in(
    select Distinct Billing from  ' + @LinkServer + '.dbo.[History Detail] 
    where ' +  @whereDateFacility  + ' and [insurance code] = rtrim( '''+ @PrimaryCode +'''));
select @@rowcount as rc;'
else
    set @sql = '
    update  ' + @LinkServer + '.dbo.[billing header] set [Bill Primary]=1 where Billing in(
    select Distinct hd.Billing from  ' + @LinkServer + '.dbo.[History Detail] hd
        inner join ' + @LinkServer + '.dbo.[billing header] bh on bh.billing=hd.billing
    where provider=''' + @Provider + ''' and ' +  @whereDateFacility  + ' and [insurance code] = rtrim( '''+ @PrimaryCode +'''));
select @@rowcount as rc;'
End

insert into dbo.[log] ([desc]) values(@sql)

--print @sql
exec @sql

从日志中提取代码片段也可以正常工作。 以下是日志中的最终代码,如果手动运行,则可以正常工作。

update [targetDb].dbo.[billing header] 
set [Bill Primary] = 1 
where Billing in (select Distinct Billing 
                  from [targetDb].dbo.[History Detail] 
                  where [Date printed] = '04/22/2014' 
                    and [insurance code] = rtrim( 'med001'));
select @@rowcount as rc;

有什么想法可以解决这个问题?

谢谢, 戴夫K。

0 个答案:

没有答案