27,580
社区成员
发帖
与我相关
我的任务
分享
create table tb(A varchar(10), B int, C int)
insert into tb values('a' , 1 , 31 )
insert into tb values('b' , 2 , 32 )
insert into tb values('a' , 3 , 33 )
go
select cast(a as varchar) d from tb
union all
select cast(b as varchar) d from tb
drop table tb
/*
d
----------
a
b
a
1
2
3
*/
create table A(a1 varchar(20),a2 varchar(20))
insert into A values('a','1')
insert into A values('b','2')
insert into A values('c','3')
create Table B(B1 varchar(20))
insert into B
select a1
from A
insert into B
select a2
from A
select * from B
--
a
b
c
1
2
3
create table #1(A varchar(10), B int, C int)
insert into #1 values('a' , 1 , 31 )
insert into #1 values('b' , 2 , 32 )
insert into #1 values('a' , 3 , 33 )
go
-----如果列中不能包含重复值
select rtrim(a) D from #1
union
select rtrim(b) from #1
/*
D
------------
1
2
3
a
b
(5 row(s) affected)
*/
------如果列中可以包含重复值
select rtrim(a) D from #1
union all
select rtrim(b) from #1
/*
D
------------
a
b
a
1
2
3
(6 row(s) affected)
*/
--如果类型不一样,都转换为varchar
create table tb(A varchar(10), B int, C int)
insert into tb values('a' , 1 , 31 )
insert into tb values('b' , 2 , 32 )
insert into tb values('a' , 3 , 33 )
go
select cast(a as varchar) d from tb
union all
select cast(b as varchar) d from tb
drop table tb
/*
d
----------
a
b
a
1
2
3
*/
create table tb(A varchar(10), B varchar(10), C int)
insert into tb values('a' , '1' , 31 )
insert into tb values('b' , '2' , 32 )
insert into tb values('a' , '3' , 33 )
go
select a d from tb
union all
select b d from tb
drop table tb
/*
d
----------
a
b
a
1
2
3
*/
SELECT A FROM tb
UNION ALL
SELECT RTRIM(B) FROM tb
select a ,b, c ,a as d from table
union all
select a ,b, c ,b as d from table
select a from tb
union all
select b from tb