sql server 存储过程、事务,增删改
CREATE procedure [dbo].[sp_TableSave]
@TypeID tinyint, -- 0 新增,1 修改,2 删除
@ID int,
@Name nvarchar(32),
@ProductIDs varchar(1024),
@BackColor nvarchar(128),
@UID int,
@Creator nvarchar(32),
@Error nvarchar(100)='' output
as
set nocount on
begin try
begin transaction -- 开始事务
declare @currDate SMALLDATETIME
SET @currDate=GETDATE()
if(@TypeID='') --修改
begin
update ProductActivity set Name=@Name,ProductIDs=@ProductIDs,Backcolor=@BackColor,IsPush=1 where ActivityID=@ID -- 更新移除产品log
insert into OperateLog(UID,UserName,Title,Content,CreateTime)
select @UID,@Creator,'更新',CAST(ProductID as varchar(10))+' /Activity:0',@currDate
from Product AS p
WHERE ActivityID=@ID and NOT EXISTS(
select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID
) -- 更新移除产品
update p set ActivityID=0,IsPush=(case when p.status=0 then 1 else 0 end)
from Product AS p
WHERE ActivityID=@ID and NOT EXISTS(
select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID
) -- 更新新增产品log
insert into OperateLog(UID,UserName,Title,Content,CreateTime)
select @UID,@Creator,'更新',CAST(ProductID as varchar(10))+' /Activity:'+CAST(@ID as varchar(10)),@currDate
from Product AS p
WHERE ActivityID<>@ID and EXISTS(
select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID
) -- 更新新增产品
update p set ActivityID=@ID,IsPush=(case when p.status=0 then 1 else 0 end)
from Product AS p
WHERE ActivityID<>@ID and EXISTS(
select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID
)
end
else if(@TypeID='') --删除
begin
-- 删除 ProductActivity
update ProductActivity set Status=1,IsPush=1 where ActivityID=@ID -- 写入product改动log
insert into OperateLog(UID,UserName,Title,Content,CreateTime)
select @UID,@Creator,'更新',cast(ProductID as varchar(10))+'/Activity:0',@currDate from Product where ActivityID=@ID -- 更新product
update Product set ActivityID=0,IsPush=(case when Product.status=0 then 1 else 0 end) where ActivityID=@ID
end
else --新增
begin
-- 更新ProductActivity表
declare @NewID int
insert into ProductActivity(Name,ProductIDs,Backcolor,Creator,CreateTime,IsPush)
values(@Name,@ProductIDs,@BackColor,@Creator,@currDate,1)
set @NewID = SCOPE_IDENTITY() -- 更新product表
update p set ActivityID=@NewID, IsPush=(case when p.Status=0 then 1 else 0 end)
FROM Product AS p
INNER JOIN dbo.F_split(@ProductIDs,',') AS t ON t.f=p.ProductID -- 写product改动log
insert into OperateLog(UID,UserName,Title,Content,CreateTime)
select @UID,@Creator,'更新', CAST(ProductID as varchar(10))+'/Activity:'+CAST(@NewID as varchar(10)),@currDate
from Product where ActivityID=@NewID
end
set @Error=''
commit transaction -- 提交事务
return;
end try
begin catch -- 异常
set @Error='1-'+ERROR_MESSAGE()
rollback transaction -- 回滚事务
return;
end catch
sql server 存储过程、事务,增删改的更多相关文章
- Sql Server——数据的增删改
		
所谓数据的增删改就是在创建好数据库和表后向表中添加数据.删除表中的数据.更改表中的一些数据. 新增数据: 语法一: insert into 表名 values (数据内容) --这里需要 ...
 - VS连接SQL Server数据库,增删改查详细教程(C#代码)_转载
		
工具: 1.Visual Studio (我使用的是vs2013) 2.SQL Server (我使用的是sql server2008) 操作: 1.打开SQL Server,打开后会看到数据库的初 ...
 - python连接sql server数据库实现增删改查
		
简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...
 - SQL server数据类型、增删改查
		
数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...
 - C#winform窗体实现对sql server数据库的增删改查
		
1.运行效果截图 2.输入用户名,密码进行查询 查找成功则显示 查找不成功显示用户信息不存在 3.输入用户名与密码,向数据库中添加用户信息 添加后数据库表信息 4.查看全部信息 5.根据编号信息进行查 ...
 - 【sql server常用操作{增删改查}】
		
use DB_x go drop database DB_y create database DB_y --创建数据库 on primary --指定主数据文件 ( name= ...
 - sql server触发器中增删改判断
		
触发器生效逻辑 在Before或者After之后使用INSERT,DELETE,UPDATE 触发器内情况判断 插入 if exists(select 1 from inserted) and not ...
 - SQL Server 约束的增删改
		
1. 非空约束 列的为空性决定表中的行是否可以包含空值.空置(NULL)不同于零(0)/空白或者长度为零的字符串(“”). (1)创建非空约束 create table orders ( docent ...
 - SQL Server 数据的增删改
		
1. 使用Insert语句添加数据 (1)如果要向表中每一列都添加数据,则可以省略列明,直接将值按照顺序写入并用逗号隔开就行. use Howie ','JD','mars','CN','sh') ( ...
 - SQL Server  存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过
		
存储过程 创建存储过程 use pubs --pubs为数据库 go create procedure MyPRO --procedure为创建存储过程关键字,也可以简写proc,MyPRO为存储过程 ...
 
随机推荐
- C编程技巧
			
1,attempted assighnment to literal if (i == 3) { //codes } else if (4 == 4); 2,引用数组元素相当于对指针加上偏移量的引用 ...
 - [置顶] Direct UI
			
有个坑爹的说法:其实Direct UI只是一个思想,要实现这个思想,还要靠自己. 采用windowless方式用api或gdi实现ui的绘制. DirectUI意为直接在父窗口上绘图(Paint on ...
 - Java面向对象面试案例
 - iOS 原生地图 开发
			
iOS开发有时候用到地图,不少人第一想到的是用第三方.当然有时候为了和安卓同步,可能会一起使用某一第三方.但有时候,我们是可以用原生地图开发的.上面两个示意图是原生地图的自定义开发.运行demo,将展 ...
 - ubuntu设置系统时间与网络时间同步和时区
			
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...
 - 编写isNull isArray isFunction的方法
			
1.isNull 判断null,需要排除掉undefined和0.''(空串). function isNull(arr){ return !arr&&typeof arr!=='un ...
 - IE下全局对象报 脚本错误提示“对象不支持此属性或方法”解决方案
			
原来是IE会把页面中的元素id可以直接当变量名一样使用,但是这个id变量不能被赋值. 例如: <body id='body'> <script type="text/jav ...
 - windows server 2008 应用程序池自动关闭  C:\Windows\system32\RpcProxy\RpcProxy.dll failed to load
			
Sign In Join Search IIS Home Downloads Learn Reference Solutions Blogs Forums HomeIIS.NET Forums64-b ...
 - Oracle运维 专业的事情交给专业的人来做
			
关于Oracle运维的重要性,随便写了几句,放在这儿和大家共勉. Oracle数据库在RDBMS中独占熬头,07年统计其市场份额占关系数据库市场的48.6%稳居首位,而且逐年增加,上升势头强劲.在使用 ...
 - 使用Open Live Writer 的代码高亮插件体验
			
由于windows live writer 2012 已经停止服务,转而推出开源项目Open Live Writer .虽然Open Live Writer 也没怎么更新,官网更是一个插件都没有放出来 ...