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 存储过程、事务,增删改的更多相关文章

  1. Sql Server——数据的增删改

    所谓数据的增删改就是在创建好数据库和表后向表中添加数据.删除表中的数据.更改表中的一些数据. 新增数据: 语法一: insert into 表名 values (数据内容)        --这里需要 ...

  2. VS连接SQL Server数据库,增删改查详细教程(C#代码)_转载

    工具: 1.Visual Studio (我使用的是vs2013) 2.SQL Server  (我使用的是sql server2008) 操作: 1.打开SQL Server,打开后会看到数据库的初 ...

  3. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  4. SQL server数据类型、增删改查

    数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...

  5. C#winform窗体实现对sql server数据库的增删改查

    1.运行效果截图 2.输入用户名,密码进行查询 查找成功则显示 查找不成功显示用户信息不存在 3.输入用户名与密码,向数据库中添加用户信息 添加后数据库表信息 4.查看全部信息 5.根据编号信息进行查 ...

  6. 【sql server常用操作{增删改查}】

    use DB_x   go   drop database DB_y   create database DB_y --创建数据库   on primary --指定主数据文件   (   name= ...

  7. sql server触发器中增删改判断

    触发器生效逻辑 在Before或者After之后使用INSERT,DELETE,UPDATE 触发器内情况判断 插入 if exists(select 1 from inserted) and not ...

  8. SQL Server 约束的增删改

    1. 非空约束 列的为空性决定表中的行是否可以包含空值.空置(NULL)不同于零(0)/空白或者长度为零的字符串(“”). (1)创建非空约束 create table orders ( docent ...

  9. SQL Server 数据的增删改

    1. 使用Insert语句添加数据 (1)如果要向表中每一列都添加数据,则可以省略列明,直接将值按照顺序写入并用逗号隔开就行. use Howie ','JD','mars','CN','sh') ( ...

  10. SQL Server 存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过

    存储过程 创建存储过程 use pubs --pubs为数据库 go create procedure MyPRO --procedure为创建存储过程关键字,也可以简写proc,MyPRO为存储过程 ...

随机推荐

  1. 自动测试工具SilkTest全面介绍

    象交互,并最终记录测试结果,用户可以根据这些测试结果来判断测试成功还是失败. 4Test 脚本语言 和绝大多数自动化测试工具一样, SilkTest 可以自动捕捉,检测和重复用户交互的操作从而驱动测试 ...

  2. Makefile中使用$$的使用

      http://blog.csdn.net/darennet/article/details/8185881   Makefile中使用$$的使用     在makefile中,会经常使用shell ...

  3. 创建渐进式jpeg图片

    <?php // Create an image instance $im = imagecreatefromjpeg('test.jpg');   // Enable interlancing ...

  4. jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释

    3.1 源代码 init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(&qu ...

  5. odbc连接数据库

    using System; using System.Collections.Generic; using System.Text; using Console = System.Console; u ...

  6. javascript模式——Mixin

    Mixin是一种扩展收集功能的方式,能提高代码的复用率. 在javascript中,原型可以继承于其它对象的原型,并且可以为任意数量的实例定义属性.可以利用这一点来促进函数的复用. 下面一段代码就是将 ...

  7. 自定义不等高的cell-(storyboard)

    对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView之间的间距约束 设置tableViewCell的真实行高和估算行高 // 告诉tableView所有 ...

  8. Ubuntu 12.04 下安装git

    ---恢复内容开始--- 1.安装build-essential. 列出Git相关包(git-core 和 git-doc)所以来的各个安装包并安装: sudo apt-get build-dep g ...

  9. eclipse 和myEclipse 项目导入

    经常在eclipse/myeclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 有两种情况: 一.eclipse无法识别其他eclipse的web项目 解决步骤: ...

  10. c#访问各数据库的providerName各驱动

    在machine.config(C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/CONFIG)文件中有这么一段: <system.data> & ...