首页 > 技术文章 > SQL 存储过程[1]-常用参数及示例

guorongtao 2020-09-16 09:17 原文

SQL 存储过程[1]-常用参数及示例

1、常用参数:

  • use 使用数据库
  • GO 向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号 即断句。
  • as 执行的操作,是个关键字。例如 create table test as select * from table1
  • declare 申明
  • @ 申明变量
  • -- 注释
  • begin end 控制批处理
  • if 条件
  • if exists 是否存在
  • while 循环
  • case...when 分支判断
  • reture 无条件返回语句
  • waitfor '时间' 延期执行 / 指定日期执行
  • goto 无条件跳转到指定的程序执行点
  • try ... catch 异常处理
  • PRINT 输出语句
  • raisettor 返回错误信息
  • set 设置变量的值
  • exec 执行
  • alter 修改
  • drop 删除
  • with encryption 加密存储过程
  • sp_helptext 获取关于加密存储的信息
  • sp_rename 重命名存储过程
  • sp_OACreate 调用com组件
  • sp_monitor 监控存储过程
  • sp_procoption 自动执行存储过程 例如:sp_procoption 'stores','startup','off'
  • sp_configure 远程执行存储过程 例如:sp_configure 'remote access',1
  • rollback transaction 回滚事务

2、示例:

存储过程 sss

if exists(select name from sysobjects where name='sss' and type='P')
drop procedure sss
go
create procedure sss 
as 
select * from users 
go
exec sss

带参数的存储过程:

use TestDB
go
if exists(select name from sysobjects where name='sss' and type='P')
drop procedure sss
go
create procedure sss 
@aa1 int,
@bb1 varchar(20)
as --执行SELECT语句 
select * from users where fid=@aa1 and fname1=@bb1
go
exec sss 2,'滔Roy'

  

 

创建时间:2020.09.16  更新时间:

 

推荐阅读