MSSQL创建存储过程

  1. mssql 存储过程语法:
create proc 存储过程名称 
参数名1 参数类型 [参数默认值] [是否为输出参数],
参数名2 参数类型 [参数默认值] [是否为输出参数]
...
 [with {RECOMPILE|ENCRYPTION|RECOMPILE,ENCRYPTION}]
as 
/*sql 代码块*/

存储过程名称最大长度不能超过256个字符
with recompile:代表每次调用存储过程,db服务器都为对存储过程进行重编译,清除buffer pool中的缓存信息
with ENCRYPTION:加密存储过程
with recompile, ENCRYPTION:重编译存储过程和加密存储过程

  • 数据表
  1. 创建无参查询存储过程
use test
   go
/*1.无参查询的存储过程*/
create proc find_cinema
   as
   begin
   select id from  cinema
   end 
   go 
   /*执行存储过程*/
   exec  find_cinema 
   go

  • 删除存储过程
drop proc find_cinema

  1. 创建带输入参数的存储过程
/*创建有输入参数的存储过程*/
create proc find_cinema_byid
   @a int
   as
   begin
     select * from cinema where id=@a
   end
   go
   /*执行存储过程*/
   exec find_cinema_byid 1

  1. 创建前判断存储过程是否存在
USE [test];

--判断某表的某字段是否存在
if (not exists(select * from syscolumns where id=object_id('BlanceManageO2O') and name='OrderInfoNum'))
ALTER TABLE BlanceManageO2O ADD OrderInfoNum INT DEFAULT 0;

GO

--判断某存储过程是否存在
if (exists (select * from sys.objects where name = 'sp_BlanceOrderInsertCatering'))
    drop proc sp_BlanceOrderInsertCatering
GO

CREATE PROCEDURE [dbo].[sp_BlanceOrderInsertCatering] 
    -- 由存储过程SettlementManageO2O 中传入值
    @ShopId int,
    @AgentId int,
    @BlanceId int,
    @IndustryId int,  --行业类型 行业Id 1-零售 2-餐饮 3-酒店 4-娱乐 5-家政 6-旅游 7-二手
    @Year    int,                                        --年
    @Month  int,                                        --月
    @Day    int    ,                                       --日
    @O2OShopPercent decimal(18,2),                      --020提成百分比
    @WeixinPoundaga decimal(18,2),                      -- 获取系统设置PlatBaseSet表中的微信支付手续费百分比  
    @TransferPriceSum     decimal(18,2) output   
AS    
BEGIN
    --处理程序
END


GO

if (exists (select * from sys.objects where name = 'SettlementManageCatering'))
    drop proc SettlementManageCatering
GO
CREATE PROCEDURE [dbo].[SettlementManageCatering]
AS
BEGIN
   --处理程序
END

GO
posted @ 2022-01-10 16:17  ꧁༺星星的轨迹方程式༻꧂  阅读(670)  评论(0)    收藏  举报