表 SMS_W 放一些定时邮件
编号    标题           发送对象     状态 
ID      Title        SendTo    Status
1      下午一点开会    1,2,3       0
2      下午二点开会    2,3         0
...........................表 UserInfo 为用户信息
ID  UserName   Email
1    赵          [email protected]
2    钱          [email protected]
3    孙          [email protected]表 SMS_S 放正在发送的邮件
编号      标题       接收人         邮箱地址
ID      Title    UserName       Email
求写一个存储过程,按表 SMS_W 中的记录和发送对象循环写入到 SMS_S 表中,并更新原 SMS_W 中的状态Status ,请高手支招,谢谢!谢谢!实现后的效果如下:
ID      Title      UserName       Email
1    下午一点开会       赵         [email protected]
2    下午一点开会       钱         [email protected]
3    下午一点开会       孙         [email protected]
4    下午二点开会       钱         [email protected]
5    下午二点开会       孙         [email protected]

解决方案 »

  1.   

    tba
    ID  classid   name
    1     1,2,3   西服 
    2     2,3    中山装
    3     1,3    名裤
    tbb 
    id   classname
    1     衣服
    2     上衣
    3     裤子我得的结果是
    id   classname            name
    1     衣服,上衣,裤子      西服 
    2          上衣,裤子     中山装
    3     衣服,裤子          名裤create table tba(ID int,classid varchar(20),name varchar(10))
    insert into tba values(1,'1,2,3','西服')
    insert into tba values(2,'2,3'  ,'中山装')
    insert into tba values(3,'1,3'  ,'名裤')
    create table tbb(ID varchar(10), classname varchar(10))
    insert into tbb values('1','衣服')
    insert into tbb values('2','上衣')
    insert into tbb values('3','裤子')
    go--第1种方法,创建函数来显示
    create function f_hb(@id varchar(10))
    returns varchar(1000)
    as
    begin
      declare @str varchar(1000)
      set @str=''
      select @str=@str+','+[classname] from tbb where charindex(','+cast(id as varchar)+',',','+@id+',')>0
      return stuff(@str,1,1,'')
    end
    go 
    select id,classid=dbo.f_hb(classid),name from tba
    drop function f_hb
    /*
    id          classid       name       
    ----------- ------------- ---------- 
    1           衣服,上衣,裤子 西服
    2           上衣,裤子      中山装
    3           衣服,裤子      名裤
    (所影响的行数为 3 行)
    */--第2种方法.update
    while(exists (select * from tba,tbb where charindex(tbb.id,tba.classid) >0))
    update tba
    set classid= replace(classid,tbb.id,tbb.classname)
    from tbb
    where charindex(tbb.id,tba.classid)>0
    select * from tba
    /*
    ID          classid              name       
    ----------- -------------------- ---------- 
    1           衣服,上衣,裤子       西服
    2           上衣,裤子            中山装
    3           衣服,裤子            名裤
    (所影响的行数为 3 行)
    */
    drop table tba,tbb
      

  2.   

    --分解字符串包含的信息值去另外一表查询相应的信息
    (爱新觉罗.毓华  2007-12-22  广东深圳)/*问题描述:
    需要将用户表中的用户组与信息表中的用户组对比.如果有相同的组则可查看该信息. 
    一个用户可能是几个用户组的成员.一条信息可能是几个用户组都可以查看. 表一 
    ID     usergroup     username 
    1      1,2,4         用户A 
    2      3             用户B 表二 
    id     usergroup     title   
    1      1,3,4         信息标题1 
    2      3             信息标题2 
    3      1,2,3         信息标题3 用户A可以看到   
    表二中的 
    信息标题1 
    信息标题3 用户B可以看到   
    表二中的 
    信息标题1 
    信息标题2 
    信息标题3
    */-------------------------------------------------------------
    --sql server 2000中用临时表的写法.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    go--建立一个辅助的临时表就可以了
    SELECT TOP 8000 id = identity(int,1,1) INTO tmp FROM syscolumns a, syscolumns b  
     
    select distinct c.username , d.title from
    (
      SELECT A.username,usergroup = SUBSTRING(A.usergroup, B.ID, CHARINDEX(',', A.usergroup + ',', B.ID) - B.ID) FROM tb A, tmp B WHERE SUBSTRING(',' + a.usergroup, B.id, 1) = ',' 
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA,tmp/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */--------------------------------------------------------------------
    --sql server 2000中不用临时表的SQL语句.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    goselect distinct c.username , d.title from
    (
      select a.username,usergroup=substring(a.usergroup,b.id,charindex(',',a.usergroup+',',b.id)-b.id)
      from tb a,(select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9  union select 10 union select 11 union select 12 union select 13 union select 14 union select 15) b
      where substring(','+a.usergroup,b.id,1)=','
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */-------------------------------------------------------------------
    --sql server 2005中的写法.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    goselect distinct c.username , d.title from
    (
      SELECT A.username, B.usergroup FROM(SELECT username, [usergroup] = CONVERT(xml,'<root><v>' + REPLACE([usergroup], ',', '</v><v>') + '</v></root>') FROM TB)A
      OUTER APPLY(SELECT usergroup = N.v.value('.', 'varchar(100)') FROM A.[usergroup].nodes('/root/v') N(v))B
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */
      

  3.   

    表1 
            id     name     Technical   ..... 
            01     张三           00001       ..... 
            02     李四           00001       ..... 表2     id               name       ..... 
            00001       项目经理     ..... 
            00002       法人代表     ..... 
            00003       CTO             ..... 
    表1为用户表,表2为"数据字典"现在可以通过   LEFT   得到 
    SELECT   a.id,a.name,b.fname   as   Technical   FROM   表1   a   left   join   表2   b   on   a.Technical=b.id 
            id     name       Technical     ..... 
            01     张三           项目经理       ..... 
            02     李四           法人代表       ..... 
    可是现在出现了这样的问题: 
    表1 
            id     name             Technical                   ..... 
            01     张三             00001,00002                 ..... 
            02     李四             00001,00002,00003     ..... 
    怎么样得到 
            id     name               Technical                       ..... 
            01     张三             项目经理,法人代表               ..... 
            02     李四             项目经理,法人代表   ,CTO     ..... 身兼数职,用左联右连接就不行了,在用上面的SQL语句就不行了!!! 
    declare @表1 table (
    id           varchar(5),
    name           varchar(10),
    Technical       varchar(30)
    )insert @表1 select
                    '01',           '张三',                           '00001,00002'
    union all select
                    '02',           '李四',                           '00001,00002,00003'
    declare @表2 table (
    id  varchar(10),                             
    name varchar(10)
    )
    insert @表2 select
                    '00001',               '项目经理'
    union all select
                    '00002',               '法人代表'
    union all select
                    '00003',               'CTO'  while exists (select 1 from @表1 a,@表2 b where a.Technical like '%'+b.id+'%')
    update a
    set Technical=replace(a.Technical,b.id,b.name)
    from @表1 a,@表2 b
    where a.Technical like '%'+b.id+'%'select * from @表1--结果
    id    name       Technical                      
    ----- ---------- ------------------------------ 
    01    张三         项目经理,法人代表
    02    李四         项目经理,法人代表,CTO(所影响的行数为 2 行)
      

  4.   

    基本上明白,谢谢 liangCK  的解答... 结贴。
      

  5.   

    create table sms_w(id int,title nvarchar(20),sendto varchar(20),status tinyint)
    insert sms_w select 1  ,    N'下午一点开会'  ,  '1,2,3'   ,    0 
    insert sms_w select 2  ,    N'下午二点开会'  ,  '2,3'     ,    0create table userinfo(id int,username nvarchar(20),email varchar(50))
    insert userinfo select 1  ,  N'赵'   ,       '[email protected]
    insert userinfo select 2  ,  N'钱'   ,       '[email protected]
    insert userinfo select 3  ,  N'孙'   ,       '[email protected]' create table sms_s(id int identity,title nvarchar(20),username nvarchar(20),email varchar(50))select top 500 id=identity(int,1,1) into # from sys.columns a,sys.columns binsert sms_s (title,username,email)
     select t.title,b.username,b.email
      from
       (
         select title,sendto=substring(a.sendto,b.id,charindex(',',a.sendto+',',b.id)-b.id)
         from sms_w a,# b
         where substring(','+a.sendto,b.id,1)=','
       ) t inner join userinfo b on t.sendto=b.idselect * from sms_sdrop table sms_w,userinfo,sms_s,#/*
    id          title                username             email
    ----------- -------------------- -------------------- --------------------------------------------------
    1           下午一点开会               赵                    [email protected]
    2           下午一点开会               钱                    [email protected]
    3           下午一点开会               孙                    [email protected]
    4           下午二点开会               钱                    [email protected]
    5           下午二点开会               孙                    [email protected](5 行受影响)
    */
      

  6.   

    create table SMS_W(编号 int , 标题 varchar(20) , 发送对象 varchar(20) , 状态 int)
    insert into SMS_W values(1, '下午一点开会', '1,2,3', 0 )
    insert into SMS_W values(2, '下午二点开会', '2,3'  , 0 )
    create table UserInfo(ID int , UserName varchar(10) , Email varchar(20))
    insert into UserInfo values(1 , '赵' , '[email protected]') 
    insert into UserInfo values(2 , '钱' , '[email protected]') 
    insert into UserInfo values(3 , '孙' , '[email protected]' )
    go
    -- 建立一个辅助的临时表就可以了
    SELECT TOP 8000 
    id = identity(int,1,1) INTO # FROM syscolumns a, syscolumns b   
     
    select m.编号, m.标题 , n.UserName,n.Email
    from
    (
      SELECT A.编号, A.标题,发送对象 = SUBSTRING(A.发送对象, B.ID, CHARINDEX(',', A.发送对象 + ',', B.ID) - B.ID)
      FROM SMS_W A, # B
      WHERE SUBSTRING(',' + a.发送对象, B.id, 1) = ','    
    ) m,userinfo n
    where m.发送对象 = n.iddrop table SMS_W,UserInfo,#/*
    编号          标题                   UserName   Email                
    ----------- -------------------- ---------- -------------------- 
    1           下午一点开会               赵          [email protected]
    1           下午一点开会               钱          [email protected]
    1           下午一点开会               孙          [email protected]
    2           下午二点开会               钱          [email protected]
    2           下午二点开会               孙          [email protected](所影响的行数为 5 行)
    */