下面有一个存储过程。它是一个嵌套循环。而不是使用FETCH_STATUS我想使用其他东西来标记我的光标完成,以便我可以做一个嵌套循环。 但它永远不会在光标中拾取@Client_ID,我不知道为什么。
ALTER PROCEDURE [dbo].[Import_Agent_Client_Bucket_2010]
AS
BEGIN
-- Loop Through Each Agent, Create a Bucket, Add their Clients to the Bucket
DECLARE Agent_Cursor CURSOR FOR
SELECT TOP 10 Agent_GUID, Agent_ID
FROM realforms_2011.dbo.Agent
DECLARE @Agent_GUID uniqueidentifier
DECLARE @Agent_ID int
OPEN Agent_Cursor;
FETCH NEXT FROM Agent_Cursor
INTO @Agent_GUID, @Agent_ID;
DECLARE @MaxAgentCount int
SELECT @MaxAgentCount = COUNT(Agent_ID)
FROM Agent
DECLARE @AgentCounter int
SET @AgentCounter = 0
WHILE (1 < 2)
BEGIN
PRINT 'Agent Counter:'
PRINT @AgentCounter
SET @AgentCounter = @AgentCounter + 1
-- Create a bucket for each agent
DECLARE @cbPKTable TABLE (cbPK UNIQUEIDENTIFIER, cbID int)
INSERT INTO realforms_2011.dbo.Client_Bucket ([Description] ) OUTPUT inserted.Client_Bucket_GUID, inserted.Client_Bucket_ID INTO @cbPKTable
SELECT ISNULL(a.First_Name, ' ') + ' ' + ISNULL(a.Last_Name, ' ') + '''s Clients'
FROM realforms_2011.dbo.Agent a
WHERE Agent_GUID = @Agent_GUID
DECLARE @Client_Bucket_GUID uniqueidentifier
SELECT @Client_Bucket_GUID = cbPK FROM @cbPKTable
PRINT 'Client Bucket GUID Inserted:'
PRINT @Client_Bucket_GUID
DECLARE @Client_Bucket_ID int
SELECT @Client_Bucket_ID = cbID FROM @cbPKTable
INSERT INTO realforms_2011.dbo.Agent_Client_Bucket (Agent_GUID, Agent_ID, Client_Bucket_GUID, Client_Bucket_ID)
VALUES (@Agent_GUID, @Agent_ID, @Client_Bucket_GUID, @Client_Bucket_ID)
DECLARE @Client_GUID uniqueidentifier
DECLARE @Client_ID int
-- Get clients from the server (2010)
DECLARE Client_Cursor CURSOR FOR
SELECT C.Client_ID
FROM realforms.dbo.Client C
INNER JOIN realforms.dbo.Agent_Client AC ON AC.Client_ID = C.Client_ID
WHERE AC.Agent_ID = @Agent_ID
ORDER BY C.Client_ID ASC
DECLARE @MaxClientCreateDate datetime
SELECT @MaxClientCreateDate = MAX(Create_Date)
FROM realforms.dbo.Client C
PRINT 'Max Create Date:'
PRINT @MaxClientCreateDate
DECLARE @ClientCreateDate datetime
SET @ClientCreateDate = CAST('01-01-2000' AS datetime)
OPEN Client_Cursor;
FETCH NEXT FROM Client_Cursor
INTO @Client_ID
-- loop through each 2010 client
WHILE (1 < 2)
BEGIN
DECLARE @myNewPKTable TABLE (myNewPK UNIQUEIDENTIFIER)
BEGIN TRY
PRINT 'Client ID:'
PRINT @Client_ID
PRINT 'Agent ID:'
PRINT @Agent_ID
INSERT INTO realforms_2011.dbo.Client (Client_ID,Name,Secondary_Name,[Address],Address_2,City_State_Zip,Phone,Email_Address,Secondary_Email_Address,Create_Date,Last_Change_Date,[Status],File_Under,[Year],IsFavorite) OUTPUT inserted.Client_GUID INTO @myNewPKTable
SELECT c.Client_ID,Name,Secondary_Name,[Address],Address_2,City_State_Zip,Phone,Email_Address,Secondary_Email_Address,Create_Date,Last_Change_Date,[Status],File_Under,2010,0
FROM realforms.dbo.Client C
INNER JOIN realforms.dbo.Agent_Client AC ON AC.Client_ID = C.Client_ID
WHERE AC.Agent_ID = @Agent_ID AND C.Client_ID = @Client_ID
SELECT @Client_GUID = myNewPK FROM @myNewPKTable
PRINT 'Client GUID Inserted: '
PRINT @Client_GUID
INSERT INTO realforms_2011.dbo.Client_Bucket_Client (Client_Bucket_GUID, Client_GUID, Client_ID, Client_Bucket_ID, [Year])
VALUES (@Client_Bucket_GUID, @Client_GUID, @Client_ID, @Client_Bucket_ID, 2010)
PRINT 'Client Bucket GUID: '
PRINT @Client_Bucket_GUID
PRINT 'Client ID Inserted: '
PRINT @Client_ID
SELECT @ClientCreateDate = CAST(Create_Date as datetime)
FROM realforms.dbo.Client C
INNER JOIN realforms.dbo.Agent_Client AC ON AC.Client_ID = C.Client_ID
WHERE AC.Agent_ID = @Agent_ID AND C.Client_ID = @Client_ID
PRINT @ClientCreateDate
END TRY
BEGIN CATCH
PRINT 'Error:'
PRINT ERROR_MESSAGE()
PRINT 'Last Client GUID Inserted: '
PRINT @Client_GUID
PRINT 'Last Client ID: '
PRINT @Client_ID
END CATCH
IF @@ERROR != 0 GOTO ERR_HANDLER
IF (@ClientCreateDate >= @MaxClientCreateDate)
BREAK
FETCH NEXT FROM Client_Cursor
INTO @Client_ID;
END;
CLOSE Client_Cursor;
DEALLOCATE Client_Cursor;
IF (@AgentCounter >= @MaxAgentCount)
BREAK
FETCH NEXT FROM Agent_Cursor
INTO @Agent_GUID, @Agent_ID;
END;
CLOSE Agent_Cursor;
DEALLOCATE Agent_Cursor;
ERR_HANDLER: PRINT 'ERROR'
END
它打破了@Client_ID
答案 0 :(得分:3)
您可以嵌套fetch_status,因为每个循环的最后一个语句都是fetch next。