小小SQLServer,你懂的

declare @i int
declare @sum int

set @i=1
set @sum=0

while @i<=100
begin
    set @sum=@sum+@i
    set @i=@i+1
end

print @sum


if 1=10
    print ''
else if 2=2
    print '对错'
else 
    print ''



declare @today int
declare @week nvarchar(3)

set @today=25
set @week= 
    case 
    when @today=1 then '星期一'
    when @today=2 then '星期二'
    when @today=3 then '星期三'
    else '错误'
    end
print @week


create table t_a(
id int not null,
name varchar(5)
)


insert into t_a values(1,'a1')
insert into t_a values(1,'a2')
insert into t_a values(2,'a3')
insert into t_a values(4,'a4')
insert into t_a values(5,'a5')

create table t_b(
id int not null,
name varchar(5)
)


insert into t_b values(1,'b1')
insert into t_b values(1,'b2')
insert into t_b values(2,'b3')
insert into t_b values(4,'b4')
insert into t_b values(5,'b5')


select * from t_a
union all 
select * from t_b

update t_b set name ='b3' where name='a3'


select * from t_a left join t_b on (t_a.id=t_b.id)

select * from t_a right join t_b on(t_a.id=t_b.id)

select * from t_a inner join t_b on(t_a.id=t_b.id)

select * from t_a,t_b where t_a.id=t_b.id


--删除有重复的SQL
delete from t_a
where name in (
    select max(name) from t_a group by id having count(name)>1
)

--游标

declare @id int 
declare f_cursor  cursor for select id from t_a 

open f_cursor
while @@fetch_status=0
begin 
    fetch next from f_cursor into @id
    print @id
    print 'd'
end
close f_cursor
deallocate f_cursor

 

posted @ 2013-04-24 23:00  lhfly  阅读(312)  评论(0编辑  收藏  举报