SqlServer高级存储过程

--------------存储过程------------------------
--T-sql的执行逻辑
--1.语法分析
--2.将目标sql进行编译
--3.开始执行
--如果是存储过程的话,则直接执行第3步

--存储过程的优势
--1.执行速度快,效率高
--2.模块化编程
--3.降低网络流量
--4.安全处理

--存储过程分为两种:系统存储过程、用户自定义存储过程
--系统存储过程以sp_或xp_开头,如:sp_helptext、sp_databases、sp_columns、sp_tables

--查询系统存储过程
--因为系统存储过程都保存在master数据库中,所以查询系统存储过程一定要先打开mastr数据库
use master
go
select * from sys.sysobjects where name like 'sp_%' or name like 'xp_%'
go

--虽然系统存储过程都保存在master数据库中
--但是,我们可以在各个数据库中调用系统存储过程
use netbar
go
--查询当前数据库(netbar)中所有的数据表
execute sp_tables
go
--查询数据表tblcardinfo所有的列信息
exec sp_columns 'TblCardInfo'
go

------------------如何创建用户自定义存储过程---------------------------------------
--创建不带参数的存储过程
create procedure pro_ComputerUseState
as
begin
  select * from tblcomputer;
end

--调用不带参数的存储过程
exec pro_ComputerUseState;
go

--调用系统存储过程sp_helptext来查看我们刚刚定义的存储过程的文本信息
exec sp_helptext 'pro_ComputerUseState';
go

--如何修改存储过程
alter proc pro_ComputerUseState
as
begin
  select intComputerId,
 'intInUse'=case intInuse
 when 0 then '未使用'
 when 1 then '以使用'
 end,
    chvComputerName,
    chvDescription
  from tblcomputer
end

--如何创建有输入参数的存储过程
create procedure pro_getComputerState
@state int--参数默认为输入参数
as
begin
  select intComputerId,
 'intInUse'=case intInuse
 when 0 then '未使用'
 when 1 then '以使用'
 end,
    chvComputerName,
    chvDescription
  from tblcomputer
  where intInuse=@state
end
go
--调用带有输入参数的存储过程的两种方式
--1.
exec pro_getComputerState 0;
go
--2.
declare @state int;
set @state = 1;
exec pro_getComputerState @state;
go
--如何创建带有输入参数默认值的存储过程
alter procedure pro_getComputerState
@state int=0
as
begin
  select intComputerId,
 'intInUse'=case intInuse
 when 0 then '未使用'
 when 1 then '以使用'
 end,
    chvComputerName,
    chvDescription
  from tblcomputer
  where intInuse=@state
end
go
--调用带有输入参数默认值的存储过程的两种方式
--1.
execute pro_getComputerState;
go
--2.
execute pro_getComputerState default;
go

--创建带有输入参数和输出参数的存储过程
--参数之间用逗号,隔开,最后一个参数后无需逗号
create proc pro_getComputerStateById
@intComputerId int,
@state int output
as
begin
  select @state = intInUse
  from tblcomputer
  where intcomputerid=@intComputerId
end

--调用带输出参数的存储过程
--调用时一定要在输出参数后加关键字output
declare @state int, @computerId int;
set @computerId = 7;
exec pro_getComputerStateById @computerId,@state output
select @state;

--根据电脑id删除电脑
--存储过程内为删除操作
create proc pro_deleteComputerById
@computerId int
as
begin
  delete from tblcomputer where intcomputerId=@computerId;
end

exec pro_deletecomputerbyid 9

--创建存储过程:当目标id计算机不存在时返回-1;删除成功返回1;删除失败返回0
--在存储过程内部可以使用return关键字返回指定的数值,且只能返回数值
alter proc pro_deleteComputerById
@computerId int
as
begin
  if not exists(select * from tblcomputer where intcomputerId = @computerId)
    return -1;
  delete from tblcomputer where intcomputerId=@computerId;
  if @@rowcount > 0
    return 1;
  return 0;
end

--调用带返回值的存储过程
declare @result int;
exec @result = pro_deleteComputerById 9
select @result;
go

--1.如何创建不含参数的存储过程
--2.如何创建带有输入参数的存储过程
--3.如何创建既有输入参数又有输出参数的存储过程
--4.如何创建带有返回值的存储过程
--5.如何调用上述类型的存储过程
--6.存储过程能不能既有输入参数、输出参数、返回值???肯定可以。
--  存储过程永远都会有返回值,默认的返回值为0

--异常处理
--raiserror:3个参数
create procedure pro_deleteCardInfoById
@intCardId int
as
begin
  if not exists(select * from TblCardInfo where intCardId = @intCardId)
  begin
    raiserror('指定的卡号不存在', 16, 1);
    return -1;
  end
  delete from TblCardInfo where intCardId=@intCardId;
  if @@rowcount > 0
    return 1;
  return 0;
end

exec pro_deleteCardInfoById 6
select @@error;
go

declare @text nvarchar(1000);
set @text = 'select * from tblcardInfo';
exec(@text);
go

--如何执行sql文本字符串
create proc pro_excutesql
@sqltext nvarchar(4000)
as
begin
  exec(@sqltext);
end

exec pro_excutesql 'update tblcomputer set chvdescription=''xxx'' where intcomputerid = 1'

select * from tblcardinfo;
select * from tblcomputer


 

原文地址:https://www.cnblogs.com/changjiang/p/2605289.html