0
declare @company_branch_id  uniqueidentifier = 'd3534ff2-b2b2-473f-9be1-e03069bf5c87'
declare @dateFrom date = '10/8/2013'
declare @dateTo date = '11/9/2013'


DECLARE @tbl    TABLE(
    tbl_Ref varchar(50)
    , Reference varchar(100)
    , customer_branch_id uniqueidentifier
    , trans_date datetime
    , duedate datetime
    , Charge decimal(18,4)
    , Credits decimal(18,4)
    , Allocated decimal(18,4)
    , Outstanding decimal(18,4)
    , pay_amount decimal(18,4))

Insert into @tbl
Exec [dbo].[usp_getCustomerBalanceRpt] @company_branch_id, @dateFrom, @dateTo

UPDATE @tbl
SET trans_date = (SELECT tbl_Rental_InvoiceMaster.Date 
                  FROM tbl_Rental_InvoiceMaster
                  INNER JOIN @tbl ON @tbl.Reference = tbl_Rental_InvoiceMaster.Reference
                  WHERE tbl_Rental_InvoiceMaster.Reference= @tbl.Reference
                  AND @tbl.tbl_Ref='Rental Invoice')
SELECT * FROM @tbl

@company_branch_id、@dateFrom、@dateTo 是传递给 sp 的参数。执行上述代码时,附近显示ERROR

INNER JOIN @tbl ON @tbl.Reference = tbl_Rental_InvoiceMaster.Reference
WHERE tbl_Rental_InvoiceMaster.Reference= @tbl.Reference
AND @tbl.tbl_Ref='Rental Invoice'

作为'Must declare the scalar variable "@tbl".'

4

1 回答 1

1

当我们在连接中使用表变量时,我们需要给表起别名以便执行查询。

以下查询应该有效:

UPDATE @tbl
SET trans_date = (SELECT tbl_Rental_InvoiceMaster.Date 
                  FROM tbl_Rental_InvoiceMaster
                  INNER JOIN @tbl t ON t.Reference = tbl_Rental_InvoiceMaster.Reference
                  WHERE tbl_Rental_InvoiceMaster.Reference= t.Reference
                  AND t.tbl_Ref='Rental Invoice')
SELECT * FROM @tbl
于 2013-10-26T08:13:31.440 回答