[英]SQL LOOP Pass values from Temp Table as parameters to stored procedure
[英]Updating values of a temp table based on values from another Stored Procedure
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的參數。 執行上述代碼時,錯誤顯示在
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".'
當我們在聯接中使用表變量時,我們需要為表加上別名以執行查詢。
以下查詢應該工作:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.