views:

350

answers:

3

The following sql query works fine if I leave the four commented SET statements commented out.

However, if I uncomment them, or just say I uncomment the first one

SET @StoreID = tt_StoreID

then I get the following message

Invalid column name 'tt_StoreID'

Why doesn't it recognize it, and how do I fix it ?

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

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  A: 

You need to use

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

Also the loop that iterates over the cursor currently does nothing, but that is probably because you got stopped with the "getting data into variables" part.

Edit:
while the using the INTO @variable construct is "on the right track", the code snippet shown with only this change will fail since the cursor has more row columns declared than variables referenced in the INTO clause. The simple fix is to either add variables or remove columns in the cursor's SELECT list. It is hard to be more specific for the intent of the OP is not apparent in the code (empty loop, no action/usage based on the 4 variables shown...) or the question.

mjv
That won't work as his cursor has more fields in than just those four, which aren't the first four
CodeByMoonlight
You'll get this error : "Cursorfetch: The number of variables declared in the INTO list must match that of selected columns."
CodeByMoonlight
@CodeByMoonlight, You are right, see my edit. I'm unsure as to the intent of Laurie MC, the OP. Obviously he/she will need to only fetch the rows effectively needed (or to supply a dummy variable for rows "needed" for the query but not for the process, if there is such a case)
mjv
My guess is that the intent is going to involve updating @txnTable row by row in some way..I predict another question
CodeByMoonlight
@CodeBy, right again, LOL. Let me move out the way and do real work :-)
mjv
A: 
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

If you have a key for hte values and you don't need to process a loop then just do a straight query for it.

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

Middletone
how do you move from one row fetched to the next?
Raj More
now if he just needs the first row of values then he can just run the first statement for the cursor and close it as it looks like he just needs one row. If that's the fast then a direct query Select A into @A from T would sufice and be much simpler.
Middletone
A: 

First things first: Cursors are not scabale, and have a host of other problems. Move away from cursors as quickly as you can. You can do almost anything using SET based querying rather than cursors.

Having said / ranted that, here is how your cursor code should be changed:

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
Raj More
Same problem as mjv's solution
CodeByMoonlight
tx. changed to reflect comments.
Raj More