这个存储过程怎么写,求高手出来帮忙

来源:百度知道 编辑:UC知道 时间:2024/07/02 08:51:55
A表中有10条记录(注册用户表),B表中有10000 条记录(通话记录表)
我要分别找出10个注册用户的通话总条数,这10个用户,如果有话单的我们在C表中插入一条记录,内容列为:"您本月话单数为xx条”,如果没有通话记录的,在C表中插入一条记录,内容列为:"您本月没有话单”。怎么写,求高手指点。

--设a表中有一个存储用户名(a_name)的字节,和b表中有一个存储对应关系的用户名(b_name)字节
DECLARE @the_a_name varchar(50)
DECLARE a_Cursor
cursor for select a_name from a
open a_Cursor
FETCH NEXT FROM a_Cursor into @the_a_name
while(@@fetch_status=0)
begin
DECLARE @count_b_name int
DECLARE @resultContent nvarchar(50)
select @count_b_name=count(*) from b where b_name=@the_a_name
if @count_b_name>0
begin
Set @resultContent = '您本月话单数为'+@count_b_name+'条'
end
Else
begin
Set @resultContent = '您本月没有话单'
end
exec('insert into [c] (content,addDate)values('+@resultContent+',getdate())')
FETCH NEXT FROM a_Cursor INTO @the_a_name
END
CLOSE a_Cursor