SQL Server Stored Procedures

Source: Internet
Author: User
Tags sql server books
Question: SQL Server database development-stored procedure application
Author: cultivator
Note: Due to limited personal abilities, errors or omissions may inevitably occur in the article. Please forgive me! At the same time, you are welcome to point out so that I can modify it in time to avoid misleading the next viewer. Finally, I hope this article will help you.

Some may have been using SQL Server for development for some time, but they have not or seldom used stored procedures in projects. Some may think that there is no need to use stored procedures. In fact, when a project is completed in the maintenance phase, you will find that the stored procedure has brought us benefits. It is easy to modify and cannot change our applications, you only need to modify the content of the stored procedure, and increase the speed of our program. Definitions in SQL Server books online:
Stored procedures are a set of statements that can be saved to accept and return user-supplied parameters.
You can create a process for permanent use, or temporary use in a session (Partial temporary process), or temporary use in all sessions (Global temporary process ).
You can also create a stored procedure that runs automatically when Microsoft SQL Server is started.

To use stored procedures, first we must be familiar with some basic T-SQL statements, because stored procedures are made up of a set of T-SQL statements, and we need to understand some concepts about functions, procedures, because we need to call the stored procedure in the application, just as we call the function of the application, but the calling method is somewhat different.

The following describes how to establish and use a stored procedure.

1. Create a stored procedure

Like a data table, we need to create a stored procedure before using it. Its concise syntax is:

Create proc stored procedure name
[Parameter list (multiple parameters are separated by commas (,)]
As
SQL statement

Example:

Create proc upgetusername
@ Intuserid int,
@ Ostrusername nvarchar (20) Output -- parameters to be output
As
Begin
-- Assign the uname value to the @ ostrusername variable, that is, the parameter to be output.
Select @ ostrusername = uname from uuser where uid = @ intuserid
End

The create proc statement (the complete statement is create procedure) tells SQL server that a stored procedure needs to be created now. upgetusername is the name of the stored procedure, @ intuserid and @ ostrusername are the two parameters of the stored procedure respectively. Note that in SQL Server, all User-Defined variables start, the output keyword indicates that this parameter is used for output. As is followed by the Stored Procedure content. If you run the preceding code once in the "query analyzer", SQL Server creates a stored procedure named "upgetusername" in the current database. You can open "Enterprise Manager", select the database for the current operation, and select "Stored Procedure" in the tree list on the left ", now you can see the stored procedure you just created in the list on the right (if not, refresh it ).

Ii. Call of Stored Procedures

Previously, we have created a stored procedure named "upgetusername". The function of this stored procedure is to obtain the name of a user. After the storage process is set up, the next step is to call it in the application. Next, let's take a look at the call in the ASP program. Dim adocomm
'// Create an object for calling the Stored Procedure
Set adocomm = Createobject ("ADODB. Command ")
With adocomm
'// Set the connection and set adoconn to the connected ADODB. connection object
. Activeconnection = adoconn
'// The type is stored in the process, admo-storedproc = 4
. Commandtype = 4
'// Stored procedure name
. Commandtext = "upgetusername"
'// Set the user ID
. Parameters. Item ("@ intuserid"). value = 1
'// Execute the Stored Procedure
. Execute

'// Get the name of the user returned from the Stored Procedure
Response. Write "username:" &. Parameters. Item ("@ ostrusername"). Value
End
'// Release the object
Set adocomm = nothing

Through the above two steps, we can create and use simple stored procedures. Next we will look at a slightly more complex stored procedure to further understand the application of the stored procedure.

Iii. Practical Application of Stored Procedures

User Login is often used in ASP projects. I believe many of my friends have done similar systems, but there may not be many friends who use stored procedures for verification. We will use it as an example, write a simple Stored Procedure for user login verification.

Create proc upuserlogin
@ Strloginname nvarchar (20 ),
@ Strloginpwd nvarchar (20 ),
@ Blnreturn bit output
As
-- Defines a variable temporarily used to save the password
Declare @ strpwd nvarchar (20)
Begin
-- Query the password of the current user from the table and assign the value to the @ strpwd variable. We will compare it below
Select @ strpwd = uloginpwd from uuser where uloginname = @ strloginname

If @ strloginpwd = @ strpwd
Begin
Set @ blnreturn = 1
-- Updates the last logon time of a user.
Update uuser set ulastlogin = getdate () Where uloginname = @ strloginname
End
Else
Set @ blnreturn = 0
End

The stored procedure for user login has been set up. Now let's try it in the program. NOTE: If multiple statements exist in a region, you must use the begin... end keyword.

Dim adocomm
'// Create an object for calling the Stored Procedure
Set adocomm = Createobject ("ADODB. Command ")
With adocomm
'// Set the connection and set adoconn to the connected ADODB. connection object
. Activeconnection = adoconn
'// The type is stored in the process, admo-storedproc = 4
. Commandtype = 4
'// Stored procedure name
. Commandtext = "upuserlogin"
'// Set the logon name
. Parameters. Item ("@ strloginname"). value = "admin"
'// Set the logon Password
. Parameters. Item ("@ strloginpwd"). value = "123456"
'// Execute the Stored Procedure
. Execute

'// Determine whether the logon is successful
If. Parameters. Item ("@ blnreturn"). value = 1 then
Response. Write "Congratulations! login successful! "
Else
Response. Write "no, it seems wrong... "
End if
End
'// Release the object
Set adocomm = nothing

Through the above steps, the simple user login verification process is also completed. Now, you only need to integrate it into the program to implement simple user login verification, you can handle other details by yourself.
The two stored procedures described above return only one value. Let's look at a stored procedure that returns a record set. Create proc upgetuserinfos
@ Intusergroup int
As
Begin
-- Extract qualified data from the database
Select uname, ugroup, ulastlogin from uuser where ugroup = @ intusergroup
-- Insert a column total
Union
Select 'Total number of students: ', count (ugroup), null from uuser where ugroup = @ intusergroup
End

Now let's take a look at ASP program calls.

Dim adocomm
Dim adort
'// Create an object for calling the Stored Procedure
Set adocomm = Createobject ("ADODB. Command ")
Set adors = Createobject ("ADODB. recordset ")
With adocomm
'// Set the connection and set adoconn to the connected ADODB. connection object
. Activeconnection = adoconn
'// The type is stored in the process, admo-storedproc = 4
. Commandtype = 4
'// Stored procedure name
. Commandtext = "upgetuserinfos"
'// Set the user group
. Parameters. Item ("@ intusergroup"). value = 1
'// Execute the stored procedure. Unlike the preceding examples, the recordset open method is used here.
Adors. Open adocomm
'// Display the first value
Response. Write adors. Fields (0). Value
End
'// Release the object
Set adors = nothing
Set adocomm = nothing

Stored procedures are not only useful. They also have more powerful functions, such as using cursors, temporary tables to call data from multiple tables, or even multiple databases, and then return it to the user, which you can explore slowly during use.


 

Related Article

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.