SQL 高级编程 捕捉错误

sql高级编程知识  2008-05-09 14:22:24|  分类: 默认分类 |  标签: |字号大中小 订阅 .

1.局部变量声明和赋值应用
例子1:
declare @name varchar(8)
set @name ='鑫联华'--或者select @name='鑫联华'
print @name --打印变量
select * from office where officeName=@name
例子2:
declare @name varchar(8)
set @name = (select top 1 officeName from office)
select * from office where officeName=@name

2.全局变量
例子1(系统自带的全局变量)
print @@error   --最后一个T-SQL错误的错误号
print @@identity --最后一次插入的标识值
print @@language --当前使用的语言名称
print @@max_connections  --可以创建的同时连接的最大数目数
print @@rowcount --受上一个sql语句影响的行数
print @@servername --本地服务器的名称
print @@servicename --该计算机上的sql服务的名称
print @@timeticks --当前计算机上每刻度的微秒数
print @@version --sql server的版本号

例子2
print '服务器名称:'+@@servername
select @@servername as '服务器名称' 


3.判断语句
例子1:
declare @name varchar(8)
set @name = (select top 1 officeName from office)
print @name
if(@name='鑫联华')
   begin
    select * from office where officeName=@name
   end
else 
   begin
    select * from office
   end


4.while语句
例子1:
declare @name varchar(8)
set @name = '鑫联华'

while (@name='鑫联华')
begin
 print '鑫联华qqq'
 set @name='aaa'
 break --推出循环
 set @name='bbb'
end
print @name


5.case语句
例子1:
select * from office where officeName=case
     when officetype = '高职部w1' then '鑫联华'
     when officetype = '士大夫' then '啊啊'
     else 'aaa'
            end

6.existsnot exists
例子1:
if exists (select * from sysDatabases where name='stuDB')
 drop database stuDB
create database stuDB


例子2:

if exists (select * from office where officeName='鑫联华1')
 begin
 print '数据大于0'
 end
else
 begin
 print '数据等于0'
 end

例子3:
if not exists (select * from office where officeName='鑫联华1')
 begin
 print '数据大于0'
 end
else
 begin
 print '数据等于0'
 end

7.事务处理(transaction)
begin transaction--事务开始
declare @erroNumber int
set @erroNumber = 0
update office set officeName='鑫联华' where officeName='鑫联华'
set @erroNumber = @erroNumber +@@error
update office set officeName='啊啊' where officeName='啊啊'
set @erroNumber = @erroNumber +@@error

print '错误代码:'+convert(varchar(5),@erroNumber)
if(@erroNumber<>0)--更新出错
 begin
   print '更新失败!'
  rollback transaction --事务回滚
 end
else
 begin
  print '更新成功!'
  commit transaction --事务提交
 end

8.存储过程
exec sp_databases --列出当前系统中的数据库
exec sp_renamedb 'studb','studb1' --改变数据库名称(单用户访问)
exec sp_tables --当前数据库中可查询对象的列表(表,视图等)
exec sp_columns office --查看表 office 中列信息
exec sp_help office --查看表office的所有信息
exec sp_helpconstraint office --查看表 office的约束
exec sp_helpindex office --查看表 office的索引
exec sp_helptext syssegments --查看视图的语句文本
exec sp_stored_procedures --返回当前数据库中的存储过程列表

exec sp_password sa,@new=li --添加或者修改登陆帐户的密码

exec xp_cmdshell 'dir' --调用dos命令(这个过程在master数据库里)
exec master.dbo.xp_cmdshell 'dir',no_output --调用dos命令 no_output不显示结果 (这个过程在master数据库里)


例子1:(不带参数的存储过程)

use hospital
go

/*--检测是否存在:存储过程存放在系统表sysobjects中----*/
if exists (select * from sysobjects where name='proc_stu')
 drop procedure proc_stu
go
/*---创建存储过程----*/
create procedure proc_stu
 as
    declare @name varchar(8),@type varchar(8)
    select top 1 @name=officeName,@type=officetype from office
    print '名字:'+@name
    print '类型:'+@type
go

/****调用执行存储过程*******/
exec proc_stu

 

例子2:(带参数的存储过程)

use hospital
go

/*--检测是否存在:存储过程存放在系统表sysobjects中----*/
if exists (select * from sysobjects where name='proc_stu')
 drop procedure proc_stu
go
/*---创建存储过程----*/
create procedure proc_stu
 @name varchar(8),@type varchar(8) -- 参数定义必须在 as 前(参数变量前不能用declare)
 as
    
        declare @department varchar(8)
 set @department=(select department from office where officeName=@name and officetype=@type)
        print 'department:'+@department
go

/****调用执行存储过程*******/
exec proc_stu '鑫联华','高职部w1'
--exec proc_stu @name='鑫联华',@type='高职部w1'--或者这样调用


例子3:预先给参数赋值
use hospital
go

/*--检测是否存在:存储过程存放在系统表sysobjects中----*/
if exists (select * from sysobjects where name='proc_stu')
 drop procedure proc_stu
go
/*---创建存储过程----*/
create procedure proc_stu
 @name varchar(8)='鑫联华',@type varchar(8)='高职部w1' -- 参数定义必须在 as 前(参数变量前不能用declare)
 as
    
        declare @department varchar(8)
 set @department=(select department from office where officeName=@name and officetype=@type)
        print 'department:'+@department
go

/****调用执行存储过程*******/
exec proc_stu
exec proc_stu '鑫联华'
exec proc_stu '鑫联华''高职部w1'
exec proc_stu @type='高职部w1' --只输入第二个参数

 

例子 4:(带返回参数)

use hospital
go

/*--检测是否存在:存储过程存放在系统表sysobjects中----*/
if exists (select * from sysobjects where name='proc_stu')
 drop procedure proc_stu
go
/*---创建存储过程----*/
create procedure proc_stu
 @department varchar(8) output,--output关键字 返回参数,否则视为输入参数
 @name varchar(8)='鑫联华',@type varchar(8)='高职部w1' -- 参数定义必须在 as 前(参数变量前不能用declare)
 as
    
        
 set @department=(select department from office where officeName=@name and officetype=@type)
        print 'department:'+@department
go

/****调用执行存储过程*******/

declare @text varchar(8)
exec proc_stu @text output,@name='鑫联华'--output 的传入参数必须写在前边 如果多个按顺序写
print '返回值:'+@text


例子5
处理错误信息
raiserror   ({msg_id|msg_str}{,severity,state} [,argument[,...n]]) [with option[,...n]]   
msg_id | msg_str }:必需指定错误消息ID或错误消息文本,
severity :指定错误级别(用户可以使用从 018 之间的严重级别),
state :错误调用状态的信息(值默认为 1)。


use hospital
go

/*--检测是否存在:存储过程存放在系统表sysobjects中----*/
if exists (select * from sysobjects where name='proc_stu')
 drop procedure proc_stu
go
/*---创建存储过程----*/
create procedure proc_stu
 @department varchar(8) output,--output关键字 返回参数,否则视为输入参数
 @name varchar(8)='鑫联华',@type varchar(8)='高职部w1' -- 参数定义必须在 as 前(参数变量前不能用declare)
 as
    
        
 set @department=(select department from office where officeName=@name and officetype=@type)
        print 'department:'+@department
 raiserror ('错误信息!',16,1)--表示错误级别为16,调用状态为1(默认)
 raiserror (4008,16,1)--错误编号
go

/****调用执行存储过程*******/

declare @text varchar(8)
exec proc_stu @text output,@name='鑫联华'
print '返回值:'+@text
raiserror ('%d%s',16,1,300,'aaa')--表示错误级别为16,调用状态为1(默认) %d表示数字型替换,%s表示字符替换 最多可以替换20个

 

 


网上查到的介绍
RAISERROR ( { msg_id | msg_str } { , severity , state } 
[ , argument [ ,...n ] ] ) 
[ WITH option [ ,...n ] ] 

上面是引用了联机丛书中raiserror的定义。 
其实看了定义也就不该说第几个了,但是按照常用的理解,就按照你的思路来说吧。 

第一个参数:msg_id,自定义错误号应该大于50000,以前在vb中实现了一把,raiserror的这个错误号确实可以被捕获的,看看sql中多少函数跟vb想象,这点就可以理解了;msg_str,错误信息提示,里面可以用格式修饰符,将后面的参数数组相对应的值返回。相应于vb,err.number可以取msg_id,而err.description可以取msg_str了。 

第二个参数:这里通常是定义安全级别跟错误状态的,相应的可以参考联机丛书,我就不粘出来了。 

第三个参数:[ , argument [ ,...n ] ],参数组,对应msg_str进行输出。 

 

举一个操作pubs中表jobs的例子,在表中搜索max_lvl大于1000的信息,如果没有则返回一条错误: 
------------------------------------------------------------- 
use pubs 
declare @count int 
select @count=count(*) from jobs where max_lvl>1000 
if @count=0 
raiserror('有%d个max_lvl>1000的工作',16,1,@count) 
------------------------------------------------------------- 

执行后将返回: 
------------------------------------------------------------- 
服务器: 消息 50000,级别 16,状态 1,行 5 
有0个max_lvl>1000的工作 
------------------------------------------------------------- 

转自:http://blog.163.com/liqiang135@126/blog/static/1711830420084922224495/

原文地址:https://www.cnblogs.com/bantongshui/p/3170013.html