Home > Database > Mysql Tutorial > sql数据库表字段分割字符串,查找指定位置批量替换方法

sql数据库表字段分割字符串,查找指定位置批量替换方法

WBOY
Release: 2016-06-07 14:57:26
Original
1942 people have browsed it

/*============================================================================== * *Filename:TB_UpdateList.sql *Description:sql数据库字段分割字符指定位置批量替换,引用f_split *Version:1.0 *Created:2014.03.12 *Author:liangjw *E-mail:liangjw0

/*==============================================================================
*
* Filename: TB_UpdateList.sql
* Description: sql数据库字段分割字符指定位置批量替换,引用 f_split
* Version: 1.0
* Created: 2014.03.12
* Author : liangjw
* E-mail : liangjw0504@163.com
* Q   Q  : 592568532
* Profile Url:http://90ideas.net/
* Company: Copyright (C) Create Family Wealth Power By Peter
*
==============================================================================*/
 * 备注信息: 上传部分自己总结的常用方法的封装,有不足和不完美之处,希望大家指出来,愿意一起
 * 主要研究erp,cms,crm,b2b,oa等系统和网站的开发,欢迎有共同追求和学的IT人员一起学习和交流。
 * 学习和讨论有关asp.net  mvc ,Ajax ,jquery ,html/css, xml,sqlserver ,wpf,IIS以及服务器的搭建和安全性相关技术的交流和学习。
/****** Object:  UserDefinedFunction [dbo].[f_split1]    Script Date: 12/14/2010 21:45:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--测试数据信息
-- SELECT * FROM dbo.[f_split1] ('21|80|102|92|38|0|0|0|102|0|0|0|0','|')

ALTER function [dbo].[f_split1](@c   varchar(2000),@split   varchar(2))--@c表示字符串,@split表示分隔符   
returns @t table(id INT,col varchar(20)) ----返回一个表,用这个表来存储分割出来的所有数据  
as   
    begin   
    DECLARE @tmpid int=1
      while(charindex(@split,@c)<>0)----当分隔符的索引不等于0时  
        begin   
          insert @t(id,col) values (@tmpid,substring(@c,1,charindex(@split,@c)-1))----分割出来的单个字符   
             set @c = stuff(@c,1,charindex(@split,@c),'')----设置分割后的字符串   
              SET @tmpid=@tmpid+1
        end   
      insert @t(id,col) values (@tmpid,@c)  ----把分割出来的单个字符插入到表@t表
      return   
END
--------------------------------------------------------------------------
--------------------------------------------------------------------------
 
/****** Object:  StoredProcedure [dbo].[TB_UpdateList]   Script Date: 03/12/2014 17:19:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--更改指定字符串中,指定位置的数据信息。 
--SELECT * FROM dbo.[f_split1] ('21|80|102|92|38|0|0|0|102|0|0|0|0','|')
ALTER PROCEDURE [dbo].[TB_UpdateList] 
AS 
  --判断临时表数据是否存在,如果存在则删除临时表
     if OBJECT_ID('tempdb..##project') is not null
		DROP TABLE ##project  
     if OBJECT_ID('tempdb..##projectItem') is not null
		DROP TABLE ##projectItem
		
	--获取数据信息
	SELECT id, SizeSum  INTO ##project FROM dbo.TB_MakeTrimPlan WHERE Size9>0  
    --定义变量
    DECLARE @tmpid INT, @SizeSum NVARCHAR(500),@tmplid INT,@SizeLSum NVARCHAR(500)
    DECLARE @sql NVARCHAR(max),@listvalue NVARCHAR(500),@getF NVARCHAR(500),@getd NVARCHAR(500)
    
    --创建临时表
    CREATE  TABLE ##projectItem(
    id INT ,
    col NVARCHAR(50)
    )
    --查询数据信息
	SELECT @tmpid = MIN(id) FROM ##project
		WHILE @tmpid IS NOT NULL BEGIN
	         SELECT @SizeSum = SizeSum  FROM ##project WHERE id =@tmpid
	          
              INSERT INTO ##projectItem  SELECT * FROM dbo.[f_split1](@SizeSum,'|') 
                -- DROP TABLE ##projectItem
                SELECT  @tmplid=MIN(id) FROM ##projectItem
                --定义变量
                DECLARE @Ttotallistvalue NVARCHAR(500),@tmpq NVARCHAR(500)
                       SET @Ttotallistvalue=''
                       SET @tmpq=''
					WHILE @tmplid IS NOT NULL BEGIN
					
					    --查询数据信息
					     SELECT @listvalue =col  FROM ##projectItem WHERE id = @tmplid
					       --取出数据信息
					       SET @tmpq = @tmpq + @listvalue+'|'    
					       IF @tmplid=9 BEGIN  --分割后第九个,可以自定义位数
					             SET @listvalue= 0  
	                         END  
	                     --如果是指定位置就去修改数值信息
					     SET @Ttotallistvalue= @Ttotallistvalue + @listvalue+'|'   
					      	--找出最小值依次循环
					        SELECT @tmplid = MIN(id) FROM ##projectItem WHERE id > @tmplid 
					    END
					
					--去除最后一个字符
					SET @getF=left(@Ttotallistvalue,len(@Ttotallistvalue)-1)
					SET @getD=left(@tmpq,len(@tmpq)-1)
				    --更新数据信息
					SET @sql ='update TB_MakeTrimPlan set SizeSum= '''
					           +@getF+''' where SizeSum='''+@getD+''''  
                    EXEC(@sql)
               
             SELECT @tmpid = MIN(id) FROM dbo.##project WHERE id >@tmpid 
        END                                                                                
    --判断临时表数据是否存在,如果存在则删除临时表
	if OBJECT_ID('tempdb..##project') is not null
		DROP TABLE ##project  
     if OBJECT_ID('tempdb..##projectItem') is not null
		DROP TABLE ##projectItem

Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template