--根据索引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
*/

获取格式字符串第idx个值及实例的更多相关文章

  1. Oracle数据库 获取CLOB字段存储的xml格式字符串指定节点的值

    参照: Oracle存储过程中使用游标来批量解析CLOB字段里面的xml字符串 背景:在写存储过程时,需要获取表单提交的信息.表单信息是以xml格式的字符串存储在colb类型的字段dataxml中,如 ...

  2. c# json转换成dynamic对象,然后在dynamic对象中动态获取指定字符串列表中的值

    using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.T ...

  3. C#获取Json字符串中的某个值

    问题描述: json数据格式{"resCode":0,"resMag":"aaa","data":[{"par ...

  4. 获取xml字符串中的属性值

    pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...

  5. JQuery设置获取下拉菜单选项的值 多实例

    分享下JQuery如何设置获取下拉菜单某个选项的值,多种方法,值得收藏. JQuery获取和设置Select选项 获取Select :获取select 选中的 text :$(“#ddlRegType ...

  6. 获取Json数据某节点的值

    时间匆忙,直接上代码,回家还得做清蒸鱼呢! #region 获取Json字符串某节点的值 /// <summary> /// 获取Json字符串某节点的值 /// </summary ...

  7. 获取字符串对应的MD5值 (AL16UTF16LE)

    CREATE OR REPLACE FUNCTION fn_md5_utf16le (InputString IN VARCHAR2) RETURN VARCHAR2 IS retval ); /** ...

  8. .NET获取Html字符串中指定标签的指定属性的值

    using System.Text; using System.Text.RegularExpressions; //以上为要用到的命名空间 /// <summary> /// 获取Htm ...

  9. JS如何获取url查询字符串的键和值?

    /** * 根据url查询字符串里的键名获取其值 */function getSearchString(key, search) { // 获取URL中?之后的字符 var str = search; ...

随机推荐

  1. Bash脚本编程之数组

    数组简介 在bash脚本编程当中,变量是存储单个元素的内存空间:而数组是存储多个元素的一段连续的内存空间. 数组由数组名和下标构成,如下. ARRAY_NAME[SUBSCRIPT] 数组按照下标的类 ...

  2. GetPrivateProfileString() 当 key 包含空格时,需要进行转义

    使用 GetPrivateProfileString() 方法可以方便的读取 ini 格式文件中的内容,如: [section] tommy = worker 使用 C# 读取如下: 1. 先引入 G ...

  3. python 打飞机项目 ( 基类封装 )

    项目代码 | plane # -*- coding:utf-8 -*- import pygame, time from Plane import Plane from HeroPlane impor ...

  4. 计算机组成原理——I/O接口以及I/O设备数据传送控制方式

    接口可以看作是两个部件之间交接的部分.硬件与硬件之间有接口,硬件与软件之间有接口,软件与软件之间也有接口. 这里我们所说的I/O接口,一边连接着主机,一边连接着外设. I/O接口的功能 I/O接口的基 ...

  5. php 将科学计算法得出的结果转换成原始数据 NumToStr

    由于php最大只支持显示 15位因的数据运算,大于15位的2数加减乘除的数据的结果,会直接用科学计数法显示, 但在现实生活中,科学计数法不利于普通人识别,所以,本函数将:科学计数法的出的结果转换成原始 ...

  6. ES-索引管理

    参考: https://es.xiaoleilu.com/070_Index_Mgmt/00_Intro.html 创建索引 PUT /new_index 创建更多详细设置的索引: 删除索引 DELT ...

  7. How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象

    When designing business classes, a common task is to ensure that a newly created business object is ...

  8. Dynamics 365客户端编程示例:获取当前用户的信息,表单级通知/提示,表单OnLoad事件执行代码

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  9. TCP 三次握手与四次挥手

    TCP是什么      TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的.可靠的. 基于IP的传输层协议.       TCP有6种标示: ...

  10. Redis和MongoDB区别

    MongoDB 更类似 MySQL,支持字段索引.游标操作,其优势在于查询功能比较强大,擅长查询 JSON 数据,能存储海量数据,但是不支持事务.Redis 是一个开源(BSD许可)的,内存中的数据结 ...