search
HomeDatabaseMysql TutorialSQL SERVER存储过程的使用

Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用。当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句。这样就可以提高存储过程的性能。 什么是存储过程: 存储过程Procedure是一组为了完成特定功能的

   Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用。当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句。这样就可以提高存储过程的性能。

  什么是存储过程:

  存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行。存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数、输出参数、返回单个或多个结果集以及返回值。由于存储过程在创建时即在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句块要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,所以在一定程度上也可以减少网络流量、简单网络负担。

  存储过程的优点:

  A、 存储过程允许标准组件式编程

  存储过程创建后可以在程序中被多次调用执行,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。

  B、 存储过程能够实现较快的执行速度

  如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析、优化,并给出最终被存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要预编译和优化,,所以速度就要慢一些。

  C、 存储过程减轻网络流量

  对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。

  D、 存储过程可被作为一种安全机制来充分利用

  系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。

  系统存储过程

  系统存储过程是系统创建的存储过程,目的在于能够方便的从系统表中查询信息或完成与更新数据库表相关的管理任务或其他的系统管理任务。系统存储过程主要存储在master数据库中,以“sp”下划线开头的存储过程。尽管这些系统存储过程在master数据库中,但我们在其他数据库还是可以调用系统存储过程。有一些系统存储过程会在创建新的数据库的时候被自动创建在当前数据库中。

  常用系统存储过程有:

  exec sp_databases; --查看数据库

  exec sp_tables; --查看表

  exec sp_columns student;--查看列

  exec sp_helpIndex student;--查看索引

  exec sp_helpConstraint student;--约束

  exec sp_stored_procedures;

  exec sp_helptext 'sp_stored_procedures';--查看存储过程创建、定义语句

  exec sp_rename student, stuInfo;--修改表、索引、列的名称

  exec sp_renamedb myTempDB, myDB;--更改数据库名称

  exec sp_defaultdb 'master', 'myDB';--更改登录名的默认数据库

  exec sp_helpdb;--数据库帮助,查询数据库信息

  exec sp_helpdb master;

  系统存储过程示例:

  --表重命名

  exec sp_rename 'stu', 'stud';

  select * from stud;

  --列重命名

  exec sp_rename 'stud.name', 'sName', 'column';

  exec sp_help 'stud';

  --重命名索引

  exec sp_rename N'student.idx_cid', N'idx_cidd', N'index';

  exec sp_help 'student';

  --查询所有存储过程

  select * from sys.objects where type = 'P';

  select * from sys.objects where type_desc like '%pro%' and name like 'sp%';

  用户自定义存储过程:

  1、创建语法

  create proc | procedure pro_name

  [{@参数数据类型} [=默认值] [output],

  {@参数数据类型} [=默认值] [output],

  ....

  ]

  as

  SQL_statements

  2、创建不带参数存储过程

  --创建存储过程

  if (exists (select * from sys.objects where name = 'proc_get_student'))

  drop proc proc_get_student

  go

  create proc proc_get_student

  as

  select * from student;

  --调用、执行存储过程

  exec proc_get_student;

  3、修改存储过程

  --修改存储过程

  alter proc proc_get_student

  as

  select * from student;

  4、带参数存储过程

  --带参存储过程

  if (object_id('proc_find_stu', 'P') is not null)

  drop proc proc_find_stu

  go

  create proc proc_find_stu(@startId int, @endId int)

  as

  select * from student where id between @startId and @endId

  go

  exec proc_find_stu 2, 4;

  5、带通配符参数存储过程

  --带通配符参数存储过程

  if (object_id('proc_findStudentByName', 'P') is not null)

  drop proc proc_findStudentByName

  go

  create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')

  as

  select * from student where name like @name and name like @nextName;

  go

  exec proc_findStudentByName;

  exec proc_findStudentByName '%o%', 't%';

  6、 带输出参数存储过程

  if (object_id('proc_getStudentRecord', 'P') is not null)

  drop proc proc_getStudentRecord

  go

  create proc proc_getStudentRecord(

  @id int, --默认输入参数

  @name varchar(20) out, --输出参数

  @age varchar(20) output--输入输出参数

  )

  as

  select @name = name, @age = age from student where id = @id and sex = @age;

  go

  --

  declare @id int,

  @name varchar(20),

  @temp varchar(20);

  set @id = 7;

  set @temp = 1;

  exec proc_getStudentRecord @id, @name out, @temp output;

  select @name, @temp;

  print @name + '#' + @temp;

  7、 不缓存存储过程

  --WITH RECOMPILE 不缓存

  if (object_id('proc_temp', 'P') is not null)

  drop proc proc_temp

  go

  create proc proc_temp

  with recompile

  as

  select * from student;

  go

  exec proc_temp;

  8、加密存储过程

  --加密WITH ENCRYPTION

  if (object_id('proc_temp_encryption', 'P') is not null)

  drop proc proc_temp_encryption

  go

  create proc proc_temp_encryption

  with encryption

  as

  select * from student;

  go

  exec proc_temp_encryption;

  exec sp_helptext 'proc_temp';

  exec sp_helptext 'proc_temp_encryption';

  9、带游标参数存储过程

  if (object_id('proc_cursor', 'P') is not null)

  drop proc proc_cursor

  go

  create proc proc_cursor

  @cur cursor varying output

  as

  set @cur = cursor forward_only static for

  select id, name, age from student;

  open @cur;

  go

  --调用

  declare @exec_cur cursor;

  declare @id int,

  @name varchar(20),

  @age int;

  exec proc_cursor @cur = @exec_cur output;--调用存储过程

  fetch next from @exec_cur into @id, @name, @age;

  while (@@fetch_status = 0)

  begin

  fetch next from @exec_cur into @id, @name, @age;

  print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);

  end

  close @exec_cur;

  deallocate @exec_cur;--删除游标

  10、分页存储过程

  ---存储过程、row_number完成分页

  if (object_id('pro_page', 'P') is not null)

  drop proc proc_cursor

  go

  create proc pro_page

  @startIndex int,

  @endIndex int

  as

  select count(*) from product

  ;

  select * from (

  select row_number() over(order by pid) as rowId, * from product

  ) temp

  where temp.rowId between @startIndex and @endIndex

  go

  --drop proc pro_page

  exec pro_page 1, 4

  --

  --分页存储过程

  if (object_id('pro_page', 'P') is not null)

  drop proc pro_stu

  go

  create procedure pro_stu(

  @pageIndex int,

  @pageSize int

  )

  as

  declare @startRow int, @endRow int

  set @startRow = (@pageIndex - 1) * @pageSize +1

  set @endRow = @startRow + @pageSize -1

  select * from (

  select *, row_number() over (order by id asc) as number from student

  ) t

  where t.number between @startRow and @endRow;

  exec pro_stu 2, 2;

  Raiserror返回用户定义的错误信息,可以指定严重级别,设置系统变量记录所发生的错误。

  语法如下:

  Raiserror({msg_id | msg_str | @local_variable}

  {, severity, state}

  [,argument[,…n]]

  [with option[,…n]]

  )

  # msg_id:在sysmessages系统表中指定的用户定义错误信息

  # msg_str:用户定义的信息,信息最大长度在2047个字符。

  # severity:用户定义与该消息关联的严重级别。当使用msg_id引发使用sp_addmessage创建的用户定义消息时,raiserror上指定严重性将覆盖sp_addmessage中定义的严重性。

  任何用户可以指定0-18直接的严重级别。只有sysadmin固定服务器角色常用或具有alter trace权限的用户才能指定19-25直接的严重级别。19-25之间的安全级别需要使用with log选项。

  # state:介于1至127直接的任何整数。State默认值是1。

  raiserror('is error', 16, 1);

  select * from sys.messages;

  --使用sysmessages中定义的消息

  raiserror(33003, 16, 1);

  raiserror(33006, 16, 1);

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
Vue3+TS+Vite开发技巧:如何进行数据加密和存储Vue3+TS+Vite开发技巧:如何进行数据加密和存储Sep 10, 2023 pm 04:51 PM

Vue3+TS+Vite开发技巧:如何进行数据加密和存储随着互联网技术的快速发展,数据的安全性和隐私保护变得越来越重要。在Vue3+TS+Vite开发环境下,如何进行数据加密和存储,是每个开发人员都需要面对的问题。本文将介绍一些常用的数据加密和存储的技巧,帮助开发人员提升应用的安全性和用户体验。一、数据加密前端数据加密前端加密是保护数据安全性的重要一环。常用

如何在 Windows 11 上清理缓存:详细的带图片教程如何在 Windows 11 上清理缓存:详细的带图片教程Apr 24, 2023 pm 09:37 PM

什么是缓存?缓存(发音为ka·shay)是一种专门的高速硬件或软件组件,用于存储经常请求的数据和指令,这些数据和指令又可用于更快地加载网站、应用程序、服务和系统的其他部分。缓存使最常访问的数据随时可用。缓存文件与缓存内存不同。缓存文件是指经常需要的文件,如PNG、图标、徽标、着色器等,多个程序可能需要这些文件。这些文件存储在您的物理驱动器空间中,通常是隐藏的。另一方面,高速缓存内存是一种比主内存和/或RAM更快的内存类型。它极大地减少了数据访问时间,因为与RAM相比,它更靠近CPU并且速度

如何安装、卸载、重置Windows服务器备份如何安装、卸载、重置Windows服务器备份Mar 06, 2024 am 10:37 AM

WindowsServerBackup是WindowsServer操作系统自带的一个功能,旨在帮助用户保护重要数据和系统配置,并为中小型和企业级企业提供完整的备份和恢复解决方案。只有运行Server2022及更高版本的用户才能使用这一功能。在本文中,我们将介绍如何安装、卸载或重置WindowsServerBackup。如何重置Windows服务器备份如果您的服务器备份遇到问题,备份所需时间过长,或无法访问已存储的文件,那么您可以考虑重新设置WindowsServer备份设置。要重置Windows

Windows Server 2025预览版迎来更新,微软改善Insiders测试体验Windows Server 2025预览版迎来更新,微软改善Insiders测试体验Feb 19, 2024 pm 02:36 PM

在发布WindowsServer的build26040版本之际,微软公布了该产品的官方名称:WindowsServer2025。一同推出的,还有Windows11WindowsInsiderCanaryChannel版本的build26040。有些朋友可能还记得,多年前有人成功将WindowsNT从工作站模式转换为服务器模式,显示微软操作系统各版本之间的共性。尽管现在微软的服务器操作系统版本和Windows11之间有明显区别,但关注细节的人可能会好奇:为什么WindowsServer更新了品牌,

PHP和swoole如何实现高效的数据缓存和存储?PHP和swoole如何实现高效的数据缓存和存储?Jul 23, 2023 pm 04:03 PM

PHP和swoole如何实现高效的数据缓存和存储?概述:在Web应用开发中,数据的缓存和存储是非常重要的一部分。而PHP和swoole提供了一种高效的方法来实现数据的缓存与存储。本文将介绍如何使用PHP和swoole来实现高效的数据缓存和存储,并给出相应的代码示例。一、swoole简介:swoole是一个针对PHP语言开发的,高性能的异步网络通信引擎,它可以

使用PHP数组实现数据缓存和存储的方法和技巧使用PHP数组实现数据缓存和存储的方法和技巧Jul 16, 2023 pm 02:33 PM

使用PHP数组实现数据缓存和存储的方法和技巧随着互联网的发展和数据量的急剧增长,数据缓存和存储成为了我们在开发过程中必须要考虑的问题之一。PHP作为一门广泛应用的编程语言,也提供了丰富的方法和技巧来实现数据缓存和存储。其中,使用PHP数组进行数据缓存和存储是一种简单而高效的方法。一、数据缓存数据缓存的目的是为了减少对数据库或其他外部数据源的访问次数,从而提高

怎么修改Nginx版本名称伪装任意web server怎么修改Nginx版本名称伪装任意web serverMay 14, 2023 pm 09:19 PM

如何修改nginx默认的名称,可以稍微的伪装一下,也可以装x一般来说修改3个位置,一个是nginx.h、另一个是ngx_http_header_filter_module.c、还有一个ngx_http_special_response.c。提示:一般修改都是在nginx编译之前修改,修改完了之后需要重新编译代码如下:scr/core/nginx.conf#definenginx_version"1.4.7"#definenginx_ver"nginx/"n

一文读懂人工智能表:从MindsDB说起一文读懂人工智能表:从MindsDB说起Apr 12, 2023 pm 12:04 PM

本文转载自微信公众号「活在信息时代」,作者活在信息时代。转载本文请联系活在信息时代公众号。对于熟悉数据库操作的同学来说,编写优美的SQL语句,从数据库中想方设法找出自己需要的数据,是常规操作了。而对于熟悉机器学习的同学来说,获取数据,对数据进行预处理,建立模型,确定训练集和测试集,用训练好的模型对未来进行一系列的预测,也是一种常规操作了。那么,我们能否将两种技术结合起来呢?我们看到数据库里存储了数据,而进行预测需要基于以往的数据。如果我们通过数据库里现有的数据,对于未来的数据进行查询的话,那么是

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.