mssql游标有点问题,插入游标内的临时表
我的存储过程遇到了一些问题。 我编写了一个使用游标的存储过程。 一切正常,直到我将光标中的值插入临时表的地方。 这是错误
消息 156,级别 15,状态 1,过程 ors_DailyReportMessageStatus,第 36 行 关键字“where”附近的语法不正确。
这是代码
ALTER PROCEDURE [dbo].[ors_DailyReportMessageStatus]
-- Add the parameters for the stored procedure here
@startDate datetime, @endDate datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @num int;
DECLARE @stat varchar(20);
DECLARE @statusCursor CURSOR
SET @statusCursor = CURSOR FOR
select count(ms.status) as message, ms.status from message_status ms JOIN [message] m on m.id=ms.message_id
where (m.ADDED_ON >= @startDate AND m.ADDED_ON < @endDate) GROUP BY ms.status
SET NOCOUNT ON;
if object_id('tempdb..#tempdailystatus') is not null
begin
drop table #tempdailystatus
end
CREATE TABLE #tempdailystatus(id int identity, total int , [status] varchar(20));
insert into #tempdailystatus ([status])
select distinct([status]) from message_status;
open @statusCursor
fetch next from @statusCursor into @num, @stat;
while @@FETCH_STATUS = 0
begin
-- this is where the error is
insert into #tempdailystatus (total) values (@num) where id = (select id from #tempdailystatus where [status] = @stat)
-- this were just to see whether the cursor is ok. and it is
--print @stat
--print @num ;
fetch next from @statusCursor into @num,@stat
end
close @statusCursor
deallocate @statusCursor
-- Insert statements for procedure here
-- SELECT * from #tempdailystatus
drop table #tempdailystatus
END
我可能忽略了一些东西吗?我好像忘记了什么。
感谢您阅读本文并提出建议。我将不胜感激^_^
i'm having a little trouble with my stored procedure.
I've written a stored procedure where i'm using a cursor.
Every thing works fine till the place where i'm inserting values from the cursor to the temp table.
here is the error
Msg 156, Level 15, State 1, Procedure ors_DailyReportMessageStatus, Line 36
Incorrect syntax near the keyword 'where'.
and here is the code
ALTER PROCEDURE [dbo].[ors_DailyReportMessageStatus]
-- Add the parameters for the stored procedure here
@startDate datetime, @endDate datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @num int;
DECLARE @stat varchar(20);
DECLARE @statusCursor CURSOR
SET @statusCursor = CURSOR FOR
select count(ms.status) as message, ms.status from message_status ms JOIN [message] m on m.id=ms.message_id
where (m.ADDED_ON >= @startDate AND m.ADDED_ON < @endDate) GROUP BY ms.status
SET NOCOUNT ON;
if object_id('tempdb..#tempdailystatus') is not null
begin
drop table #tempdailystatus
end
CREATE TABLE #tempdailystatus(id int identity, total int , [status] varchar(20));
insert into #tempdailystatus ([status])
select distinct([status]) from message_status;
open @statusCursor
fetch next from @statusCursor into @num, @stat;
while @@FETCH_STATUS = 0
begin
-- this is where the error is
insert into #tempdailystatus (total) values (@num) where id = (select id from #tempdailystatus where [status] = @stat)
-- this were just to see whether the cursor is ok. and it is
--print @stat
--print @num ;
fetch next from @statusCursor into @num,@stat
end
close @statusCursor
deallocate @statusCursor
-- Insert statements for procedure here
-- SELECT * from #tempdailystatus
drop table #tempdailystatus
END
Am I possibly ignoring something? it seems like i'm forgetting something.
Thanks you for reading this and for giving suggestion .will appreciate it ^_^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将:替换
为:
我对您的逻辑做了一些假设,因此您可能需要进行相应调整。
try replacing:
with:
I made some assumptions about your logic, so you may need to adjust accordingly.