SQL Server 2008 存储过程示例

  • 格式:doc
  • 大小:24.00 KB
  • 文档页数:3

下载文档原格式

  / 6
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

--有输入参数的存储过程-- 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=@newsid return @countend --调用方法-- declare @count intexec @count=MyFunction 2 print @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 = @nameelse 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 = @nameelse 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 whereaddDate = CASE @addDate IS NULL THEN addDate ELSE @addDate END, name = CASE @name WHEN '' THEN name ELSE @name END

SQLSERVER存储过程基本语法

一、定义变量

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

二、表、临时表、表变量

--创建临时表1 create 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_User1 drop 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