sql存储过程

--1.减少网络传输
--2.杜绝sql注入攻击(无论是带参数的sql语句与存储过程原理一样)
--3.模块化设计
--3已经编译好,直接执行,不需要检查语句,编译了,提高性能
sp_helptext 'sp_tables'  --以sp,xp开头的都是系统预定义的存储过程
--exec sp_ helptext 'sp_renamedb'

--用户自定义存储过程,一般用"Usp_"开头的命名        xp_cmdshell
----------------------------------------------
go
create proc usp_helloworld
as
begin
print 'helloworld'
end
go
exec usp_helloworld
---------------- --带参数的存储过程
go
create proc usp_helloworld1
@msg nvarchar(3000) 
as
begin
print @msg
end

exec usp_helloworld1 N'传智播客'
-----------------------------
go
create proc usp_calAnd
@anum int,
@bnum  int
as
begin
print @anum+@bnum
end
exec usp_calAnd @anum=2,@bnum =3
---删除--------------------------------------
drop pro usp_calAnd
---------------------------------------------
go
alter proc usp_calAnd
@anum int=100,
@bnum  int=200
as
begin
print @anum+@bnum
end

exec usp_calAnd 

----------------存储过程中的输出参数------------------
select * from Tblstudent

go
create proc usp_selectByAge
@age int,
@count int output  --输出参数
as 
begin
--根据age进行查询
 select * from TblStudent where tSAge=@age
 --返回一共查询了多少条数据
 set @count=(select COUNT(*) from TblStudent where tSAge=@age)
end

delare @c int
exec usp_selectByAge @age=20,@count=@count output
print @c
--输出参数必须定义一个变量给输出参数赋值

 

SELECT * FROM bank
go
CREATE PROC usp_transfer
@from char(4),
@to char(4),
@money money,
@state bit OUTPUT --1表示转账成功,表示转账失败!
AS
BEGIN
    DECLARE @balance money,@sum int=0
    SET @state=0
    --1.查询@from中的余额
    SET @balance=(SELECT balance FROM bank WHERE bank.cId=@from)
    --2.检查余额是否充足
    IF @balance-@money>10 BEGIN  
        ----3.开始事务进行转账
        
        --打开事务
        BEGIN TRANSACTION
            --1.减钱
            UPDATE bank SET balance=bank.balance-@money where bank.cId=@from
            SET @sum=@sum+@@error
            --2.加钱
            UPDATE bank SET balance=bank.balance+@money where bank.cId=@to
            SET @sum=@sum+@@error
            
            IF @sum=0 BEGIN  
                SET @state=1 --设置转账成功
                COMMIT TRAN
            END
            ELSE
                ROLLBACK TRAN
            
    END
END
select * from Customers
--参数:
    --每页大小pagesize
    --当前用户要查看第几页
    --总页数
    --总条数
    go
alter proc  usp_row
@pagenum int,
@pageindex int,
@recordcount int output,
@pagecount int output
     as
     begin
        --1.分页查询语句
        select 
        t.CustomerID,
        t.CompanyName,
        t.City,
        t.[Address],
        t.Phone

        from(select *,rn=ROW_NUMBER()over(Order by CustomerId asc) from Customers) as t
        where  t.rn between @pagenum*(@pageindex-1)+1 and @pagenum*@pageindex
        --2.返回总条数
        set @recordcount=(select COUNT(*) from Customers)
        --3.计算总页数,并返回
        set @pagecount=CEILING(@recordcount*1.0/@pagenum)
    end
    declare @num int
    set @num=3
    declare @num2 int
    set @num2=2
    exec usp_row @pagenum=4,@pageindex=3,@recordcount=@num output,@pagecount=@num2 output
    select @num
    select @num2

 

posted @ 2014-02-26 20:52  我叫小菜  阅读(193)  评论(0编辑  收藏  举报