如题
tb1id  tname
1   a
2   b
3   c
4   d
tb2tname  ttype
a      运行
a      维护
a      准备
b      运行
b      维护
b      准备
c      运行
c      维护
c      准备
d      运行
d      维护
d      准备基础表tb1数据是变量
要用SQL SEVER存储过程实现当 tb2中无数据的时候,插入每个tname的3种状态,状态是固定的3种
50分笑纳~在线等!!!

解决方案 »

  1.   

    insert into tb2
    select tb1.tname , m.ttype from tb1 ,
    (
    select '运行' as ttype union all 
    select '维护' as ttype union all 
    select '准备'
    ) m
      

  2.   

    create table tb1 (id int , tname varchar(10))
    insert into tb1 values(1 , 'a') 
    insert into tb1 values(2 , 'b') 
    insert into tb1 values(3 , 'c') 
    insert into tb1 values(4 , 'd')
    create table tb2(tname varchar(10), ttype varchar(10))
    goinsert into tb2
    select tb1.tname , m.ttype from tb1 ,
    (
    select '运行' as ttype union all 
    select '维护' as ttype union all 
    select '准备'
    ) mselect * from tb2drop table tb1 , tb2/*
    tname      ttype      
    ---------- ---------- 
    a          运行
    a          维护
    a          准备
    b          运行
    b          维护
    b          准备
    c          运行
    c          维护
    c          准备
    d          运行
    d          维护
    d          准备(所影响的行数为 12 行)*/
      

  3.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[tname] varchar(1))
    insert [tb]
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c' union all
    select 4,'d'
     
    ---查询---
    select 
      a.tname,
      b.ttype  
    from tb a,(select '运行' ttype  union select '维护' union select '准备') b
    ---结果---
    tname ttype   
    ----- ---- 
    a     维护
    b     维护
    c     维护
    d     维护
    a     运行
    b     运行
    c     运行
    d     运行
    a     准备
    b     准备
    c     准备
    d     准备(所影响的行数为 12 行)
      

  4.   

    select * from 
    (select 'aaa' a union all
    select 'bbb' a union all
    select 'ccc' a) a
    cross join 
    (select '运行' b union all
    select '维护' b union all
    select '准备' b) b
    a    b
    ---- ----
    aaa  运行
    aaa  维护
    aaa  准备
    bbb  运行
    bbb  维护
    bbb  准备
    ccc  运行
    ccc  维护
    ccc  准备
      

  5.   

    insert tb2(tname,ttype)
    select a.tname,b.b 
    (select tname from tb1) a
    cross join 
    (select '运行' b union all
    select '维护' b union all
    select '准备' ) b
      

  6.   

    表别名,你这个不需要存储过程.直接用SQL语句就行了.
      

  7.   

    如果需要存储过程.--创建测试数据
    create table tb1 (id int , tname varchar(10))
    insert into tb1 values(1 , 'a') 
    insert into tb1 values(2 , 'b') 
    insert into tb1 values(3 , 'c') 
    insert into tb1 values(4 , 'd')
    create table tb2(tname varchar(10), ttype varchar(10))
    go--创建存储过程
    create procedure my_proc
    as
    insert into tb2
    select tb1.tname , m.ttype from tb1 ,
    (
    select '运行' as ttype union all 
    select '维护' as ttype union all 
    select '准备'
    ) m
    go--执行存储过程
    exec my_proc--查询数据
    select * from tb2--删除表和存储过程
    drop table tb1 , tb2
    drop procedure my_proc/*
    tname      ttype      
    ---------- ---------- 
    a          运行
    a          维护
    a          准备
    b          运行
    b          维护
    b          准备
    c          运行
    c          维护
    c          准备
    d          运行
    d          维护
    d          准备(所影响的行数为 12 行)*/
      

  8.   

    不好意思 还有个小问题
    我的tb2表中还有个自增长的ID值,
    插入的时候就不好用了,请问怎么解决好呢?
    麻烦您了~
      

  9.   

    tb2表加ID,自增.--不用存储过程的,直接用SQL语句.
    create table tb1 (id int , tname varchar(10))
    insert into tb1 values(1 , 'a') 
    insert into tb1 values(2 , 'b') 
    insert into tb1 values(3 , 'c') 
    insert into tb1 values(4 , 'd')
    create table tb2(id int identity , tname varchar(10), ttype varchar(10))
    goinsert into tb2(tname , ttype)
    select tb1.tname , m.ttype from tb1 ,
    (
    select '运行' as ttype union all 
    select '维护' as ttype union all 
    select '准备'
    ) mselect * from tb2drop table tb1 , tb2/*
    id          tname      ttype      
    ----------- ---------- ---------- 
    1           a          运行
    2           a          维护
    3           a          准备
    4           b          运行
    5           b          维护
    6           b          准备
    7           c          运行
    8           c          维护
    9           c          准备
    10          d          运行
    11          d          维护
    12          d          准备(所影响的行数为 12 行)
    */
    --用存储过程的.--创建测试数据
    create table tb1 (id int , tname varchar(10))
    insert into tb1 values(1 , 'a') 
    insert into tb1 values(2 , 'b') 
    insert into tb1 values(3 , 'c') 
    insert into tb1 values(4 , 'd')
    create table tb2(id int identity , tname varchar(10), ttype varchar(10))
    go--创建存储过程
    create procedure my_proc
    as
    insert into tb2(tname , ttype)
    select tb1.tname , m.ttype from tb1 ,
    (
    select '运行' as ttype union all 
    select '维护' as ttype union all 
    select '准备'
    ) m
    go--执行存储过程
    exec my_proc--查询数据
    select * from tb2--删除表和存储过程
    drop table tb1 , tb2
    drop procedure my_proc/*
    id          tname      ttype      
    ----------- ---------- ---------- 
    1           a          运行
    2           a          维护
    3           a          准备
    4           b          运行
    5           b          维护
    6           b          准备
    7           c          运行
    8           c          维护
    9           c          准备
    10          d          运行
    11          d          维护
    12          d          准备(所影响的行数为 12 行)
    */