以下动态sql语句出错

时间:2012-09-05 17:31:11

标签: tsql sybase

我遇到了一个错误,我在以下动态SQL语句中看不到:

  SET @sql_statement =
    "INSERT INTO " +
      @archive_db_name + ".dbo._TEST_TB_ACTIVE_ORDERS 
    SELECT " +
      @reporting_db_name + ".dbo._TEST_TB_ACTIVE_ORDERS.* 
    FROM " +
      @reporting_db_name + ".dbo._TEST_TB_ACTIVE_ORDERS 
    WHERE " +
      @reporting_db_name + ".dbo._TEST_TB_ACTIVE_ORDERS.ORDER_ID 
    IN
      (
        select #tmp_table_order_ids.order_id
        from #tmp_table_order_ids
      )"
  EXEC(@sql_statement)

错误是:

Msg 102, Level 15, State 1: Server 'server_name', Line 5: Incorrect syntax near 'IN'

但我在错误指定的位置看不到任何语法错误。有人可以请指出我为什么会收到这个错误吗?

2 个答案:

答案 0 :(得分:0)

尝试将双引号更改为单引号

  SET @sql_statement = 
    'INSERT INTO ' + 
      @archive_db_name + '.dbo._TEST_TB_ACTIVE_ORDERS  
    SELECT ' + 
      @reporting_db_name + '.dbo._TEST_TB_ACTIVE_ORDERS1.*  
    FROM ' + 
      @reporting_db_name + '.dbo._TEST_TB_ACTIVE_ORDERS1  
    WHERE ' + 
      @reporting_db_name + '.dbo._TEST_TB_ACTIVE_ORDERS1.ORDER_ID  
    IN 
      ( 
        select #tmp_table_order_ids.order_id 
        from #tmp_table_order_ids 
      )'
  EXEC(@sql_statement) 

答案 1 :(得分:0)

没有错误:

declare @reporting_db_name varchar(50)
declare @archive_db_name varchar(50)

declare @sql_statement varchar(500)

select @reporting_db_name ='PHILARIS_RD'

select @archive_db_name ='PHILARIS_RD'
SET @sql_statement =
    "INSERT INTO " +
      @archive_db_name + ".dbo.A 
    SELECT " +
      @reporting_db_name + ".dbo.A.* 
    FROM " +
      @reporting_db_name + ".dbo.A 
    WHERE " +
      @reporting_db_name + ".dbo.A.ID 
    IN
      (
        select B.ID
        from B
      )"
 EXEC(@sql_statement)