zoukankan      html  css  js  c++  java
  • 创建存储过程

    UserAccount

    UserID

    UserName

    PassWord

    RegisterTime

    RegisterIP

    12

    6                   

    6                   

    2012-12-31

    6

    18

    5                   

    5                   

    2013-01-01

    5

    19

    1                   

    1                   

    2013-01-01

    1

    20

    2                   

    2                   

    2013-01-01

    2

    21

    3                   

    3                   

    2013-01-01

    3

    22

    4                   

    4                   

    2013-01-01

    4

    23

    5                   

    5                   

    2013-01-01

    5

    25

    7                   

    7                   

    2013-01-01

    7

    26

    8                   

    8                   

    2013-01-01

    8

    NULL

    NULL

    NULL

    NULL

    NULL

    针对上面的表,我使用存储过程对它做一些操作:

    1. 只返回单一记录集的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountasselect * from UserAccountgo-------------执行上面的存储过程----------------exec GetUserAccount

     结果:相当于运行 select * from UserAccount 这行代码,结果为整个表的数据。

    2.没有输入输出的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure inUserAccountasinsert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9)go-------------执行上面的存储过程----------------exec inUserAccount

     结果:相当于运行 insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9) 这行代码。

    3.有返回值的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure inUserAccountReasinsert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(10,10,'2013-01-02',10)return @@rowcountgo-------------执行上面的存储过程----------------exec inUserAccountRe

     解释:这里的@@rowcount为执行存储过程影响的行数,执行的结果是不仅插入了一条数据,还返回了一个值即 return value =1  ,这个可以在程序中获取,稍后在c#调用存储过程中会有说到。

    4.有输入参数和输出参数的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe@UserName nchar(20),@UserID int outputasif(@UserName>5)select @UserID=COUNT(*) from UserAccount where UserID>25elseset @UserID=1000go-------------执行上面的存储过程----------------exec GetUserAccountRe '7',null

     解释:@UserName为输入参数,@UserID为输出参数。 运行结果为@userIDCOOUT*)即 =1

    5. 同时具有返回值、输入参数、输出参数的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe1@UserName nchar(20),@UserID int outputasif(@UserName>5)select @UserID=COUNT(*) from UserAccount where UserID>25elseset @UserID=1000return @@rowcountgo-------------执行上面的存储过程----------------exec GetUserAccountRe1 '7',null

     结果:@userIDCOOUT*)即 =1Retun Value=1

    6.同时返回参数和记录集的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe2@UserName nchar(20),@UserID int outputasif(@UserName>5)select @UserID=COUNT(*) from UserAccount where UserID>25elseset @UserID=1000select * from UserAccountreturn @@rowcountgo-------------执行上面的存储过程----------------exec GetUserAccountRe2 '7',null

     结果:返回执行 select * from UserAccount 这句代码的结果集,同时@userIDCOOUT*)即 =1Retun Value=9 

    7.返回多个记录集的存储过程 

    -------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe3asselect * from UserAccountselect * from UserAccount where UserID>5go-------------执行上面的存储过程----------------exec GetUserAccountRe3

     结果:返回两个结果集,一个为 select * from UserAccount,另一个为 select * from UserAccount where UserID>5 

    小结:上面我们创建了各式的存储过程,下面看我们在c#中怎样调用这些存储过程。

     

  • 相关阅读:
    常见字符编码扫盲(UTF,Unicode, GB2312) 四
    Ogre 实用技巧 四
    CEGUI中文显示问题的解决方法 四
    大幅革新 AMD下一代图形产品前瞻 四
    力争上游 ——我眼中的“计算机产业链” 四
    养成 SQL SERVER 的好习惯 四
    说说 Windows 中的中文字体 四
    Unicode字符集和多字节字符集关系 四
    各种电影 四
    [projectEuler.net]12
  • 原文地址:https://www.cnblogs.com/lizihong/p/4318316.html
Copyright © 2011-2022 走看看