获取格式字符串第idx个值及实例
–根据索引idx值获取格式串中第idx个值 如数据\’11,12,13,14,15,16\’
方法:格式串+分隔符;@str=\’11,12,13,14,15,16\’+\’,\’
select dbo.GetStrByindex(\’11,12,13,14,15,16,\’, \’,\’, 3); — 13
create function [dbo].[GetStrByindex](@str varchar(8000),@split varchar(10),@idx int)
–@str:待查找字符串
returns varchar(100)
as
begin
declare @inx int
set @inx=0
WHILE(CHARINDEX(@split,@str)<>0)
begin
SET @INX=@INX+1
if @inx=@idx
–第一个@split之前的字符串
return SUBSTRING(@str,1,CHARINDEX(@split,@str)-1)
–将第一个@split后面的字符串重新赋给@str
SET @str=STUFF(@str,1,CHARINDEX(@split,@str),\’\’)
end
return \’\’;
end
GO
此过程可能在数据维护时,偶尔会遇到。
/* 参数1:@Type 整型 0:新增1:修改2:删除 参数2:@Weld_id 字符串 主表GuID 参数3:@Param 字符串,需严格按照如下格式组织(字段内容中不包含字符“|”) 民工|日期|方法|层次|电流|电压|标识号1|牌号1|规格1|标识号2|牌号2|规格2 参数4:@ID 整型 子表唯一字典 返回值 -1: 插入成功; 0:更新成功; 1:参数@Param格式错误; 2:日期格式错误; 3:更新失败; 4: 插入失败; 5:删除成功 SQL调用实例 */ if (exists (select * from sys.objects where name = \'up_OperateWeld_Container\')) drop proc up_OperateWeld_Container go Create PROC [dbo].[up_OperateWeld_Container] ( @Type int, --0: 新增 1:修改 2:删除 @Weld_id varchar(100), --主表guid @Param nvarchar(4000), @ID int = -1 --新增无用。修改,删除时需传入该ID,定位修改删除信息 ) as --判断参数是否正常 if len(@Param+\'|\')-len(replace(@Param+\'|\', \'|\', \'\'))<>12 return 1; --参数2格式错误 --解析参数 declare @SQL varchar(8000), @Count int, @Param1 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 1), @Param2 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 2), --日期信息 @Param3 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 3), @Param4 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 4), @Param5 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 5), @Param6 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 6), @Param7 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 7), @Param8 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 8), @Param9 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 9), @Param10 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 10), @Param11 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 11), @Param12 varchar(50) = dbo.GetStrByindex(@Param+\'|\', \'|\', 12); if isdate(@Param2)<>1 return 2 --日期无效 if @Type=0 begin --不存在则插入 begin try begin tran --插入焊工操作 insert into info_detail(Weld_id, col1, date, col3,col4,col5,col6,col7,col8,col9,col10,col11,col12) values (@Weld_id,@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7,@Param8, @Param9,@Param10,@Param11,@Param12); commit tran return -1 --插入成功 end try begin catch rollback; return 4 --插入失败 end catch end else if @Type=1 begin begin try begin tran --更新焊工操作 update info_detail set col1 = @Param1, date = @Param2, col3 = @Param3, col4 = @Param4, col5 = @Param5, col6 = @Param6, col7 = @Param7, col8 = @Param8, col9 = @Param9, col10 = @Param10, col11 = @Param11, col12 = @Param12 where ID=@ID; commit tran return 0 --更新成功 end try begin catch rollback; return 3 --更新失败 end catch; end else begin --删除成功 delete from info_detail where ID=@ID; return 5; end; GO /* 调用实例 --新增 declare @idx int exec @idx=up_OperateWeld_Container 0, \'b8f8964a-3e0e-47ce-8302-097c68bd7033\', \'100|2019-09-12|SAW|层次|电流|电压|标识号1|牌号1|规格1|标识号2|牌号2|规格2\', -1 select @idx --修改 declare @idx int exec @idx=up_OperateWeld_Container 1, \'b8f8964a-3e0e-47ce-8302-097c68bd7033\', \'100|2019-09-12|SAW|层次|电流|电压|标识号1|牌号1|规格1|adfdf|牌号2|规格2\', 49217 select @idx --删除 declare @idx int exec @idx=up_OperateWeld_Container 2, \'b8f8964a-3e0e-47ce-8302-097c68bd7033\', \'100|2019-09-12|方法|层次|电流|电压|标识号1|牌号1|规格1|标识号2|牌号2|规格2\', 49217 select @idx */