mssql字符串分割后的值,把表中不存在的插入表中
字符串分割后的值,把表中不存在的插入表中 --供大家参考
使用场景,自行思考……
--创建表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字符串分割后的值,把表中不存在的插入表中的更多相关文章
- SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)
1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地 ...
- mysql substring_index()查询某个字符中以某个分割符分割后的值
substring_index(某个字段,以其分割,第几个分割点之前的值); +---------------------------------------------------------+ | ...
- MSSQL字符串分割
CREATE FUNCTION dbo.f_splitstr( @str varchar(8000) )RETURNS @r TABLE(id int IDENTITY(1, 1), value va ...
- SQL Server 游标运用:鼠标轨迹字符串分割
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...
- c语言字符串分割函数(转)
源:C语言实现split以某个字符分割一个字符串 void split(char *src, const char *separator, char **dest, int *num) { /* sr ...
- 【Linux】shell字符串分割、数组访问、条件判断
参考资料: shell字符串分割再循环:http://www.shangxueba.com/jingyan/1633455.html linux shell中 if else以及大于.小于.等于逻辑表 ...
- C# 批量插入表SQLSERVER SqlBulkCopy往数据库中批量插入数据
#region 帮助实例:SQL 批量插入数据 多种方法 /// <summary> /// SqlBulkCopy往数据库中批量插入数据 /// </summary> /// ...
- SQL 字符串分割表函数
--字符串分割表函数 ) ) declare @i int; declare @count int; ); ); declare @Index int; )) declare @rowID int; ...
- PCB MS SQL表值函数与CLR 表值函数 (例:字符串分割转表)
将字符串分割为表表经常用到,这里 SQL表值函数与CLR 表值函数,两种实现方法例出来如下: SELECT * FROM FP_EMSDB_PUB.dbo.SqlSplit('/','1oz/1.5 ...
随机推荐
- Fix The thread xxx has exited with code 259 (0×103)
When run the test case in VS2013, you may encounter below problem After test case end, it will show ...
- OCP-1Z0-053-V12.02-512题 【转】
http://blog.csdn.net/gisinfo/article/details/8159875 1.Which two statements correctly describe the r ...
- VBA基础概念
一:VBA对象 'VBA对象 'VBA中的对象其实就是我们操作的具有方法.属性的excel中支持的对象 'Excel中的几个常用对象表示方法 '1.工作簿 ' Workbooks 代表工作簿集合,所有 ...
- uva12589
题目大意:给n(n<=50)个向量(xi,yi) (0<=xi<=yi<=50),选出其中k(1<=k<=n)个,从(0,0)点开始,依次首尾相连,求此k个向量与x ...
- 加密传输SSL协议3_非对称加密
困死了,这里对非对称加密体系开个头,具体的实验明天写 非对称加密体系 为了解决对称加密中密钥的传输的问题,一些天才的数学家就提出了非对称式的加密体系,也称为公钥加密体系. 加密和解密的密钥是不同的.一 ...
- function(ev) //括号里的ev是什么东西,什么意思?
事件对象: 楼上的不要误导新人 ev是我们获取事件详细信息的event是IE的写法 大多数人是写window.event 其实区别也差不多 好像window.event更安全些 我也不记得了 火狐呢 ...
- mysql模糊匹配
select * from tableName where column like ""; select * from tableName where column regexp ...
- MigLayout
1. 初始化: MigLayout l = new MigLayout(); MigLayout l = new MigLayout("","","& ...
- 转 ——eclipse下进行Python开发 环境配置
python for eclipse插件安装1.下载python for eclipsepython for eclipse下载地址,如:org.python.pydev.feature-1.6.3. ...
- str系列---字符串函数
1. strcat extern char *strcat(char *dest,char *src); 把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'. 返回指 ...