• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    SQL Server 2008 存储过程示例

    发布者: 雪落无声 | 发布时间: 2025-6-18 12:30| 查看数: 93| 评论数: 0|帖子模式


    1. --有输入参数的存储过程--
    2. create proc GetComment
    3. (@commentid int)
    4. as
    5. select * from Comment where CommentID=@commentid

    6. --有输入与输出参数的存储过程--
    7. create proc GetCommentCount
    8. @newsid int,
    9. @count int output
    10. as
    11. select @count=count(*) from Comment where NewsID=@newsid


    12. --返回单个值的函数--
    13. create function MyFunction
    14. (@newsid int)
    15. returns int
    16. as
    17. begin
    18. declare @count int
    19. select @count=count(*) from Comment where NewsID=@newsid
    20. return @count
    21. end

    22. --调用方法--
    23. declare @count int
    24. exec @count=MyFunction 2
    25. print @count

    26. --返回值为表的函数--
    27. Create function GetFunctionTable
    28. (@newsid int)
    29. returns table
    30. as
    31. return
    32. (select * from Comment where NewsID=@newsid)

    33. --返回值为表的函数的调用--
    34. select * from GetFunctionTable(2)
    复制代码
    SQLServer 存储过程中不拼接SQL字符串实现多条件查询
    1. --以前拼接的写法
    2.   set @sql=' select * from table where 1=1 '
    3.   if (@addDate is not null)
    4.    set @sql = @sql+' and addDate = '+ @addDate + ' '
    5.   if (@name <>'' and is not null)
    6.    set @sql = @sql+ ' and name = ' + @name + ' '
    7.   exec(@sql)
    复制代码
    下面是 不采用拼接SQL字符串实现多条件查询的解决方案
    1.   --第一种写法是 感觉代码有些冗余
    2.   if (@addDate is not null) and (@name <> '')
    3.    select * from table where addDate = @addDate and name = @name
    4.   else if (@addDate is not null) and (@name ='')
    5.    select * from table where addDate = @addDate
    6.   else if(@addDate is null) and (@name <> '')
    7.    select * from table where and name = @name
    8.   else if(@addDate is null) and (@name = '')
    9.   select * from table
    10.   --第二种写法是
    11.   select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')
    12.   --第三种写法是
    13.   SELECT * FROM table where
    14.   addDate = CASE @addDate IS NULL THEN addDate ELSE @addDate END,
    15.   name = CASE @name WHEN '' THEN name ELSE @name END
    复制代码
    SQLSERVER存储过程基本语法
    一、定义变量
    1. --简单赋值
    2. declare @a int
    3. set @a=5
    4. print @a

    5. --使用select语句赋值
    6. declare @user1 nvarchar(50)
    7. select @user1= '张三'
    8. print @user1
    9. declare @user2 nvarchar(50)
    10. select @user2 = Name from ST_User where ID=1
    11. print @user2

    12. --使用update语句赋值
    13. declare @user3 nvarchar(50)
    14. update ST_User set @user3 = Name where ID=1
    15. print @user3
    复制代码
    二、表、临时表、表变量
    1. --创建临时表1
    2. create table #DU_User1
    3. (
    4.    [ID] [ int ]  NOT NULL ,
    5.    [Oid] [ int ] NOT NULL ,
    6.    [Login] [nvarchar](50) NOT NULL ,
    7.    [Rtx] [nvarchar](4) NOT NULL ,
    8.    [ Name ] [nvarchar](5) NOT NULL ,
    9.    [ Password ] [nvarchar]( max ) NULL ,
    10.    [State] [nvarchar](8) NOT NULL
    11. );
    12. --向临时表1插入一条记录
    13. insert into #DU_User1 (ID,Oid,[Login],Rtx, Name ,[ Password ],State) values (100,2, 'LS' , '0000' , '临时' , '321' , '特殊' );

    14. --从ST_User查询数据,填充至新生成的临时表
    15. select * into #DU_User2 from ST_User where ID<8

    16. --查询并联合两临时表
    17. select * from #DU_User2 where ID<3 union select * from #DU_User1

    18. --删除两临时表
    19. drop table #DU_User1
    20. drop table #DU_User2

    21. --创建临时表
    22. CREATE TABLE #t
    23. (
    24.    [ID] [ int ] NOT NULL ,
    25.    [Oid] [ int ] NOT NULL ,
    26.    [Login] [nvarchar](50) NOT NULL ,
    27.    [Rtx] [nvarchar](4) NOT NULL ,
    28.    [ Name ] [nvarchar](5) NOT NULL ,
    29.    [ Password ] [nvarchar]( max ) NULL ,
    30.    [State] [nvarchar](8) NOT NULL ,
    31. )

    32. --将查询结果集(多条数据)插入临时表
    33. insert into #t select * from ST_User
    34. --不能这样插入
    35. --select * into #t from dbo.ST_User

    36. --添加一列,为int型自增长子段
    37. alter table #t add [myid] int NOT NULL IDENTITY(1,1)
    38. --添加一列,默认填充全球唯一标识
    39. alter table #t add [myid1] uniqueidentifier NOT NULL default (newid())

    40. select * from #t
    41. drop table #t
    42. --给查询结果集增加自增长列

    43. --无主键时:
    44. select IDENTITY( int ,1,1) as ID, Name ,[Login],[ Password ] into #t from ST_User
    45. select * from #t

    46. --有主键时:
    47. select ( select SUM (1) from ST_User where ID<= a.ID) as myID,* from ST_User a order by myID
    48. --定义表变量
    49. declare @t table
    50. (
    51.    id int not null ,
    52.    msg nvarchar(50) null
    53. )
    54. insert into @t values (1, '1' )
    55. insert into @t values (2, '2' )
    56. select * from @t
    复制代码
    三、循环
    1. --while循环计算1到100的和
    2. declare @a int
    3. declare @ sum int
    4. set @a=1
    5. set @ sum =0
    6. while @a<=100
    7. begin
    8.    set @ sum +=@a
    9.    set @a+=1
    10. end
    11. print @ sum
    复制代码
    四、条件语句
    1. --if,else条件分支
    2. if(1+1=2)
    3. begin
    4.    print '对'
    5. end
    6. else
    7. begin
    8.    print '错'
    9. end

    10. --when then条件分支
    11. declare @today int
    12. declare @week nvarchar(3)
    13. set @today=3
    14. set @week= case
    15.    when @today=1 then '星期一'
    16.    when @today=2 then '星期二'
    17.    when @today=3 then '星期三'
    18.    when @today=4 then '星期四'
    19.    when @today=5 then '星期五'
    20.    when @today=6 then '星期六'
    21.    when @today=7 then '星期日'
    22.    else '值错误'
    23. end
    24. print @week
    复制代码
    五、游标
    1. declare @ID int
    2. declare @Oid int
    3. declare @Login varchar (50)

    4. --定义一个游标
    5. declare user_cur cursor for select ID,Oid,[Login] from ST_User
    6. --打开游标
    7. open user_cur
    8. while @@fetch_status=0
    9. begin
    10. --读取游标
    11.    fetch next from user_cur into @ID,@Oid,@Login
    12.    print @ID
    13.    --print @Login
    14. end
    15. close user_cur
    16. --摧毁游标
    17. deallocate user_cur
    复制代码
    五、游标
    1. declare @ID int
    2. declare @Oid int
    3. declare @Login varchar (50)

    4. --定义一个游标
    5. declare user_cur cursor for select ID,Oid,[Login] from ST_User
    6. --打开游标
    7. open user_cur
    8. while @@fetch_status=0
    9. begin
    10. --读取游标
    11.    fetch next from user_cur into @ID,@Oid,@Login
    12.    print @ID
    13.    --print @Login
    14. end
    15. close user_cur
    16. --摧毁游标
    17. deallocate user_cur
    复制代码
    六、触发器
      触发器中的临时表:
      Inserted
      存放进行insert和update 操作后的数据
      Deleted
      存放进行delete 和update操作前的数据
    1. --创建触发器
    2. Create trigger User_OnUpdate
    3.    On ST_User
    4.    for Update
    5. As
    6.    declare @msg nvarchar(50)
    7.    --@msg记录修改情况
    8.    select @msg = N '姓名从“' + Deleted. Name + N '”修改为“' + Inserted. Name + '”' from Inserted,Deleted
    9.    --插入日志表
    10.    insert into [LOG](MSG) values (@msg)
    11.    
    12. --删除触发器
    13. drop trigger User_OnUpdate
    复制代码
    七、存储过程
    1. --创建带output参数的存储过程
    2. CREATE PROCEDURE PR_Sum
    3.    @a int ,
    4.    @b int ,
    5.    @ sum int output
    6. AS
    7. BEGIN
    8.    set @ sum =@a+@b
    9. END

    10. --创建Return返回值存储过程
    11. CREATE PROCEDURE PR_Sum2
    12.    @a int ,
    13.    @b int
    14. AS
    15. BEGIN
    16.    Return @a+@b
    17. END
    18.    
    19. --执行存储过程获取output型返回值
    20. declare @mysum int
    21. execute PR_Sum 1,2,@mysum output
    22. print @mysum

    23. --执行存储过程获取Return型返回值
    24. declare @mysum2 int
    25. execute @mysum2= PR_Sum2 1,2
    26. print @mysum2
    复制代码
    八、自定义函数
      函数的分类:
        1)标量值函数
        2)表值函数
            a:内联表值函数
            b:多语句表值函数
        3)系统函数
    1. --新建标量值函数
    2. create function FUNC_Sum1
    3. (
    4.    @a int ,
    5.    @b int
    6. )
    7. returns int
    8. as
    9. begin
    10.    return @a+@b
    11. end

    12. --新建内联表值函数
    13. create function FUNC_UserTab_1
    14. (
    15.    @myId int
    16. )
    17. returns table
    18. as
    19. return ( select * from ST_User where ID<@myId)

    20. --新建多语句表值函数
    21. create function FUNC_UserTab_2
    22. (
    23.    @myId int
    24. )
    25. returns @t table
    26. (
    27.    [ID] [ int ] NOT NULL ,
    28.    [Oid] [ int ] NOT NULL ,
    29.    [Login] [nvarchar](50) NOT NULL ,
    30.    [Rtx] [nvarchar](4) NOT NULL ,
    31.    [ Name ] [nvarchar](5) NOT NULL ,
    32.    [ Password ] [nvarchar]( max ) NULL ,
    33.    [State] [nvarchar](8) NOT NULL
    34. )
    35. as
    36. begin
    37.    insert into @t select * from ST_User where ID<@myId
    38.    return
    39. end

    40. --调用表值函数
    41. select * from dbo.FUNC_UserTab_1(15)
    42. --调用标量值函数
    43. declare @s int
    44. set @s=dbo.FUNC_Sum1(100,50)
    45. print @s

    46. --删除标量值函数
    47. drop function FUNC_Sum1
    复制代码
    来源:https://www.jb51.net/article/54730.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表