如何在sqlserver中写存储过程
original link http://www.codeproject.com/Articles/126898/Sql-Server-How-to-write-a-Stored-procedure-in-Sql 存储过程在sqlserver 中被定义成一组有逻辑关系的sql语句 ,他们实现了一些特定的任务。使用存储过程有很多好处。最主要的优点是提高数据库性能。 还有如下好处
假设有一张表叫tbl_Students 结构如下: ?? CREATE TABLE tbl_Students ( [Studentid] [int] IDENTITY(1,1) NOT NULL,[Firstname] [nvarchar](200) NOT NULL,[Lastname] [nvarchar](200) NULL,[Email] [nvarchar](100) NULL ) 我们添加以下数据到上表 insert into tbl_Students n(Firstname.Lastname,email) values('Vivek','Johari','vivek@abc.com') Insert into tbl_Students (Firstname,lastname,Email) Values('Pankaj','Kumar','pankaj@abc.com') Insert into tbl_Students (Firstname,Email) Values('Amit','Singh','amit@abc.com') Insert into tbl_Students (Firstname,Email) Values('Manish','manish@abc.comm') Insert into tbl_Students (Firstname,Email) Values('Abhishek','abhishek@abc.com') 现在我们开始写存储过程,第一步是创建存储过程语句 Create Procedure Procedure-name ( Input parameters,Output Parameters (If required) ) As Begin Sql statement used in the stored procedure End
/* Getstudentname is the name of the stored procedure*/ Create PROCEDURE Getstudentname( @studentid INT --Input parameter,Studentid of the student ) AS BEGIN SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END
/* GetstudentnameInOutputVariable is the name of the stored procedure which uses output variable @Studentname to collect the student name returns by the stored procedure */ Create PROCEDURE GetstudentnameInOutputVariable ( @studentid INT,--Input parameter,Studentid of the student @studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT keyword ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END
/* Stored Procedure GetstudentnameInOutputVariable is modified to collect the email address of the student with the help of the Alert Keyword */ Alter PROCEDURE GetstudentnameInOutputVariable ( @studentid INT,Studentid of the student @studentname VARCHAR (200) OUT,-- Output parameter to collect the student name @StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname,@StudentEmail=email FROM tbl_Students WHERE studentid=@studentid END 注意 并不是所有的存储过程都需要写入返回值?,例如一些插入 删除 更新的语句如下 ? /* This Stored procedure is used to Insert value into the table tbl_students. */ Create Procedure InsertStudentrecord ( @StudentFirstName Varchar(200),@StudentLastName Varchar(200),@StudentEmail Varchar(50) ) As Begin Insert into tbl_Students (Firstname,Email) Values(@StudentFirstName,@StudentLastName,@StudentEmail) End Sqlserver中执行存储过程 ? sqlserver中的存储过程可以用execute 或exec 来执行。例如执行存储过程Getstudentname Execute Getstudentname 1Exec Getstudentname 1 ? 执行,带有output参数的存储过程,首先我们要声明变量来容纳参数,例如 Declare @Studentname as nvarchar(200) -- Declaring the variable to collect the Studentname Declare @Studentemail as nvarchar(50) -- Declaring the variable to collect the Studentemail Execute GetstudentnameInOutputVariable 1,@Studentname output,@Studentemail output select @Studentname,@Studentemail -- "Select" Statement is used to show the output from Procedure 结 束语 ?? 我们可以说储过程不但可以增强代码的可复用执行,还可以通过减少网络数据来提高数据库性能! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- SQL Server 2008新特性——FILESTREAM
- sql – Null中有多少种语言不等于甚至没有Null?
- MySQL数据库导出与导入及常见错误解决
- Mysql建表与索引使用规范详解
- 如何在SQL查询中处理可选参数?
- 使用具有WHERE条件的单个查询(SQL Express 2005)从多个表中
- sqlserver重建所有索引的存储过程
- sql-server – SQL Server sp_msforeachtable用法,仅选择满
- sql-server-2008 – 如何让SQL Server Management Studio 2
- SQL Server 2005 For XML Explicit – 需要帮助格式化