--在网吧写的,没有办法验证有没有错误
--只能一个字段字段的取哦
create table t1
( f1 int,
f2 int,
f3 int,
f4 int
)
create table t2
( t int,
v int
)insert into t2
select 1 as t , f1 as v
from t1
where f1 <> 0
union
select 2 as t , f2 as v
from t1
where f2 <> 0
union
select 3 as t , f3 as v
from t1
where f3 <> 0
union
select 4 as t , f4 as v
from t1
where f4 <> 0select * from t2
drop table t1
drop table t2
--只能一个字段字段的取哦
create table t1
( f1 int,
f2 int,
f3 int,
f4 int
)
create table t2
( t int,
v int
)insert into t2
select 1 as t , f1 as v
from t1
where f1 <> 0
union
select 2 as t , f2 as v
from t1
where f2 <> 0
union
select 3 as t , f3 as v
from t1
where f3 <> 0
union
select 4 as t , f4 as v
from t1
where f4 <> 0select * from t2
drop table t1
drop table t2
解决方案 »
- 迷惑,请指点迷津
- XP安SQL SERVER 2008 企业版 无SQL Server Management Studio出来???
- sql server 2008有没有类似log explorer这样能够查看事务日志的软件呢?
- 不是数据库中的有效用户
- 困惑很长时间的查询语句
- 兩表之間的sql語句!
- 跨数据库建立外键
- 求高手一sql_____(已将一个月的数据插入临时表,求月平均、周平均、前天、昨天、今天)
- 在精华区中我看见如下内容"UPDATE master..sysdatabases SET status=0 WHERE name='dbname'"
- 怎样得到局域网内所有的SQLServer实例列表???
- 怎么删除已经存在的逻辑设备
- 如何随机汉字?急
( f1 int,
f2 int,
f3 int,
f4 int
)
insert t1
select 50,60,80,90 union
select 100,150,200,0 uniondrop table t2
create table t2
( t int,
v int
)DECLARE @SQL as varchar(8000)
set @SQL=''
select @SQL=@SQL + ' select '+ cast(T_Id as varchar) +' as T_Id,' + name + ' from t1 union all' from (select (select count(*) from (select name from syscolumns where id=object_id('t1'))T2 where T2.name<=T1.name) as T_Id,T1.name from (select name from syscolumns where id=object_id('t1')) T1) M
set @SQL='insert into t2 '+left(@SQL,len(@SQL)-len(' union all'))
exec (@SQL)
select * from t2
set @SQL=''
select @SQL=@SQL + ' select '+ cast(T_Id as varchar) +' as T_Id,' + name + ' from t1 where '+ name +'>0 union all' from (select (select count(*) from (select name from syscolumns where id=object_id('t1'))T2 where T2.name<=T1.name) as T_Id,T1.name from (select name from syscolumns where id=object_id('t1')) T1) M
set @SQL='insert into t2 '+left(@SQL,len(@SQL)-len(' union all'))
exec (@SQL)