我有一个在WHILE循环内运行的UPDATE语句。在WHILE循环的每次迭代中,在表中计算一些计数并采取措施。
我已经尝试过使用交叉连接,但无法正常工作。
declare @rowId int
declare @PrevRowId int
declare @Dataid int
declare @countexistIDs int
declare @countID int
Set @prevrowid=0
Set @rowId=0
Set @Dataid=0
SET @countexistIDs = 0
SET @countID = 0
SELECT
isnull(t2.inqty,0) + isnull(t2.comqty,0) AS qty
INTO #Temp
FROM tblitem t1,
tblin t2
WHERE t1.typeid = t2.typeid
AND t1.status IS NULL
SELECT top 1 @rowid = rowid, @Dataid = Dataid FROM #tempDataId
ORDER BY rowid ASC
WHILE @PrevRowId <> @rowid
BEGIN
SET @prevRowId = @rowId
SELECT @countID = COUNT(*) FROM tblitem
WHERE dataid = @dataid AND status IS NULL
SELECT @countexistIDs = COUNT(t2.typeid) FROM tblitem t2
INNER JOIN #Temp t3 ON t2.typeid = t3.typeid
WHERE t2.quantity <= t3.qty
IF @countexistPTIDs = @countID
BEGIN
UPDATE #tempDataId SET complete = 'yes' WHERE dataid = @Dataid
UPDATE t2
SET invqty = t2.qty - t1.quantity
FROM tblitem t1, #Temp t2
WHERE t1.producttypeid = t2.producttypeid
AND t1.status IS NULL
AND t1.WareHouseID = t2.warehouseid
AND t1.dataid = @Dataid
END
SELECT top 1 @rowid = rowid,@Dataid = dataid
FROM #tempDataId WHERE rowid > @PrevRowId ORDER BY rowid ASC
SET @countexistIDs = 0
SET @countID = 0
END
我正在尝试使用查询来执行此操作,而不是遍历每个DataId。