给用户群发短信的存储过程

xjc55 2007-04-11 08:17:49
如题所示,我的意图,从表user中取出每个人的名称,然后把传进的参数和每个人的名称一起写进表msg
create procedure send_msg_all
@msg_content nvarchar(500),
@msg_title nvarchar(100),
@msg_team int,
@username nvarchar(50) output,
@count int output
as
select @count=count(*) from mydaigo_user where roles=@msg_team
while @count>0
begin
select @username=username from user where roles=@msg_team
insert into msg(title,msg_content,toname,postname) values(@msg_title,@msg_content,@username,'校园代购网')
set @count=@count-1
end
按以上方法做,虽然记录条数插进去了,但是用户名称却都是那一个名称,请问如何使记录下移,取得所有符合条件的名称

...全文
161 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjc55 2007-04-12
  • 打赏
  • 举报
回复
明白,谢谢
OracleRoob 2007-04-11
  • 打赏
  • 举报
回复
create procedure send_msg_all
(
@msg_content nvarchar(500),
@msg_title nvarchar(100),
@msg_team int,
@username nvarchar(50) output,
@count int output
)
as

--不需要用循环

insert into msg(title,msg_content,toname,postname)
select @msg_title,@msg_content,username,'校园代购网'
from [user] --这里需要加[]
where roles=@msg_team

GO

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧