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".'
答案 0 :(得分: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