search
HomeDatabaseMysql TutorialSQLserver的存储过程

SQLserver的存储过程

Jun 07, 2016 pm 03:25 PM
createsqlserverstorageprocess

存储 过程 【 Create 是创建 存储 过程 , alter 是更改、改变 存储 过程 】 【在第一次写 存储 过程 时用 create ,若修改 存储 过程 程序之后 , 则 alter 替换 create 再执行】 【在数据库中 begin end 为大括号的意思】 创建 存储 过程 的格式: --(proce

存储过程

 

Create是创建存储过程alter是更改、改变存储过程

【在第一次写存储过程时用create,若修改存储过程程序之后,alter替换create再执行】

【在数据库中begin  end为大括号的意思】

 

·创建存储过程的格式:

--(procedure可简写为proc)proc为程序、步骤的意思。后跟存储过程

 

create proc 存储过程

as

   代码块

Go

--exec为执行的意思。执行存储过程

Exec  存储过程名 

 

 

---------修改存储过程
alter proc hehe   ---alter更改、改变的意思
as
select 学生学号,语文分数 from fenshu
go
exec hehe

 

 

-------------查询多个表

create proc chaxun

as

begin

select * from fenshu

select * from jiaoshi

select * from xuesheng

end

go

exec chaxun

 

--------------带参数的存储过程

create proc chucunguocheng

@yican varchar(20),    @yican 含义为形参

@ercan varchar(20)

as

begin

   print @yican+@ercan

end

go

exec chucunguocheng '你好','中国'

 

例题:

-------输入学号,判断学生优秀、结业、不结业(三门课及格为优秀,两门课及格为结业)

alter proc biye

@xuehao int   --创建输入变量

as

begin

declare @y int

declare @s int

declare @w int

declare @zongshu int

select @y=COUNT(*) from fenshu where 学生学号=@xuehao and 语文分数>=60

select @s=COUNT(*) from fenshu where 学生学号=@xuehao and 数学分数>=60

select @w=COUNT(*) from fenshu where 学生学号=@xuehao and 英语分数>=60

set @zongshu=@y+@s+@w

if @zongshu=3

     print '优秀'

if @zongshu =2

     print '结业'

if @zongshu=1

     print'不结业'

if @zongshu=0

     print'输入错误'

end

go

exec biye 1

结果为:

SQLserver的存储过程

 

 

--------综合练习题

存储过程综合训练)

创建一个货物表:编号,货物名称,单位,价格,库存数量,备注。(10条数据)

之后,进货,如果已有此货,增加数量,否则,新增入数据库表中。

出货,如果有人要货,判断数量是否充足,充足减库存,否则告知不足。

根据名字随时删除数据库中的数据,有则删除,无则告知。

 

 

------------创建数据库及数据表,并插入数据----------

create database 笔记本

go

create table bijiben

(

   编号 int,

   名称 nvarchar(20),

   备注 varchar(20),

   价格 int,

   库存 int,

   单位 nvarchar(10)

)

go                       --------(随机排名)------

insert into bijiben values(1,'苹果','macbook',12000,10,'美国')

insert into bijiben values(2,'宏基','acer',3500,20,'中国台湾')

insert into bijiben values(3,'华硕','asus',3500,25,'中国')

insert into bijiben values(4,'戴尔','dell',4300,30,'美国')

insert into bijiben values(5,'神舟','hass',4000,20,'中国')

insert into bijiben values(6,'联想','lenovo',4200,30,'中国')

insert into bijiben values(7,'惠普','ph',3600,20,'美国')

insert into bijiben values(8,'三星','samsung',3700,10,'日本')

insert into bijiben values(9,'索尼','sony',7000,10,'日本')

insert into bijiben values(10,'东芝','toshiba',3200,10,'日本')

 

select *from bijiben

----------------------进货------------------------

create proc jinhuo --创建进货存储过程

@bianhao int,  --进货编号

@bjbn nvarchar(20),--笔记本名

@beizhu nvarchar(20),--备注

@jiage int,--价格

@jinhuo int,--进多少台

@danwei nvarchar(20)--单位

as

begin

   declare @ybjbn nvarchar(20),@ykc int  --@ykc为数据中的原有的库存数

   select @ybjbn=count(名称) from bijiben where 名称=@bjbn

   if @ybjbn=0  --当数据库中没有输入的数据时

   begin

     insert into bijiben values(@bianhao,@bjbn,@beizhu,@jiage,@jinhuo,@danwei)

     print'新电脑添加成功!'

   end

   else if @ybjbn=1  --当数据库中有输入的数据时

     begin

        select @ykc=库存 from bijiben where 名称=@bjbn

        set @ykc=@ykc+@jinhuo

        update bijiben set 库存=@ykc where 名称=@bjbn

        print'该电脑库存添加成功!'

     end

end

go

exec jinhuo 11,'戴尔','dell',4200,10,'美国'

----------------------出货------------------------

create proc chuhuo  --创建出货存储过程

@name nvarchar(20), --要出货的笔记本名称

@shuliang int       --出货的数量

as

begin

   declare @ygeshu int,@hgeshu int --@ygeshu为数据库原来的库存,@hgeshu交易后剩余的库存

   select @ygeshu=库存 from bijiben where 名称=@name

   if @shuliang>@ygeshu  --当出货的数量大于库存的数量时

     print'对不起,库存不足~~'

   else   

   begin 

     set @hgeshu=@ygeshu-@shuliang 

     update bijiben set 库存=@hgeshu where 名称=@name --修改交易后库存数

     print'交易成功!'

   end

end

go

exec chuhuo '苹果',11

---------------------------删除一款笔记本数据-------

create proc qingchu

@scbjbn nvarchar(20) --要删除的笔记本的名称

as

begin

   declare @sgeshu int   --要查找笔记本的个数

   select @sgeshu=COUNT(*) from bijiben where 名称=@scbjbn

   if @sgeshu=1

   begin

     delete from bijiben where 名称=@scbjbn

     print'该笔记本的数据删除成功!'

   end

   if @sgeshu=0

     print'未找到该名称的笔记本~~'

end

exec qingchu '苹果'

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools