20070730PM


--查询jobs表中前第5行到前第10行记录(限制返回到结果集中的行数)
select top 6 from jobs
where jobs_id not in(select top 4 from jobs)


--左右连接
select * from a_table_1 a,b_table_1 b
where a.id = b.stu_id

select * from a_table_1 a inner join b_table_1 b
on a.id = b.stu_id

select a.id,b.score from a_table_1 a left outer join b_table_1 b
on a.id = b.stu_id

select a.id,a.name,b.score from a_table_1 a full outer join b_table_1 b
on a.id = b.stu_id


--创建一个存储过程,根据指定的金额和指定的转帐帐户,
--完成简单转帐功能
--(要求在转帐过程中,全部执行成功或者全部不执行)
--例如:A帐户转帐1000元到C帐户
create database test
go
use test
go
create table zhanghu
(
   zhid int,
   zhname varchar(15),
   zhmoney money
)
go
insert into zhanghu(zhid,zhname,zhmoney)
values(10001,'A',5000)
insert into zhanghu(zhid,zhname,zhmoney)
values(10003,'B',500)
insert into zhanghu(zhid,zhname,zhmoney)
values(10004,'C',10000)
go


create   proc p_transfer
@out_id int,
@in_id int,
@money money
as
begin
begin tran

if(select zhmoney from zhanghu where zhid = @out_id)>@money
   begin
    update zhanghu set zhmoney = zhmoney - @money
    where zhid = @out_id

    if exists(select * from zhanghu where zhid = @in_id)
     begin
      update zhanghu set zhmoney = zhmoney + @money
      where zhid = @in_id
      commit tran
     end
    else
     begin
     print '转入帐户不存在!'
     rollback tran
     end
   end
else
   begin
   print '     余额不足!'
   rollback tran
   end
end
go

exec p_transfer '10001','10005',1000
go

select * from zhanghu
go