给用户群发短信的存储过程
如题所示,我的意图,从表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
按以上方法做,虽然记录条数插进去了,但是用户名称却都是那一个名称,请问如何使记录下移,取得所有符合条件的名称
------解决方案--------------------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