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 ...
随机推荐
- ORA-02287: 此处不同意序号
ORA-02287: 此处不同意序号 insert into gls_vchitem (viid, yr, km) select gls_vchitem_seq.n ...
- setter设置器 gutter访问器
set方法书写规范: 1.必须以set开头,set后跟去掉下划线的实例变量并且首字母大写.ps: setAge:2.一定有参数3.不能有返回值4.一定是对象方法(-开头)5.形参一般是去掉下划线的实例 ...
- Dell 2950服务器CPU-E1422错误解决方法
.造成原因:CPU松动或者是硅胶损耗 .解决方法: .断掉电源,将其后盖打开(在手没有静电的情况下操作) .拔掉周围的排热扇 .按住关卡,将其CPU卸下:并使用清洁剂清理,再次给CPU上涂上硅胶(均匀 ...
- BOOST_PP_INC_I(x)实现
这个比较有意思,# define BOOST_PP_INC_I(x) BOOST_PP_INC_ ## x 连接在一起以后,然后定义为x+1 实现了inc功能,不过最多也就到255 # /* Copy ...
- error C4996 The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
error C4996: 'strupr': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ co ...
- JAVA日期字符串转化,日期加减
SimpleDateFormat函数语法: G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E ...
- C#获得命令提示符输出
原文:http://blog.csdn.net/abrahu/article/details/6611504 C#获得命令提示符输出 分类: c#应用程序2011-07-16 23:34 600人阅读 ...
- 使用runOnUiThread更新UI
android中更新UI的方式比较多,这里就不一一介绍了,比较常用的Thread+Handler,但是这种方式较繁琐,如在使用ProgressDialog创建进度对话框一文中就是使用的这种方式更新UI ...
- Swift 控制流
Swift 1,顺序结构 2,分支结构 switch 中每一个case块完成后会自动终止switch语句, 不用手动终止 case 可有多值,如 case "A","a& ...
- Cookies与保持登录(新浪微博的简单模拟登录)
Cookies与保持登录(新浪微博的简单登录) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino ...