MySQL 数据库

这篇博客详细介绍了MySQL数据库中的存储过程和函数的创建与使用,包括有输入参数的存储过程、带有输入与输出参数的存储过程、返回单个值的函数以及返回表的函数。还探讨了如何避免在SQL Server中拼接SQL字符串实现多条件查询,并展示了不同的实现方式。此外,文章还涵盖了SQLServer存储过程的基本语法,如变量声明、表和临时表的使用、循环、条件语句、游标、触发器和存储过程的创建及执行。最后,文章讲解了自定义函数的分类和创建,包括标量值函数和表值函数的实例。
摘要由CSDN通过智能技术生成

数据库

–有输入参数的存储过程–
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid

–有输入与输出参数的存储过程–
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid

–返回单个值的函数–
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end

–调用方法–
declare @count int
exec @count=MyFunction 2
print @count

–返回值为表的函数–
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(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 (@

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值