如何在存储过程中使用每个?

时间:2015-06-10 05:08:54

标签: sql sql-server foreach

在下面的代码中我使用了单@partyID=750004,但现在该方案需要使用更多@partyID。仅供参考,如750005,750007,750009,7500011 ......

如何使用“For each”循环

BEGIN TRY
 BEGIN TRAN
 Declare @partyID bigint = 750004
  IF NOT EXISTS (SELECT * FROM [QAdmin].[PartyLicenseInfo] WHERE PartyId = @partyID AND [State]='SC')
    BEGIN
        INSERT INTO QAdmin.PartyLicenseInfo (PartyId, [State], LicenseNumber, LicenseExpirationDate, CreatedBy, CreatedDate, ModifiedBy, ModifiedDate) 
        VALUES (@partyID, 'SC', 'SC1234', getdate()+360, 0, GETDATE(), null, null)
    END
  ELSE
    BEGIN
        UPDATE [QAdmin].[PartyLicenseInfo] SET LicenseExpirationDate = getdate()+360, ModifiedBy = 0, ModifiedDate = GETDATE() 
                           WHERE PartyId = @partyID AND [State]='SC'
  END
 COMMIT TRAN
END TRY
BEGIN CATCH
    ROLLBACK TRAN
END CATCH

3 个答案:

答案 0 :(得分:1)

DECLARE cur CURSOR FOR 

SELECT partyid from table1    // @partyID like 750005,750007,750009,7500011

FETCH NEXT FROM cur INTO @partyID

WHILE (@@FETCH_STATUS=0)
BEGIN

    IF NOT EXISTS  (SELECT * FROM [QAdmin].[PartyLicenseInfo] WHERE PartyId = @partyID AND [State]='SC')
         BEGIN
               INSERT INTO QAdmin.PartyLicenseInfo (PartyId, [State], LicenseNumber, LicenseExpirationDate, CreatedBy, CreatedDate, ModifiedBy, ModifiedDate) 
               VALUES (@partyID, 'SC', 'SC1234', getdate()+360, 0, GETDATE(), null, null)
         END
    ELSE
         BEGIN
               UPDATE [QAdmin].[PartyLicenseInfo] SET LicenseExpirationDate = getdate()+360, ModifiedBy = 0, ModifiedDate = GETDATE() 
               WHERE PartyId = @partyID AND [State]='SC'
         END

FETCH NEXT FROM cur INTO @partyID

END

CLOSE cur
DEALLOCATE cur

答案 1 :(得分:0)

您似乎想要使用CURSOR。虽然大多数时候使用基于集合的解决方案是最好的,但有时候CURSOR是最好的解决方案。如果不了解您的真实问题,我们无法为您提供更多帮助:

DECLARE @PractitionerId int

DECLARE MY_CURSOR CURSOR 
  LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR 
SELECT DISTINCT PractitionerId 
FROM Practitioner

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @PractitionerId
WHILE @@FETCH_STATUS = 0
BEGIN 
    --Do something with Id here
    PRINT @PractitionerId
    FETCH NEXT FROM MY_CURSOR INTO @PractitionerId
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

答案 2 :(得分:0)

最好的选择是摆脱游标并使用基于集合的方法。

首先,声明一个表变量来存储所有ID:

DECLARE @partyID TABLE (partyID int)

INSERT INTO @partyID VALUES 
(750005),(750007),(750009),(7500011)

然后使用此表变量执行INSERT INTO SELECTUPDATE操作:

INSERT INTO QAdmin.PartyLicenseInfo (PartyId, [State], LicenseNumber, 
            LicenseExpirationDate, CreatedBy, CreatedDate, ModifiedBy,
            ModifiedDate) 
SELECT partyID, 'SC', 'SC1234', getdate()+360, 0, GETDATE(), null, null 
FROM @partyID AS p1
WHERE NOT EXISTS (SELECT 1
                  FROM [QAdmin].[PartyLicenseInfo] AS p2
                  WHERE p2.PartyId = p1.partyID AND [State]='SC')

如果您的原始PartyLicenseInfo子句返回NOT EXISTS,则上述内容会仅将新记录插入TRUE

UPDATE p1
SET 
   LicenseExpirationDate = getdate()+360, ModifiedBy = 0, 
   ModifiedDate = GETDATE() 
FROM [QAdmin].[PartyLicenseInfo] AS p1
INNER JOIN @partyID AS p2 ON p1.PartyID = p2.partyID
WHERE [State]='SC'

以上内容将更新PartyLicenseInfo PartyID中包含@partyID[State]='SC'的记录。