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 you have completed the maintenance phase of a project, you will find that the stored procedure has brought us benefits. It is easy to modify and cannot be changed to our application. Program , You only need to change the content of the stored procedure, but also improve the speed of our program.
Reference: |
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:
Reference: |
Create proc stored procedure name [Parameter list (multiple parameters are separated by commas (,)] As SQL statement |
|
Example:
Reference: |
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. As long as the above Code Run the query analyzer once. 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.
Reference: |
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.
Reference: |
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 = @ strloginnameIf @ 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.
Reference: |
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.
Reference: |
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.
reference: |
dim adocomm dim adort '// create an object, we used to call the Stored Procedure set adocomm = Createobject ("ADODB. command ") set adors = Createobject (" ADODB. recordset ") with adocomm '// sets the connection and sets adoconn to the connected ADODB. connection object . activeconnection = adoconn '// The type is stored procedure, and adw.storedproc = 4 . commandtype = 4 '// stored procedure name . commandtext = "upgetuserinfos" '// set the user group . parameters. item ("@ intusergroup "). value = 1 '// executes 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 with '// release object set adors = nothing set adocomm = nothing |
|
How can this problem be solved? Is it easy? However, stored procedures are not only useful. They also have more powerful functions, such as using cursors and temporary tables to access multiple tables, you can even call data in multiple databases and then return the data to the user, which can be explored slowly during use.
Well, the stored procedure application will be introduced here first. If you have any questions in this article, please follow up and I will try my best to answer them. Next time, I will introduce the trigger application.