如何解决必须声明标量变量“@variable”

时间:2017-11-08 19:19:56

标签: sql sql-server database

关于此错误有很多问题,但我无法弄清楚为什么我仍然会收到此错误。我已经声明了变量,但它仍在抱怨Must declare the scalar variable "@tranId".;

这是我的SP。

ALTER PROCEDURE "ESG"."ADD_TEST_DATA"
AS
BEGIN

DECLARE @envelopeId table(identifiers VARCHAR(1000));

INSERT INTO @envelopeId (identifiers) VALUES('axaa1aaa-aaaa-a5aa-aaaa-aa8aaaa9aaaa');
INSERT INTO @envelopeId (identifiers) VALUES('bxbb1bbb-bbbb-b5bb-bbb4-bb8bbbb9bbbf');

DECLARE @tranId table(ids INT);

INSERT INTO @tranId SELECT DOCUMENT_SET_TRANSACTION_ID FROM ESG.DOCUMENT_SET_TRANSACTION WHERE IDENTIFIER IN (SELECT identifiers FROM @envelopeId); 
DELETE FROM ESG.DOCUMENT_SET_TRANS_MDATA WHERE DOCUMENT_SET_TRANS_MDATA.DOCUMENT_SET_TRANSACTION_ID IN (@tranId);

END

已经声明了@tranId变量然后它正在抱怨什么?

2 个答案:

答案 0 :(得分:2)

您已将@tranId声明为表格。在删除语句中,您没有从表中选择列(即.. original.set_index('position')['news_results'].apply(pd.Series).stack().apply(pd.Series).reset_index() Out[1633]: position level_1 title url 0 1 1 Worst case https://www.politico.com/magazine 1 1 2 Bad Night https://www.nbcnews.com/pol 2 1 3 On the anniversary https://www.usatoday.com/stor 3 2 1 Bad Things Happ https://www.nytimes.com/maga 4 2 2 Best Night https://www.cnn.com/pols )。

答案 1 :(得分:2)

为什么所有表变量都在这里?您可以大大简化此操作,只需删除即可。

ALTER PROCEDURE ESG.ADD_TEST_DATA
AS
BEGIN

    DELETE FROM ESG.DOCUMENT_SET_TRANS_MDATA 
    WHERE DOCUMENT_SET_TRANS_MDATA.DOCUMENT_SET_TRANSACTION_ID IN 
    (
        SELECT DOCUMENT_SET_TRANSACTION_ID 
        FROM ESG.DOCUMENT_SET_TRANSACTION 
        WHERE IDENTIFIER IN
        (
            'axaa1aaa-aaaa-a5aa-aaaa-aa8aaaa9aaaa'
            , 'bxbb1bbb-bbbb-b5bb-bbb4-bb8bbbb9bbbf'
        )
    );

END