select * from table1 得到:id         name
1 自动化楼宇
2 自动化办公 现在要将下面的select语句笛卡尔积的加到上面的集合中select convert(varchar(10),getdate(),23) + ' ' + ltrim(number+1)+':00' from master..spt_values where type='P' and number<=5得到最终我想要的集合:id         name         time
1 自动化楼宇  2010-05-17 1:00  
1 自动化楼宇  2010-05-17 2:00
1 自动化楼宇  2010-05-17 3:00  
1 自动化楼宇  2010-05-17 4:00
1 自动化楼宇  2010-05-17 5:00   
1 自动化楼宇  2010-05-17 6:00    
2 自动化办公  2010-05-17 1:00
2 自动化办公  2010-05-17 2:00
2 自动化办公  2010-05-17 3:00  
2 自动化办公  2010-05-17 4:00
2 自动化办公  2010-05-17 5:00 
2 自动化办公  2010-05-17 6:00
求此SQL语句

解决方案 »

  1.   

    ---测试数据---
    if object_id('[table1]') is not null drop table [table1]
    go
    create table [table1]([id] int,[name] varchar(10))
    insert [table1]
    select 1,'自动化楼宇' union all
    select 2,'自动化办公'
     
    ---查询---
    select 
      a.*,
      [time]=convert(varchar(16),dateadd(hh,b.number,convert(varchar(10),getdate(),120)),120)
    from table1 a,
    master..spt_values  b
    where
    b.type='P' and b.number between 1 and 6
    order by 1,3---结果---
    id          name       time
    ----------- ---------- ----------------
    1           自动化楼宇      2010-05-17 01:00
    1           自动化楼宇      2010-05-17 02:00
    1           自动化楼宇      2010-05-17 03:00
    1           自动化楼宇      2010-05-17 04:00
    1           自动化楼宇      2010-05-17 05:00
    1           自动化楼宇      2010-05-17 06:00
    2           自动化办公      2010-05-17 01:00
    2           自动化办公      2010-05-17 02:00
    2           自动化办公      2010-05-17 03:00
    2           自动化办公      2010-05-17 04:00
    2           自动化办公      2010-05-17 05:00
    2           自动化办公      2010-05-17 06:00(12 行受影响)
      

  2.   


    select *
    from t,(select time=convert(varchar(10),getdate(),23) + ' ' + ltrim(number+1)+':00' from master..spt_values where type='P' and number<=5) as a
      

  3.   

    if OBJECT_ID('t') is not null
      drop table t
    go
      
    create table t
    (
    id int,
    name varchar(20)
    )
    goinsert into t
    select 1,'自动化楼宇'
    union
    select 2,'自动化办公'
    goselect *
    from t,(select time=convert(varchar(10),getdate(),23) + ' ' + ltrim(number+1)+':00' from master..spt_values where type='P' and number<=5) as a
    go