表:Work name       date                 time         pic 张华       a,e                  1,2,3      zhanghua.jpg
 李四       f,g                  1,3         lisi.jpg字段date表示周一到周日
a表周一,e表周五......字段time中1表上午,2表示下午,3表示晚上字段pic是每个人的照片
--------------------------------------------------------------------------------------------------- 
上面的数据表示:李四:    周六,周日的上午与晚上上班
张华:   周一,周五的上午,下午,晚上都上班     查询张华的上班记录为:name='张华',a1='zhanghua.jpg',a2='zhanghua.jpg',a3='zhanghua.jpg',e1='zhanghua.jpg',e2='zhanghua.jpg',e3='zhanghua.jpg'
说明:a1表示周一的上午,a2表示周一的下午,a3表示周一的晚上,b1表示周二的上午,b2表示周二的下午......g1表示周日的上午,g2表示周日的下午,g3表示周日的晚上
  如果上班就显示他的照片

解决方案 »

  1.   

    数据结构不能修改吗?一个很不好的结构啊
    id name     week timeType pic    datetime
    1  zhanghua a     1         2008-07-04
    2  zhanghua a     2       
    3  zhanghua a     3        
    4  zhanghua e     1       有机会还是重新设计一下吧
      

  2.   


    create table DKJL (name nvarchar(100),date nvarchar(100),time nvarchar(100),pic nvarchar(100))
    insert into DKJL select '张华','a,e','1,2,3','zhanghua.jpg' union  select '李四','f,g','1,3','lisi.jpg'create proc dynamicTable1(@name nvarchar(100))
    as
     begin
       declare @dyDateStr nvarchar(100),@dyTimeStr nvarchar(100),@curDateStr nvarchar(10),@curTimeStr nvarchar(10),@dySQL nvarchar(1000)
       declare @tempTimeStr nvarchar(100),@tempDateStr nvarchar(100)
        --创建一个数据表
       if exists(select 1 from sysobjects where id=object_id('dynamicTable') and objectproperty(id,'istable')=1)
       exec ('drop table dynamicTable')
       create table dynamicTable(name nvarchar(100))--
       
       select @dyDateStr=date,@dyTimeStr=time from dkjl where name=@name
        
       set @dyDateStr= @dyDateStr +','print @dyDateStr
       set @dyTimeStr= @dyTimeStr+','
      set @tempDateStr=@dydatestr
      set @tempTimeStr=@dyTimeStr
        --循环Date字符串
       while(@dyDateStr is not null and @dyDateStr<>'')
         begin
              set @curDateStr = substring(@dyDateStr,1,charindex(',',@dyDateStr)-1)--
              set @dyTimeStr = @tempTimeStr
             while (@dyTimeStr is not null and  @dyTimeStr<>'')
               begin
                 set @curTimeStr=substring(@dyTimeStr,1,charindex(',',@dyTimeStr)-1)
                 --更改表结构
                 set @dySQL = 'alter table dynamicTable add '+quotename(@curDateStr+@curTimeStr)+' nvarchar(100)'
                 exec (@dySql)
                 set @dyTimeStr= stuff(@dyTimeStr,1,charindex(',',@dyTimeStr),'')
               end
              set @dyDateStr =stuff(@dyDateStr,1,charindex(',',@dyDateStr),'')
         end
         set @dyTimeStr = @tempTimeStr
         while(@tempDateStr is not null and @tempDateStr<>'')
         begin
              set @curDateStr = substring(@tempDateStr,1,charindex(',',@tempDateStr)-1)--
                set  @tempTimeStr= @dyTimeStr
             while ( @tempTimeStr is not null and   @tempTimeStr<>'')
               begin
                 set @curTimeStr=substring(@tempTimeStr,1,charindex(',',@tempTimeStr)-1)
                 --更改表结构
                 set @dySQL = 'insert into  dynamicTable ( name,'+quotename(@curDateStr+@curTimeStr)+')  select  '+quotename(@name,'''')+','+quotename(@curDateStr+@curTimeStr,'''')
           
                 exec (@dySql)
                 set @tempTimeStr= stuff(@tempTimeStr,1,charindex(',',@tempTimeStr),'')
               end
              set @tempDateStr =stuff(@tempDateStr,1,charindex(',',@tempDateStr),'')
         end
       
       exec ('select name,a1=max(case when a1=''a1'' then ''周一上午上班'' else null end), a2=max(case when a2=''a2'' then ''周一下午上班'' else null end), a3=max(case when a3=''a3'' then ''周一晚上上班'' else null end),e1=max(case when e1=''e1'' then ''周五上午上班'' else null end),e2=max(case when e2=''e2'' then ''周五下午一班'' else null end),e3=max(case when e3=''e3'' then ''周五下午上班'' else null end ) from dynamictable group by name')
     endname                                                                                                 a1           a2           a3           e1           e2           e3           
    ---------------------------------------------------------------------------------------------------- ------------ ------------ ------------ ------------ ------------ ------------ 
    张华                                                                                                   周一上午上班       周一下午上班       周一晚上上班       周五上午上班       周五下午一班       周五下午上班(所影响的行数为 1 行)警告: 聚合或其它 SET 操作消除了空值。