CREATE function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
return substring(@str,@start,@location-@start)
end
运行效果:
调用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9
mysql中要获得存储过程的返回值,可以增加一个out参数,用来返回。
mysql中存储过程的例子:
CREATE PROCEDURE addvoucher (
IN userid INT,
IN voucherid INT,
OUT result INT
)
BEGIN
SELECT
@endate_a := endate ,@batch_a := batch ,@c_count_a := c_count,
@isdead_a := isdead
FROM
t_voucher
WHERE
id = voucherid
SET autocommit = 0
IF EXISTS (
SELECT
*
FROM
t_user_voucher tuv,
t_voucher tv
WHERE
tv.id = tuv.voucherid
AND tv.batch =@batch_a
) THEN
SET result = 1-- 已存在
SELECT
result
ELSE
IF @c_count_a >0 THEN
IF (
TO_DAYS(@endate_a) - TO_DAYS(NOW())
) >0 THEN
IF @isdead_a = 1 THEN
INSERT INTO t_user_voucher (userid, voucherid, isdead)
VALUES
(userid, voucherid, 1)
UPDATE t_voucher SET c_count = c_count-1 where id = voucherid
SET result = 0-- 成功
END
下面是调用并返回结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)