侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

SQL Server 2008 存储过程示例

数据库  /  管理员 发布于 5年前   224

 --有输入参数的存储过程--create proc GetComment(@commentid int)asselect * from Comment where CommentID=@commentid --有输入与输出参数的存储过程--create proc GetCommentCount@newsid int,@count int outputasselect @count=count(*) from Comment where NewsID=@newsid  --返回单个值的函数--create function MyFunction(@newsid int)returns intasbegindeclare @count intselect @count=count(*) from Comment where NewsID=@newsidreturn @countend --调用方法--declare @count intexec @count=MyFunction 2print @count --返回值为表的函数--Create function GetFunctionTable(@newsid int)returns tableasreturn(select * from Comment where NewsID=@newsid) --返回值为表的函数的调用--select * from GetFunctionTable(2)

SQLServer 存储过程中不拼接SQL字符串实现多条件查询

--以前拼接的写法  set @sql=' select * from table where 1=1 '  if (@addDate is not null)   set @sql = @sql+' and addDate = '+ @addDate + ' '  if (@name <>'' and is not null)   set @sql = @sql+ ' and name = ' + @name + ' '  exec(@sql)

下面是 不采用拼接SQL字符串实现多条件查询的解决方案

  --第一种写法是 感觉代码有些冗余  if (@addDate is not null) and (@name <> '')   select * from table where addDate = @addDate and name = @name  else if (@addDate is not null) and (@name ='')   select * from table where addDate = @addDate  else if(@addDate is null) and (@name <> '')   select * from table where and name = @name  else if(@addDate is null) and (@name = '')  select * from table  --第二种写法是  select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')  --第三种写法是  SELECT * FROM table where  addDate = CASE @addDate IS NULL THEN addDate ELSE @addDate END,  name = CASE @name WHEN '' THEN name ELSE @name END

SQLSERVER存储过程基本语法

一、定义变量

--简单赋值declare @a intset @a=5print @a --使用select语句赋值declare @user1 nvarchar(50)select @user1= '张三'print @user1declare @user2 nvarchar(50)select @user2 = Name from ST_User where ID=1print @user2 --使用update语句赋值declare @user3 nvarchar(50)update ST_User set @user3 = Name where ID=1print @user3 

二、表、临时表、表变量

--创建临时表1create table #DU_User1(   [ID] [ int ]  NOT NULL ,   [Oid] [ int ] NOT NULL ,   [Login] [nvarchar](50) NOT NULL ,   [Rtx] [nvarchar](4) NOT NULL ,   [ Name ] [nvarchar](5) NOT NULL ,   [ Password ] [nvarchar]( max ) NULL ,   [State] [nvarchar](8) NOT NULL);--向临时表1插入一条记录insert into #DU_User1 (ID,Oid,[Login],Rtx, Name ,[ Password ],State) values (100,2, 'LS' , '0000' , '临时' , '321' , '特殊' ); --从ST_User查询数据,填充至新生成的临时表select * into #DU_User2 from ST_User where ID<8 --查询并联合两临时表select * from #DU_User2 where ID<3 union select * from #DU_User1 --删除两临时表drop table #DU_User1drop table #DU_User2 --创建临时表CREATE TABLE #t(   [ID] [ int ] NOT NULL ,   [Oid] [ int ] NOT NULL ,   [Login] [nvarchar](50) NOT NULL ,   [Rtx] [nvarchar](4) NOT NULL ,   [ Name ] [nvarchar](5) NOT NULL ,   [ Password ] [nvarchar]( max ) NULL ,   [State] [nvarchar](8) NOT NULL ,) --将查询结果集(多条数据)插入临时表insert into #t select * from ST_User--不能这样插入--select * into #t from dbo.ST_User --添加一列,为int型自增长子段alter table #t add [myid] int NOT NULL IDENTITY(1,1)--添加一列,默认填充全球唯一标识alter table #t add [myid1] uniqueidentifier NOT NULL default (newid()) select * from #tdrop table #t--给查询结果集增加自增长列 --无主键时:select IDENTITY( int ,1,1) as ID, Name ,[Login],[ Password ] into #t from ST_Userselect * from #t --有主键时:select ( select SUM (1) from ST_User where ID<= a.ID) as myID,* from ST_User a order by myID--定义表变量declare @t table(   id int not null ,   msg nvarchar(50) null)insert into @t values (1, '1' )insert into @t values (2, '2' )select * from @t

三、循环

--while循环计算1到100的和declare @a intdeclare @ sum intset @a=1set @ sum =0while @a<=100begin   set @ sum +=@a   set @a+=1endprint @ sum

四、条件语句

--if,else条件分支if(1+1=2)begin   print '对'endelsebegin   print '错'end --when then条件分支declare @today intdeclare @week nvarchar(3)set @today=3set @week= case   when @today=1 then '星期一'   when @today=2 then '星期二'   when @today=3 then '星期三'   when @today=4 then '星期四'   when @today=5 then '星期五'   when @today=6 then '星期六'   when @today=7 then '星期日'   else '值错误'endprint @week 

五、游标

declare @ID intdeclare @Oid intdeclare @Login varchar (50) --定义一个游标declare user_cur cursor for select ID,Oid,[Login] from ST_User--打开游标open user_curwhile @@fetch_status=0begin--读取游标   fetch next from user_cur into @ID,@Oid,@Login   print @ID   --print @Loginendclose user_cur--摧毁游标deallocate user_cur

五、游标

declare @ID intdeclare @Oid intdeclare @Login varchar (50) --定义一个游标declare user_cur cursor for select ID,Oid,[Login] from ST_User--打开游标open user_curwhile @@fetch_status=0begin--读取游标   fetch next from user_cur into @ID,@Oid,@Login   print @ID   --print @Loginendclose user_cur--摧毁游标deallocate user_cur

六、触发器

  触发器中的临时表:
  Inserted
  存放进行insert和update 操作后的数据
  Deleted
  存放进行delete 和update操作前的数据

--创建触发器Create trigger User_OnUpdate    On ST_User    for Update As    declare @msg nvarchar(50)   --@msg记录修改情况   select @msg = N '姓名从“' + Deleted. Name + N '”修改为“' + Inserted. Name + '”' from Inserted,Deleted   --插入日志表   insert into [LOG](MSG) values (@msg)   --删除触发器drop trigger User_OnUpdate

七、存储过程

--创建带output参数的存储过程CREATE PROCEDURE PR_Sum   @a int ,   @b int ,   @ sum int outputASBEGIN   set @ sum =@a+@bEND --创建Return返回值存储过程CREATE PROCEDURE PR_Sum2   @a int ,   @b intASBEGIN   Return @a+@bEND   --执行存储过程获取output型返回值declare @mysum intexecute PR_Sum 1,2,@mysum outputprint @mysum --执行存储过程获取Return型返回值declare @mysum2 intexecute @mysum2= PR_Sum2 1,2print @mysum2

八、自定义函数

  函数的分类:
    1)标量值函数
    2)表值函数
        a:内联表值函数
        b:多语句表值函数
    3)系统函数

--新建标量值函数create function FUNC_Sum1(   @a int ,   @b int)returns intasbegin   return @a+@bend --新建内联表值函数create function FUNC_UserTab_1(   @myId int)returns tableasreturn ( select * from ST_User where ID<@myId) --新建多语句表值函数create function FUNC_UserTab_2(   @myId int)returns @t table(   [ID] [ int ] NOT NULL ,   [Oid] [ int ] NOT NULL ,   [Login] [nvarchar](50) NOT NULL ,   [Rtx] [nvarchar](4) NOT NULL ,   [ Name ] [nvarchar](5) NOT NULL ,   [ Password ] [nvarchar]( max ) NULL ,   [State] [nvarchar](8) NOT NULL)asbegin   insert into @t select * from ST_User where ID<@myId   returnend --调用表值函数select * from dbo.FUNC_UserTab_1(15)--调用标量值函数declare @s intset @s=dbo.FUNC_Sum1(100,50)print @s --删除标量值函数drop function FUNC_Sum1


  • 上一条:
    安装sql server 2008时的4个常见错误和解决方法
    下一条:
    SQL Server 2008中的数据表压缩功能详细介绍
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客