2017-03-11 2 views
1

Ich habe einen Cursor in Informix-Datenbank, die Teil einer Funktion ist, möchte ich das in einen Cursor in MS-SQL konvertieren.Konvertieren von Informix Cursor zu MSSQL Cursor

Unten ist der Code:

DECLARE select distinct agentname, agentloginid 
      from selected_agents 

      call; 
    OPEN cur; 
    FETCH cur INTO @l_AgentName, @l_AgentLoginID; 
    WHILE @@FETCH_STATUS = 0 BEGIN getAgentLogActivity(@l_AgentName, @l_AgentLoginID, @p_startTime, @p_endTime); 
      insert into final_result(Agent_Name, Agent_Login_ID, op1, Login_Time, op2, 
          Logout_Time, Logout_Reason_Code, Logon_Duration) 
      select @l_AgentName, @l_AgentLoginID, op1, logintime, op2, logouttime, reasoncode, duration 
       from temp_login_logout; 
    FETCH cur INTO @l_AgentName, @l_AgentLoginID; 
    end 
    CLOSE cur; 
    DEALLOCATE cur; 

Hier in diesem Fall, dass ich Probleme mit Declare-Anweisung zu finden bin. Ich weiß, dass ich das auf dem Weg schreiben muss.

DECLARE cur CURSOR FOR 
    SELECT DISTINCT agentname, agentloginid 
      from selected_agents 
    OPEN cur; 
    FETCH cur INTO @l_AgentName, @l_AgentLoginID; 
    WHILE @@FETCH_STATUS = 0 BEGIN getAgentLogActivity(@l_AgentName, @l_AgentLoginID, @p_startTime, @p_endTime); 
      insert into final_result(Agent_Name, Agent_Login_ID, op1, Login_Time, op2, 
          Logout_Time, Logout_Reason_Code, Logon_Duration) 
      select @l_AgentName, @l_AgentLoginID, op1, logintime, op2, logouttime, reasoncode, duration 
       from temp_login_logout; 
    FETCH cur INTO @l_AgentName, @l_AgentLoginID; 
    end 
    CLOSE cur; 
    DEALLOCATE cur; 

AKTUALISIERT - selected_agents Erklärung

DECLARE @selected_agents TABLE (
     agentloginid NVARCHAR(50), 
     agentname NVARCHAR(50), 
     agentID INT, 
     profileid INT, 
     resourcegroupid int, 
     dateinactive datetime, 
     --filter boolean default 'f', 
     rsmid INT, 
     teamid INT 
    ); 

Hier selected_agents ist eine temporäre Tabelle, die ich in der Funktion erstellt haben.

Der Fehler, was es heißt, dass Agentname, Agentloginid nicht deklariert ist. Was ich bereits erklärt habe.

Kann mir jemand helfen, wie kann ich es korrigieren?

+0

Sie müssen nächste holen, aber wo haben Sie diese Variablen deklariert? – scsimon

+0

Ich habe vor dieser Cursor-Deklaration eine temporäre Tabelle erstellt. Diese Cursor und Temp-Tabelle ist Teil einer Funktion, die ich habe. –

+0

Den Code posten? Temp-Tabelle ist nicht dasselbe wie Variablen. Vielen Dank! – scsimon

Antwort

1

Dies ist vielleicht nicht 100% eine Lösung, aber es hat viele Kommentare, um loszulegen. Fühlen Sie sich frei zu kommentieren, um mich wissen zu lassen, wo Sie immer noch Probleme haben.

DECLARE @selected_agents TABLE (
     agentloginid NVARCHAR(50), 
     agentname NVARCHAR(50), 
     agentID INT, 
     profileid INT, 
     resourcegroupid int, 
     dateinactive datetime, 
     --filter boolean default 'f', 
     rsmid INT, 
     teamid INT 
    ); 


/* 
Do something here to insert data into @selected_agents 
*/ 

DECLARE @l_AgentName NVARCHAR(50), @l_AgentLoginID NVARCHAR(50); 
DECLARE @l_AgentExtension NVARCHAR(50); 
DECLARE @l_op1 NVARCHAR(1), @l_op2 NVARCHAR(1); 
DECLARE @l_LoginTime DATETIME2(3), @l_LogoutTime DATETIME2(3), @l_latestSynchedTime DATETIME2(3); 
DECLARE @l_LogoutReasonCode SMALLINT, @l_selType SMALLINT; 
DECLARE @l_LogonDuration INT, @l_resCount INT, @l_op INT; 
DECLARE @l_selValue varchar(4000); 

--Added @ in front of the table name, since it is a table variable you need the @. 
DECLARE cur CURSOR FOR 
    SELECT DISTINCT agentname, agentloginid 
    from @selected_agents 

OPEN cur 

--Here we added NEXT FROM to make the syntax correct 
FETCH NEXT FROM cur INTO @l_AgentName, @l_AgentLoginID; 

WHILE @@FETCH_STATUS = 0 
BEGIN 

    --Here you seem to be attempting to call a function to potentially set your variables, but this likley isn't happening 
    --In another window, try this function and then select the values of your variables to make sure they are being set 
    getAgentLogActivity(@l_AgentName, @l_AgentLoginID, @p_startTime, @p_endTime); 

    insert into final_result(Agent_Name, Agent_Login_ID, op1, Login_Time, op2,Logout_Time, Logout_Reason_Code, Logon_Duration) 
    select 
     @l_AgentName, 
     @l_AgentLoginID, 
     op1, 
     logintime, 
     op2, 
     logouttime, 
     reasoncode, 
     duration 
    from 
     temp_login_logout; 

--Here we added NEXT FROM to make the syntax correct 
FETCH NEXT FROM cur INTO @l_AgentName, @l_AgentLoginID; t 

END 
CLOSE cur; 
DEALLOCATE cur; 
+0

Sorry @scsimon, ich habe es durch deinen letzten Kommentar herausgefunden. Indem @ zur Deklaration gesetzt wurde und es funktioniert –

+0

Danke für das Zurückkreisen für die Schließung ... also war das eine Lösung. Gut zu wissen. – scsimon

+0

Ich habe diese Lösung nicht früher gesehen. So wusste nicht –