Mssql Stored Procedure entry and instance application

Source: Internet
Author: User
Mssql Stored Procedure introduction and instance application * First, let's take a look at the mssql Stored Procedure creation createprocedureproc_stuasselect * fromstudentgo creation process: Example of the following statement created in the architecture of the Human Resources Program remove_emp: Employee) mssql Stored Procedure entry and instance application
/*

First, let's take a look at mssql Stored Procedure Creation

Create procedure proc_stu
As
Select * from student
Go

Create a process: Example
The following statement creates the Human Resource program remove_emp in the architecture:

Create procedure remove_emp (employee_id NUMBER)
Tot_em NUMBER;
BEGIN
Delete from employees
WHERE employees. employee_id = remove_emp.employee_id;
Tot_emps: = tot_emps-1;
END;



For a simple example of the stored procedure, let's look at the syntax

CREATE {PROC | PROCEDURE} [schema_name.] procedure_name [; number]
[{@ Parameter [type_schema_name.] data_type}
[VARYING] [= default] [OUT | OUTPUT] [READONLY]
] [,... N]
[ [,... N]
[For replication]
AS { [;] [... N] | }
[;]
: =
[ENCRYPTION]
[RECOMPILE]
[Execute as Clause]

: =
{[BEGIN] statements [END]}

: =
External name assembly_name.class_name.method_name

Let's take a look at an instance in an instance application,
Query the Stored Procedure instance of a record with the id of 1

@ Total int OUTPUT
-----------------------------------
SET @ SQL = n' select a, B, c, d from t where id = 1'
Exec sp_executesql @ SQL, Int Out ', @ Total Out
-----------------------------------
Return @ Total


Instance 3

Add a record to the table book and query the total amount of all books in the table

Create proc insert_book
@ Param1 char (10), @ param2 varchar (20), @ param3 money, @ param4 money output
With encryption --------- encryption
As
Insert book (number, title, price) Values (@ param1, @ param2, @ param3)
Select @ param4 = sum (price) from book
Go
Example:
Declare @ total_price money
Exec insert_book '003 ', 'delphi control development Guide', $100, @ total_price
Print 'total amount is '+ convert (varchar, @ total_price)
Go


*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.