您的位置:程序门 -> ms-sql server -> 基础类



请教存储过程 临时表 添加新列


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教存储过程 临时表 添加新列[已结贴,结贴人:zphu]
发表于:2007-07-11 13:39:04 楼主
从表1中select出如下数据(table1):

select   date,   volume   from   table1
date               volume
2007-7-1         100
2007-7-2         80
2007-7-5         20
2007-7-6         5
2007-7-9         25
2007-7-10       35

从表2中select出如下数据(table2):

select   date,   volume   from   table2
date               volume
2007-7-1         30
2007-7-3         80
2007-7-5         60
2007-7-6         5
2007-7-8         25

从表3中select出如下数据(table3):

select   date,   volume   from   table3
date               volume
2007-7-2         3
2007-7-3         8
2007-7-4         6
2007-7-6         5
2007-7-11       24

有很多表table....类似上面select   出类似的数据。

我的问题:
能否建立一个存储过程,建立一个临时表,临时表有3个字段date,volume,type.type说明来自哪一个表。
最后的临时表中的内容为:

date               volume     type
2007-7-1         100           t1
2007-7-2         80             t1
2007-7-5         20             t1
2007-7-6         5               t1
2007-7-9         25             t1
2007-7-10       35             t1
2007-7-1         30             t2
2007-7-3         80             t2
2007-7-5         60             t2
2007-7-6         5               t2
2007-7-8         25             t2
2007-7-2         3               t3
2007-7-3         8               t3
2007-7-4         6               t3
2007-7-6         5               t3
2007-7-11       24             t3


发表于:2007-07-11 13:43:091楼 得分:5
select   date,   volume, 't1 '   as   type   into   #tmp   from   table1
union   all
select   date,   volume, 't2 '   as   type   from   table2
union   all
select   date,   volume, 't3 '   as   type   from   table3

----查看
select   *   from   #tmp
发表于:2007-07-11 13:43:122楼 得分:3
select   *,   't1 '   type   from   t1
union   all
select   *,   't2 '   type   from   t2
union   all
select   *,   't3 '   type   from   t3
发表于:2007-07-11 13:49:183楼 得分:10
create   proc   sp_ins
as
begin
    if   object_id( '#tmp ')   is   not   null
        drop   table   #tmp

    select   date,   volume,   't1 '   as   type   into   #tmp   from   table1

    insert   #tmp
        select   date,   volume,   't2 '   from   table2
    union   all
        select   date,   volume,   't3 '   from   table3
    union   all
        select   date,   volume,   't4 '   from   table4
end
发表于:2007-07-11 13:49:274楼 得分:2

select   date,   volume,type   =   't1 'into   #temp   from   table1   union   all
select   date,   volume,type   =   't2 '   from   table2   union   all
select   date,   volume,type   =   't3 '   from   table3   union   all


select   *   from   #temp
发表于:2007-07-11 14:00:145楼 得分:0
谢谢以上回复,结帖!


快速检索

最新资讯
热门点击