SQL Query'无效列名'

时间:2009-10-30 13:59:27

标签: sql sql-server tsql

如果我将四个已注释的SET语句注释掉,则以下sql查询可以正常工作。

但是,如果我取消注释,或者只是说我取消注释第一个

SET @StoreID = tt_StoreID

然后我收到以下消息

  

无效的列名称'tt_StoreID'

为什么不识别它,我该如何解决?

*---------------------------------

DECLARE @txnTable table (
tt_StoreID int,
tt_StoreName nvarchar (50),
tt_BatchNumber int,
tt_OpeningTime datetime,
tt_ClosingTime datetime,
tt_TransactionNumber int,
tt_Price money,
tt_Quantity float,
tt_TenderID int,
tt_TenderDesc nvarchar (25),
tt_TEID int,
tt_ItemID int,
tt_ItemLookupCode nvarchar (25),
tt_ItemDesc nvarchar (30)
)

INSERT @txnTable

SELECT Distinct dbo.Batch.StoreID, 
dbo.Store.Name, 
dbo.Batch.BatchNumber, 
dbo.Batch.OpeningTime, 
dbo.Batch.ClosingTime, 
dbo.TransactionEntry.TransactionNumber, 
dbo.TransactionEntry.Price, 
dbo.TransactionEntry.Quantity,
dbo.view_TenderEntry_Distinct_TenderID.TenderID,
dbo.view_TenderEntry_Distinct_TenderID.Description 
AS TenderDesc, 
dbo.TransactionEntry.ID AS TEID, 
dbo.Item.ID, 
dbo.Item.ItemLookupCode, 
dbo.Item.Description

FROM dbo.Store 
INNER JOIN
dbo.Batch ON dbo.Store.ID = dbo.Batch.StoreID 
INNER JOIN
dbo.[Transaction]
ON dbo.[Transaction].BatchNumber = Batch.BatchNumber
AND dbo.[Transaction].StoreID = Batch.StoreID
INNER JOIN
dbo.TransactionEntry 
ON dbo.[Transaction].StoreID = dbo.TransactionEntry.StoreID 
AND dbo.[Transaction].TransactionNumber = dbo.TransactionEntry.TransactionNumber 
INNER JOIN
dbo.view_TenderEntry_Distinct_TenderID 
ON dbo.Batch.StoreID = dbo.view_TenderEntry_Distinct_TenderID.StoreID 
AND dbo.Batch.BatchNumber = dbo.view_TenderEntry_Distinct_TenderID.BatchNumber 
AND dbo.TransactionEntry.TransactionNumber = dbo.view_TenderEntry_Distinct_TenderID.TransactionNumber
INNER JOIN
dbo.Item ON dbo.TransactionEntry.ItemID = dbo.Item.ID

WHERE Batch.BatchNumber = 28613

ORDER BY Batch.StoreID, 
Batch.BatchNumber, 
TransactionEntry.TransactionNumber, 
Item.Description

--

DECLARE @StoreID int
DECLARE @BatchNumber int
DECLARE @TransactionNo int
DECLARE @ItemDesc nvarchar (30)

DECLARE ttCursor CURSOR FOR
SELECT tt_StoreID,
tt_StoreName,
tt_BatchNumber,
tt_OpeningTime,
tt_ClosingTime,
tt_TransactionNumber,
tt_Price,
tt_Quantity,
tt_TenderID,
tt_TenderDesc,
tt_TEID,
tt_ItemID,
tt_ItemLookupCode,
tt_ItemDesc
FROM @txnTable
OPEN ttCursor

FETCH NEXT from ttCursor

-- The four lines below are where the errors occur

-- SET @StoreID = tt_StoreID
-- SET @BatchNumber = tt_BatchNumber
-- SET @TransactionNo = tt_TransactionNumber
-- SET @ItemDesc = tt_ItemDesc

WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT from ttCursor
END
CLOSE ttCursor
DEALLOCATE ttCursor

SELECT * FROM @txnTable 

3 个答案:

答案 0 :(得分:4)

您需要使用

FETCH NEXT FROM ttCursor INTO @StoreID, @BatchNumber, @TransactionNo, @ItemDesc

此外,迭代光标的循环当前没有任何作用,但这可能是因为您已经停止了“将数据转换为变量”部分。

修改
虽然使用INTO @variable构造是“在正确的轨道上”,但只显示此更改的代码段将失败,因为游标声明的行列数多于INTO子句中引用的变量。简单的解决方法是在游标的SELECT列表中添加变量或删除列。很难更具体地说OP的意图在代码中不明显(空循环,没有基于显示的4个变量的动作/用法......)或问题。

答案 1 :(得分:0)

Set @RowA = Cursor For
SELECT tt_StoreID,
tt_BatchNumber,
tt_TransactionNumber
tt_ItemDesc
FROM @txnTable

Open @RowA
Fetch Next From @RowA
Into @StoreID, @BatchNumber, @TransactionNo, @ItemDesc
    While (@@Fetch_Status=0)
        begin
        [do work]

            Fetch Next From @RowA
            Into @StoreID, @BatchNumber, @TransactionNo, @ItemDesc
        end
    Close @RowA

如果你有一个关于hte值的键,而你不需要处理循环,那么就直接查询它。

SELECT @ StoreID = tt_StoreID,@ BatchNumber = tt_BatchNumber,@ TransactionNo = @ tt_TransactionNumber,@ ItemDesc = tt_ItemDesc     FROM @txnTable,其中[yourkey] = @ Key

答案 2 :(得分:0)

首先要做的事:光标不是scabale,还有许多其他问题。尽可能快地离开游标。你可以使用基于SET的查询而不是游标来做几乎任何事情。

说完/说了这个,这就是你的游标代码应该如何改变:

FETCH NEXT from ttCursor INTO @tt_StoreID, @tt_StoreName, @tt_BatchNumber, @tt_OpeningTime, @tt_ClosingTime,
    @tt_TransactionNumber, @tt_Price, @tt_Quantity, @tt_TenderID, @tt_TenderDesc,
    @tt_TEID, @tt_ItemID, @tt_ItemLookupCode, @tt_ItemDesc


WHILE (@@FETCH_STATUS = 0)
BEGIN

    SET @StoreID = @tt_StoreID
    SET @BatchNumber = @tt_BatchNumber
    SET @TransactionNo = @tt_TransactionNumber
    SET @ItemDesc = @tt_ItemDesc

    /* YOUR QUERY GOES HERE */

    FETCH NEXT from ttCursor INTO @tt_StoreID, @tt_StoreName, @tt_BatchNumber, @tt_OpeningTime, @tt_ClosingTime,
        @tt_TransactionNumber, @tt_Price, @tt_Quantity, @tt_TenderID, @tt_TenderDesc,
        @tt_TEID, @tt_ItemID, @tt_ItemLookupCode, @tt_ItemDesc
END
CLOSE ttCursor
DEALLOCATE ttCursor