随笔 - 631  文章 - 75  评论 - 186  阅读 - 143万

SQL Server-存储过程(Procedure),带入参数和出参数

ylbtech-SQL Server:SQL Server-存储过程(Procedure),带入参数和出参数

 SQL Server 中的存储过程(Procedure),带入参数和出参数。

内容简介

1, 存储过程(Procedure)-基本创建与操作。

2, 存储过程(Procedure)-带入参。

3, 存储过程(Procedure)-带入参和出参。

技术与环境
   

语言类别:

SQL之T-SQL

thankyou: sunshine, 谢谢你的默默付出

数据库:

SQL Server

学习软件:

Microsoft SQL Server

课程总策划:

yuanbo

English name:

sunshine

个人主页:

http://www.cnblogs.com/ylbtech/

科研团队:

ylbtech

教研团队:

ylbtech

1, 存储过程(Procedure)-基本创建与操作。返回顶部

--================================
-- ylb:存储过程创建与操作
--================================
use pubs
 
go
--一、无参存储过程
--1,创建存储过程
create procedure PTitles
as
select * from titles
 
go
--2,执行存储过程
execute PTitles
 
go
--3,移除存储过程
--drop procedure PTitles
go
 
2, 存储过程(Procedure)-带入参返回顶部
--==============================
-- ylb:存储过程-入参
-- 16:44 2011/12/14
--==============================
use pubs
 
go
--1,创建带入参存储过程
select * from titles where type='business'
 
go
create proc P_Titles_ByType
@type char(12) --入参
as
select * from titles where type=@type
 
 
go
--2,执行带参数的存储过程
--a)方式一
exec P_Titles_ByType @type='business'
go
--b)方式二
exec P_Titles_ByType 'business'
 
go
--P1:写一个存储过程,要求图书类型是business且单价大于10的所有信息
--P1_1,创建存储过程
select * from titles
where type='business' and price>10
 
go
create proc P_Titles_ByTypeAndPrice
@type char(12), --入参
@price money --入参
as
select * from titles
where type=@type and price>@price
 
go
--P1_2,执行存储过程
exec P_Titles_ByTypeAndPrice
@type='business',@price=10
 
go
exec P_Titles_ByTypeAndPrice
@price=10,@type='business'
 
go
exec P_Titles_ByTypeAndPrice 'business',10
 
go
--是错的,当你直接给值时,一定注意参数的顺序和类型。
--exec P_Titles_ByTypeAndPrice 10,'business'
3, 存储过程(Procedure)-带入参和出参。返回顶部
--================================
-- ylb:存储过程-带入参和出参
-- 16:44 2011/12/14
--================================
use pubs
 
go
select * from titles
--P1:查图书编号是“BU1032”的图书的单价是多少?
select price from titles where title_id='BU1032'
 
go
--P1_1,创建
create proc P_Titles_ByTitleID_SelectPrice
@title_id varchar(6) --入参
as
select price from titles where title_id=@title_id
 
go
--P1_2,执行
exec P_Titles_ByTitleID_SelectPrice 'BU1032'
 
go
--P2_1,创建
create proc P_Titles_ByTitleID_SelectPrice2
@title_id varchar(6), --入参
@price money output   --出参【出参加标识(output)】
as
select @price=price from titles where
title_id=@title_id
--出参的@在=左边
 
go
--1,先声明变量
declare @price2 money
--2,之后在调用
exec P_Titles_ByTitleID_SelectPrice2
@title_id='BU1032',
@price=@price2 output
--3,再之后在查声明变量
select @price2
--出参要声明,配参后面要加output标识,之后再查声明变量。
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on   ylbtech  阅读(67239)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
< 2012年8月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示