Delete FROM tbl_transfer
DECLARE @qtx_OriginalItemID int, @qtx_ProductCategoryID INT,@qtx_TimeStamp datetime
DECLARE MyCur CURSOR FOR Select
--qtx_TimeStamp,qtx_Responded
qtx_OriginalItemID,qtx_ProductCategoryID,qtx_TimeStamp
from
QuoteTransfer
left JOIN QuoteMaster on qm_QuoteMasterID = qtx_NewQuoteMasterID
left JOIN [QuoteTransferRegionDepartment] on [qtrd_QuoteTransferRegionDepartmentID]=qtx_DepartmentID
--WHERE qtx_OriginalItemID = '1' AND qtx_productcategoryid = '9'
ORDER BY qtx_OriginalItemID,qtx_productcategoryid
ASC
OPEN MyCur
FETCH NEXT FROM MyCur INTO @qtx_OriginalItemID,@qtx_ProductCategoryID,@qtx_TimeStamp
WHILE @@FETCH_Status =0
BEGIN
BEGIN TRAN
SELECT @qtx_OriginalItemID, @qtx_ProductCategoryID
IF EXISTS (SELECT 1 FROM tbl_transfer WHERE ItemID = @qtx_OriginalItemID
AND CategoryID = @qtx_ProductCategoryID)
BEGIN
DELETE FROM tbl_transfer WHERE itemid = @qtx_OriginalItemID
and CategoryID = @qtx_ProductCategoryID
AND qtx_TimeStamp = @qtx_TimeStamp
SELECT @qtx_OriginalItemID,@qtx_ProductCategoryID
END
Else
begin
INSERT INTO tbl_transfer(qtx_TimeStamp,qtx_Responded,Itemid,categoryid)
SELECT qtx_TimeStamp,qtx_Responded,qtx_OriginalItemID,
qtx_ProductCategoryID
from
QuoteTransfer
left JOIN QuoteMaster on qm_QuoteMasterID = qtx_NewQuoteMasterID
left JOIN [QuoteTransferRegionDepartment] on [qtrd_QuoteTransferRegionDepartmentID]=qtx_DepartmentID
where qtx_OriginalItemID= @qtx_OriginalItemID
AND qtx_ProductCategoryID = @qtx_ProductCategoryID
AND qtx_TimeStamp = @qtx_TimeStamp
end
FETCH NEXT FROM MyCur INTO @qtx_OriginalItemID,@qtx_ProductCategoryID,@qtx_TimeStamp
END
CLOSE MyCur
DEALLOCATE MyCur
SELECT * FROM tbl_transfer
DECLARE @qtx_OriginalItemID int, @qtx_ProductCategoryID INT,@qtx_TimeStamp datetime
DECLARE MyCur CURSOR FOR Select
--qtx_TimeStamp,qtx_Responded
qtx_OriginalItemID,qtx_ProductCategoryID,qtx_TimeStamp
from
QuoteTransfer
left JOIN QuoteMaster on qm_QuoteMasterID = qtx_NewQuoteMasterID
left JOIN [QuoteTransferRegionDepartment] on [qtrd_QuoteTransferRegionDepartmentID]=qtx_DepartmentID
--WHERE qtx_OriginalItemID = '1' AND qtx_productcategoryid = '9'
ORDER BY qtx_OriginalItemID,qtx_productcategoryid
ASC
OPEN MyCur
FETCH NEXT FROM MyCur INTO @qtx_OriginalItemID,@qtx_ProductCategoryID,@qtx_TimeStamp
WHILE @@FETCH_Status =0
BEGIN
BEGIN TRAN
SELECT @qtx_OriginalItemID, @qtx_ProductCategoryID
IF EXISTS (SELECT 1 FROM tbl_transfer WHERE ItemID = @qtx_OriginalItemID
AND CategoryID = @qtx_ProductCategoryID)
BEGIN
DELETE FROM tbl_transfer WHERE itemid = @qtx_OriginalItemID
and CategoryID = @qtx_ProductCategoryID
AND qtx_TimeStamp = @qtx_TimeStamp
SELECT @qtx_OriginalItemID,@qtx_ProductCategoryID
END
Else
begin
INSERT INTO tbl_transfer(qtx_TimeStamp,qtx_Responded,Itemid,categoryid)
SELECT qtx_TimeStamp,qtx_Responded,qtx_OriginalItemID,
qtx_ProductCategoryID
from
QuoteTransfer
left JOIN QuoteMaster on qm_QuoteMasterID = qtx_NewQuoteMasterID
left JOIN [QuoteTransferRegionDepartment] on [qtrd_QuoteTransferRegionDepartmentID]=qtx_DepartmentID
where qtx_OriginalItemID= @qtx_OriginalItemID
AND qtx_ProductCategoryID = @qtx_ProductCategoryID
AND qtx_TimeStamp = @qtx_TimeStamp
end
FETCH NEXT FROM MyCur INTO @qtx_OriginalItemID,@qtx_ProductCategoryID,@qtx_TimeStamp
END
CLOSE MyCur
DEALLOCATE MyCur
SELECT * FROM tbl_transfer
No comments:
Post a Comment