sqlserver中创建包含事务的存储过程
1 ALTER proc [dbo].[Proc_InsertStudent]
2 @stuName nvarchar(50),@stuClassId int,@stuAge int
3 as
4 begin
5 set nocount on --on表示不返回计数
6 set xact_abort on --当执行事务时,如果出错,会将transcation设置为uncommittable状态
7
8 begin try
9 declare @stuCountByName int;
10 select @stuCountByName=count(*) from Students where Name=@stuName;
11
12 if(isnull(@stuName,'')='')
13 begin
14 print('名字不能为空');
15 return;
16 end
17
18 if(@stuCountByName>0)
19 begin
20 print('名字重复');
21 return
22 end
23
24 begin tran --开启事务
25 insert into Students(Name,ClassId,Age) values(@stuName,@stuClassId,@stuAge)
26 commit tran --提交事务
27
28 end try
29
30 begin catch
31 if xact_state()=-1
32 rollback tran; --回滚事务
33 select ERROR_NUMBER() as ErrorNumber;
34 select ERROR_MESSAGE() as ErrorMsg;
35 end catch
36 set xact_abort off;
37 end 其中Students表:
1 CREATE TABLE [dbo].[Students](
2 [ID] [int] IDENTITY(1,1) NOT NULL primary key,
3 [Name] [nvarchar](50) NOT NULL,
4 [ClassId] [int] NOT NULL,
5 [Age] [int] NOT NULL,
6 [CreateTime] [datetime] NOT NULL
7 );


1 ALTER proc [dbo].[Proc_InsertStudent]
2 @stuName nvarchar(50),@stuClassId int,@stuAge int
3 as
4 begin
5 set nocount on --on表示不返回计数
6 set xact_abort on --当执行事务时,如果出错,会将transcation设置为uncommittable状态
7
8 begin try
9 declare @stuCountByName int;
10 select @stuCountByName=count(*) from Students where Name=@stuName;
11
12 if(isnull(@stuName,'')='')
13 begin
14 print('名字不能为空');
15 return;
16 end
17
18 if(@stuCountByName>0)
19 begin
20 print('名字重复');
21 return
22 end
23
24 begin tran --开启事务
25 insert into Students(Name,ClassId,Age) values(@stuName,@stuClassId,@stuAge)
26 commit tran --提交事务
27
28 end try
29
30 begin catch
31 if xact_state()=-1
32 rollback tran; --回滚事务
33 select ERROR_NUMBER() as ErrorNumber;
34 select ERROR_MESSAGE() as ErrorMsg;
35 end catch
36 set xact_abort off;
37 end
sqlserver中创建包含事务的存储过程的更多相关文章
- SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题
转自 :SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题 问题: 1. System.Data.SqlClient.SqlExcepti ...
- 用字符流实现每个文件夹中创建包含所有文件信息的readme.txt
package com.readme; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; i ...
- SqlServer中创建Oracle连接服务器
转自太祖元年的:http://www.cnblogs.com/jirglt/archive/2012/06/10/2544025.html参考:http://down.51cto.com/data/9 ...
- mysql中,创建包含json数据类型的表?创建json表时候的注意事项?查询json字段中某个key的值?
需求描述: 在mysql数据库中,创建包含json数据类型的表.记录下,在创建的过程中,需要注意的问题. 操作过程: 1.通过以下的语句,创建包含json数据类型的表 mysql> create ...
- SqlServer中创建Oracle链接服务器
SqlServer中创建Oracle链接服务器 第一种:界面操作 (1)展开服务器对象-->链接服务器-->右击“新建链接服务器” (2)输入链接服务器的IP (3)链接成功后 第二种:语 ...
- SQLServer之创建分布式事务
分布式事务创建注意事项 指定一个由 Transact-SQL 分布式事务处理协调器 (MS DTC) 管理的 Microsoft 分布式事务的起点. 执行 BEGIN DISTRIBUTED TRAN ...
- Sql语句在SqlServer中创建数据库、表格并添加约束
通过Sql语句来创建数据库与架构 创建数据库 数据库的创建首先是要引用主数据库的,需要在master数据库的环境下进行创建.大致的语法如下: -- 使用master数据库 use master -- ...
- Sqlserver中查找包含某一列的所有的表
select cols.name,cols.id,objs.name,objs.id from syscolumns cols INNER JOIN sysobjects objs on cols.i ...
- sqlserver中创建链接服务器
链接服务器在跨数据库/跨服务器查询时非常有用(比如分布式数据库系统中),本文将以图文方式详细说明如何利用SQL Server Management Studio在图形界面下创建链接服务器. 1 ...
随机推荐
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- Python的文本数据
字符串的一些方法! 1.text.endswith(".jpg") 如果字符串是以给定子字符串结尾的,就返回值True. 2. text.upper(): ...
- 关于SQL Server无法查询中文的问题
SQL Server 的版本是2016,随便试了一条 带有 where 子句的查询.如下: select * from Roles where RoleName like '%系统%' 呐尼,怎么一条 ...
- Hadoop编程1:天气数据AWK & MapReduce
本文介绍通过AWK和MapReduce两种方式统计出每年温度到最高气温直.awk速度虽然快,而且简短,但是数据量巨大到时候,就遇到力瓶颈,及时分布式执行awk脚本,也会出现机器死掉等问题,需要容错机制 ...
- (7) 深入了解Java Class文件格式(六)
转载:http://blog.csdn.net/zhangjg_blog/article/details/21888943 经过前几篇文章, 终于将常量池介绍完了, 之所以花这么大的功夫介绍常量池, ...
- python学习之认识字符串
1.打印字符串 >>> print ("hello world") hello world 2.使用“/"进行符号转义 >>> pri ...
- Android自定义View自定义属性
1.引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleabl ...
- chrome 开发者工具详解
Google Chrome一共提供了8大组工具: Elements: 允许我们从浏览器的角度看页面,也就是说我们可以看到chrome渲染页面所需要的的HTML.CSS和DOM(Document Obj ...
- ado.net C#如何实现数据库增删改查
ado.net数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术也就是说是最麻烦但是是最不可缺少的 ...
- 批量删除.pyo后缀的文件
find . -name "*.pyo" -exec rm {} \; 删除以.pyo后缀的所有文件 find . ! -name "*.pyo" -exec ...