mssql游标有点问题,插入游标内的临时表

发布于 2024-11-09 18:48:32 字数 1703 浏览 3 评论 0原文

我的存储过程遇到了一些问题。 我编写了一个使用游标的存储过程。 一切正常,直到我将光标中的值插入临时表的地方。 这是错误

消息 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-11-16 18:48:32

尝试将:替换

insert into #tempdailystatus (total) values (@num) where id = (select id from   #tempdailystatus where [status] = @stat)

为:

If Exists(Select 1 From #tempdailystatus where [status] = @stat)
  Begin
    insert into #tempdailystatus (total) Values(@num)
  End

我对您的逻辑做了一些假设,因此您可能需要进行相应调整。

try replacing:

insert into #tempdailystatus (total) values (@num) where id = (select id from   #tempdailystatus where [status] = @stat)

with:

If Exists(Select 1 From #tempdailystatus where [status] = @stat)
  Begin
    insert into #tempdailystatus (total) Values(@num)
  End

I made some assumptions about your logic, so you may need to adjust accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文