我正在 SQL Server 2012 上开发这个存储过程。
存储过程将更新Quantity
行中EXTERNAL_CODES
表中的每一行@newBatches
范围。这就像一个循环,我需要在其中创建一个新行BATCHES
表中的每一行@newBatches
范围。
然后,我必须更新Quantity
行中EXTERNAL_CODES
表与每个batchId
创建的。
CREATE PROCEDURE [dbo].[CreateBatchAndKeepExternalCodes]
@newBatches as dbo.CreateBatchList READONLY,
@productId int
AS
set nocount on;
declare @lowestCodeLevel tinyint;
-- ======== VALIDATION ==========
if ((select count(name) from @newBatches) = 0)
return -112;
-- ====== CODE ========
-- Get lowest aggregation level.
set @lowestCodeLevel =
(select min(c.application_code)
from CHINA_CODES_HEADER c, PRODUCTS p
where p.Id = @productId and c.DRUG_TEN_SEATS = p.PRODUCT_CODE);
begin transaction;
insert into BATCHES (PRODUCT_ID, NAME, CREATED)
select @productId, Name, CAST(SYSDATETIMEOFFSET() as nvarchar(50))
from @newBatches;
update top(t.Quantity) EXTERNAL_CODES
set BATCH_ID = (select ID from BATCHES where NAME = t.Name)
, USED = 1
from (select Name, Quantity from @newBatches) t
where PRODUCT_ID = @productId and CODE_LEVEL = @lowestCodeLevel;
commit transaction;
RETURN 0
我收到一个错误update
:
update top(t.Quantity) EXTERNAL_CODES
set BATCH_ID = (select ID from BATCHES where NAME = t.Name)
, USED = 1
from (select Name, Quantity from @newBatches) t
where PRODUCT_ID = @productId and CODE_LEVEL = @lowestCodeLevel;
错误在这里:update top(t.Quantity)
。它找不到t.Quantity
.
dbo.CreateBatchList
is:
CREATE TYPE [dbo].[CreateBatchList] AS TABLE
(
Name nVARCHAR(20),
Quantity int
)
我的问题是我无法设置更新Quantity
行。任何想法?
错误(或警告)消息是:
SQL71005:无法解析对列 t.Quantity 的引用。
也许我可以用MERGE
.