字符串分割后的值,把表中不存在的插入表中 --供大家参考

使用场景,自行思考……

--创建表tb1
Create table tb1
(
cola int,
colb varchar(50)
)
--插入数据
insert into tb1(cola,colb)
select 1, 'A' union all
select 2, 'B' union all
select 3, 'C';
--存储过程
Create proc sp_tbTest
@sid int,--ID
@str varchar(20)--A,B,C,D,G
AS
BEGIN
insert into tb1(cola,colb) select @sid ,sp from [dbo].[split](@str,',')
where sp not in (select colb from tb1 where cola=@sid)
END
exec sp_tbTest 4,'D,G,A,B,C';--表中已近存在了A,B,C,执行存储过程的话,本次插入的是D,G
select * from tb1

--实现分割的函数

ALTER function [dbo].[split](@SourceSql varchar(8000),@Code varchar(10))
returns @temp table(sp varchar(1000))
--实现split功能 的函数
--date :2007-7-10
--Author :sp
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@Code,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@Code,@SourceSql)
end
if @SourceSql<>'/'
insert @temp values(@SourceSql)
return
end

--以下是朋友Lewis写的案例;没有用到自定义的split函数,而是直接在存储过程中分割字符串的。

create table tb_test(shop varchar(10))
insert tb_test values('a') alter PROCEDURE [dbo].sp_TEST
@strShopID varchar(1000)=''
AS
BEGIN
SET NOCOUNT ON;
declare @tbShop table(shopid varchar(32))
--declare @tbTopShop table(shopid varchar(32),Num int)
set @strShopID=@strShopID+','
while(len(@strShopID)>1)
begin
if left(@strShopID,1)=','
set @strShopID=substring(@strShopID,2,len(@strShopID))
insert @tbShop
select substring(@strShopID,1,charindex( ',',@strShopID)-1)
set @strShopID=substring(@strShopID,charindex( ',',@strShopID),len(@strShopID)) end
insert tb_test
select * from @tbshop where shopid not in(select * from tb_test)
END
sp_TEST 'a,b,c,d,e,f'
select * from tb_test

博客:http://www.haoyuncn.net/sql-split-insert

mssql字符串分割后的值,把表中不存在的插入表中的更多相关文章

  1. SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)

    1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地 ...

  2. mysql substring_index()查询某个字符中以某个分割符分割后的值

    substring_index(某个字段,以其分割,第几个分割点之前的值); +---------------------------------------------------------+ | ...

  3. MSSQL字符串分割

    CREATE FUNCTION dbo.f_splitstr( @str varchar(8000) )RETURNS @r TABLE(id int IDENTITY(1, 1), value va ...

  4. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

  5. c语言字符串分割函数(转)

    源:C语言实现split以某个字符分割一个字符串 void split(char *src, const char *separator, char **dest, int *num) { /* sr ...

  6. 【Linux】shell字符串分割、数组访问、条件判断

    参考资料: shell字符串分割再循环:http://www.shangxueba.com/jingyan/1633455.html linux shell中 if else以及大于.小于.等于逻辑表 ...

  7. C# 批量插入表SQLSERVER SqlBulkCopy往数据库中批量插入数据

    #region 帮助实例:SQL 批量插入数据 多种方法 /// <summary> /// SqlBulkCopy往数据库中批量插入数据 /// </summary> /// ...

  8. SQL 字符串分割表函数

    --字符串分割表函数 ) ) declare @i int; declare @count int; ); ); declare @Index int; )) declare @rowID int; ...

  9. PCB MS SQL表值函数与CLR 表值函数 (例:字符串分割转表)

    将字符串分割为表表经常用到,这里 SQL表值函数与CLR  表值函数,两种实现方法例出来如下: SELECT * FROM FP_EMSDB_PUB.dbo.SqlSplit('/','1oz/1.5 ...

随机推荐

  1. _00023 Kafka 奇怪的操作_001它们的定义Encoder达到Class数据传输水平和决心

    博文作者:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 博文标题:_00023 Kafka 诡异操作_001自己定义Encoder实现Class级别的数据传送 ...

  2. i = i++ 在java字节码层面的分析

    有这么一段代码: package zl.test; public class PcodeTest { /** * @param args */ public static void main(Stri ...

  3. JAVA读取propertise配置文件

    java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"key=value"的格式,在pr ...

  4. 使用PowerDesigner创建数据库表图文并茂版

    使用PowerDesigner创建数据库表图文并茂版 使用PowerDesigner 建数据库表. 一直很忙,没有时间写东西.这次搞点会声会色的,嘿嘿 此技能为项目经理必备技能. 本次主角: 1.在w ...

  5. gtest的Linux使用(Google test)

    GTest是Google开发的跨平台而且开源的C++单元测试框架,很好很强大. 下载地址:https://code.google.com/p/googletest/ . 关于GTest在Windows ...

  6. Linux学习之(())操作符

    在刚开始学习inux shell脚本编程时候,对于它的 四则运算以及逻辑运算.估计很多朋友都感觉比较难以接受.特变逻辑运算符”[]”使用时候,必须保证运算符与算数 之间有空格. 四则运算也只能借助:l ...

  7. JavaWeb核心编程之(三.3)Servlet Init 配置

    Servlet初始化 可以传入一些参数 通过 <init-param>来配置 新建 servletinit项目 新建包 com.xiaoan.test->new Class(Test ...

  8. mysql建立数据库的方法

    mysql建立数据库的方法 方法一:使用create mysql> create database roudy; Query OK, 1 row affected (0.00 sec) mysq ...

  9. jQuery中的类型判断

    在JQuery中有一个type方法,在1.11.2中是这样写的 var class2type = {}; var toString = class2type.toString; jQuery.each ...

  10. nodeJs入门笔记(二)

    js中window通常是全局变量 global 是node.js里的全局变量 node中能访问的对象一般都是 global的 属性 global 对象属性 process 用于描述当前Node 进程状 ...