troubleshooting Question

SQL - Undeclared scalar table variable

Avatar of Zack
ZackFlag for Australia asked on
SQLMicrosoft SQL Server
6 Comments1 Solution12 ViewsLast Modified:
Hi EE,

I am having issues with this table variable 

declare @DifferenceTable  table (
        DataAreaId nvarchar(10) ,
        SalesId nvarchar(50) ,
        InvoiceId nvarchar(50) ,
        ItemId nvarchar(50),
        InvoiceQty decimal(16,4) ,
        InventoryQty decimal(16,4)
    )




insert into @DifferenceTable (DataAreaId , SalesId , InvoiceId , ItemId , InvoiceQty ,  InventoryQty )
    select CustInvoiceTrans.DATAAREAID , CustInvoiceTrans.SALESID , CustInvoiceTrans.INVOICEID , CustInvoiceTrans.ITEMID , sum(CustInvoiceTrans.QTY) , 0. as InventoryQty
    from CustInvoiceTrans with(nolock)    
    group by CustInvoiceTrans.DATAAREAID , CustInvoiceTrans.SALESID , CustInvoiceTrans.INVOICEID , CustInvoiceTrans.ITEMID


update @DifferenceTable
    set InventoryQty =  tt.Qty from
                                           (select sum(InventTrans.QTY)*-1 as Qty , InventTransOrigin.DATAAREAID , REFERENCEID , InventTrans.INVOICEID , InventTrans.ITEMID
                                            from InventTransOrigin
                                             left join InventTrans      on InventTrans.INVENTTRANSORIGIN = InventTransOrigin.RECID
                                                                                    and InventTrans.DATAAREAID = InventTransOrigin.DATAAREAID
                                                                                    and InventTrans.ITEMID = InventTransOrigin.ITEMID
                                             where REFERENCECATEGORY = 0
                                             group by InventTransOrigin.DATAAREAID , REFERENCEID , InventTrans.INVOICEID , InventTrans.ITEMID
                                            ) tt
                                    where  @DifferenceTable.DataAreaId =  tt.DATAAREAID and @DifferenceTable.SalesId = tt.REFERENCEID
                                    and tt.INVOICEID = @DifferenceTable.InvoiceId and tt.ITEMID = @DifferenceTable.ItemId  


select * from @DifferenceTable
The @differencetable isn't recognised as declared, how would I resolve this?

Any assistance is welcome. 

Thank you

ASKER CERTIFIED SOLUTION
Dustin Saunders
Co-Founder and Chief Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2016

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros