首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求教这句SQL该怎么写 最好

2013-07-01 
求教这句SQL该如何写 最好数据结构:有一张临时表#t,有三列,分别为A,B,C具体数据如下ABC-------------11112

求教这句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


     想求教论坛各位达人,有没有更好的算法

     谢谢 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  
go
insert into #t
select * from #t where C =0
go
select * from #t

[解决办法]
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
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

*/

借用一下早恋的代码,其实5楼已经写出来了

热点排行