sql清空表数据后重新添加数据存储过程
ALTER PROCEDURE [dbo].[sp_add_Jurisdiction]
@CTableName varchar(20), --当前要删除、新增的表
@filedkeyValue varchar(20), --要删除的字段值
@filedName varchar(20), --要删除的字段名
@filedNameAdd1 varchar(20), --要新增的字段名1
@filedNameAdd2 varchar(20), --要新增的字段名2
@sql varchar(6000) --新增值字符串,如:25,30;25,31
AS
begin
declare @strSQL_1 varchar(6000) -- 删除
declare @strSQL_2 varchar(6000) -- 新增
BEGIN try
BEGIN TRANSACTION
set @strSQL_1 = 'delete from ['+@CTableName+'] where '+@filedName+' = '+@filedkeyValue+''
exec(@strSQL_1)
DECLARE @Run bit --继续循环的标志
declare @dotIndex as int
declare @doIndex as int
declare @strValue1 as varchar(100)
declare @strValue2 as varchar(100)
declare @strText as varchar(100)
declare @ReturnValue int
set @Run = 1
set @ReturnValue=0;
while( @Run = 1)
begin
if(len(@sql)>0) --如果字符串还不为空,则进行一下操作
begin
set @dotIndex=CharIndex(';',@sql)
if(@dotIndex = 0) --判断是否找到了';'符号
begin
set @doIndex=CharIndex(',',@sql)
set @strValue1 = substring(@sql,1,@doIndex-1)
set @strValue2 = substring(@sql,@doIndex+1,LEN(@sql))
set @strSQL_2 = 'insert into ['+@CTableName+'] ('+@filedName+','+@filedNameAdd1+','+@filedNameAdd2+') values ('+@filedkeyValue+','+@strValue1+','+@strValue2+')'
exec(@strSQL_2);
set @ReturnValue=@ReturnValue+1;
set @Run=0
end;
else
begin
set @strText = substring(@sql,1,@dotIndex-1) --截取','前的字符串
set @sql=substring(@sql,@dotIndex+1,len(@sql)) --把字符串的长度缩短至后一个
set @doIndex=CharIndex(',',@strText)
set @strValue1 = substring(@strText,1,@doIndex-1)
set @strValue2 = substring(@strText,@doIndex+1,LEN(@strText))
set @strSQL_2 = 'insert into ['+@CTableName+'] ('+@filedName+','+@filedNameAdd1+','+@filedNameAdd2+') values ('+@filedkeyValue+','+@strValue1+','+@strValue2+')'
exec(@strSQL_2)
set @ReturnValue=@ReturnValue+1;
end
end
else
begin
set @Run=0
set @ReturnValue=@ReturnValue+1;
end
end
Commit Transaction
return @ReturnValue;
end try
begin catch
set @ReturnValue=0;
Rollback Transaction
return @ReturnValue;
end catch
end
sql清空表数据后重新添加数据存储过程的更多相关文章
- SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)
SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...
- Influx Sql系列教程五:insert 添加数据
接下来开始进入influxdb的curd篇,首先我们看一下如何添加数据,也就是insert的使用姿势 在进入本篇之前,对于不了解什么是retention policy, tag, field的同学,有 ...
- SQL Server ->> DISABLE索引后插入更新数据再REBUILD索引 和 保留索引直接插入更新数据的性能差异
之前对于“DISABLE索引后插入更新数据再REBUILD索引 和 保留索引直接插入更新数据的性能差异”这两种方法一直认为其实应该差不多,因为无论如何索引最后都需要被维护,只不过是个时间顺序先后的问题 ...
- sql server中新增一条数据后返回该数据的ID
开发中遇到的问题:在新增一条数据后往往不需要返回该数据的ID,但是有的时候可能需要返回该数据的ID以便后面的编程使用. 在这里介绍两种方法: 其一:使用存储过程: create procedure a ...
- sql server 表变量存储临时查询数据
对于使用sql server 编写存储过程或者类似的sql 查询的时候我们使用表变量进行临时数据的存储,可以方便我们进行下来的数据处理 表变量的使用类似如下: declare @userinfo ta ...
- DataTable操作(建表,建行,建列,添加数据)
public DataTable GetNewTable() { DataTable dt2 = new DataTable("NewDataSet"); //创建一个新Table ...
- PHP 简单实现MySQL数据搜索、添加数据功能 以设备管理为例
测试截图: 数据库bzec ,表shebeidangan 列sb_name,sb_numandtype,sb_home,sb_usedate,sb_address,sb_updatetime,sb_r ...
- sql 两表查询后 更新某表中部分字段
这是上一个sql更新某表字段的一个延伸,在更新表数据时,实际上会有多表数据查询场景,查询后,只需要更新某一个表中的数据,以下提供两个方法, 第一种使用update 两表查询 update api_ma ...
- 为SQL Server表中的列添加/修改/删除注释属性(sp_addextendedproperty、sp_updateextendedproperty、sp_dropextendedproperty)
本篇基本完全参考:sql--sp_addextendedproperty和sp_updateextendedproperty (Transact-SQL) 三个存储过程用法一样,以sp_addexte ...
随机推荐
- 九度oj 题目1062:分段函数
题目1062:分段函数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3874 解决:2278 题目描述: 编写程序,计算下列分段函数y=f(x)的值.y=-x+2.5; 0<=x& ...
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
1100 - Again Array Queries ...
- ehcache、memcache、redis三大缓存比较
最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...
- Mayor's posters POJ - 2528
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- 【进击后端】mongodb入门
环境:ubuntu 重要字:数据库,集合,记录,字段 api地址:https://docs.mongodb.com/manual/reference/method/db.collection.upda ...
- Divide Two Integers(模拟计算机除法)
Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...
- Java电商项目-6.实现门户首页数据展示_Redis数据缓存
目录 项目的Github地址 需求介绍 搭建Redis集群环境 下面先描述单机版redis的安装 下面将进行Redis3主3从集群环境搭建 基于SOA架构, 创建门户ashop-portal-web门 ...
- Java线程:Callable和Future
接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. Callable接口类似于Runnable,从名字就可以看出来了,但 ...
- vuex资料
vuex最简单.最详细的入门文档 链接:https://segmentfault.com/a/1190000009404727 https://www.jb51.net/article/138239. ...
- Django打造大型企业官网(八)
4.16.侧边栏标题和广告位布局完成 templates/news/index.html <div class="sidebar-wrapper"> <div c ...