我在Informix数据库中有一个游标,它是函数的一部分,我想将它转换为MS-SQL中的游标。
以下是代码:
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;
在这种情况下,我发现了Declare语句的问题。我知道我必须以下面的方式写这个。
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;
更新 - selected_agents声明
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
);
现在 selected_agents 是我在函数中创建的临时表。
它所说的错误是agentname,agentloginid未声明。我已经宣布了btw。
有人可以帮助我如何纠正它。
答案 0 :(得分:1)
这可能不是100%的解决方案,但它有很多评论可以帮助您入门。请随时发表评论,告诉我您仍然遇到问题的地方。
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;