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 に渡される引数です。上記のコードを実行すると、エラーが近くに表示されます

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 に答える