基于表变量的T-SQL删除命令

时间:2010-08-19 07:05:12

标签: tsql delete-row

我需要从表中删除某些行,其中索引是表变量

中的相等索引
  declare @m_table as table
  (
     number NUMERIC(18,0)
  )
...
inserting some rows into @m_table
...
DELETE ct FROM [dbo].[customer_task] ct
          inner join project_customer pc on pc.id_customer = @m_table.number
          inner join customer_user cu on cu.id_project_customer = pc.id
WHERE ct.id_csr_user = cu.id AND ct.id_status = 1;

但是此代码会生成错误:必须声明标量变量“@m_table”如何解决?

2 个答案:

答案 0 :(得分:2)

你可能在那些'...'

中有一个'GO'(批处理分隔符)

变量声明不会跨越批次。

答案 1 :(得分:1)

错误意味着SQL希望您将@m_table视为标准表,而不是标量(int,bit等)变量。也许这样的事情会起作用吗?

DELETE ct FROM [dbo].[customer_task] ct
WHERE ct.id_csr_user IN (
    SELECT cu.id FROM customer_user cu
    INNER JOIN project_customer pc ON pc.id = cu.id_project_customer
    WHERE pc.id_customer IN (SELECT number FROM @m_table.number)
) AND ct.id_status = 1;