求教这句SQL该如何写 最好
数据结构:
有一张临时表#t,有三列,分别为A,B,C
具体数据如下
A B C
-------------
1 1 1
1 2 0
1 3 1
1 4 0
现想做到把C=0的某行数据,插入该行下面
结果
A B C
-------------
1 1 1
1 2 0
1 2 0
1 3 1
1 4 0
1 4 0
我使用的SQL语句如下:
create table #t
(
A int,
B int,
C int
)
insert into #t
select 1,1,1 union all
select 1,2,0 union all
select 1,3,1 union all
select 1,4,0
select * from #t
union all
select * from #t where C = 0
order by B,C
create table #t
(
A int,
B int,
C int
)
insert into #t
select 1,1,1 union all
select 1,2,0 union all
select 1,3,1 union all
select 1,4,0
go
insert into #t
select * from #t where C =0
go
select * from #t
create table #t借用一下早恋的代码,其实5楼已经写出来了
(
A int,
B int,
C int
)
insert into #t
select 1,1,1 union all
select 1,2,0 union all
select 1,3,1 union all
select 1,4,0
go
insert into #t
select * from #t where C =0
go
select * from #t
ORDER BY b,c
/*
A B C
----------- ----------- -----------
1 1 1
1 2 0
1 2 0
1 3 1
1 4 0
1 4 0
*/