Stored Procedures in Transact-SQL are very similar to methods in Java. They can be called repeatedly. After the stored procedure is executed once, you can cache the statements so that the statements in the cache are directly used for the next execution. This improves the performance of stored procedures. Stored Procedure concept Stored Procedure is a set of SQ
Stored Procedures in Transact-SQL are very similar to methods in Java. They can be called repeatedly. After the stored procedure is executed once, you can cache the statements so that the statements in the cache are directly used for the next execution. This improves the performance of stored procedures. Stored Procedure concept Stored Procedure is a set of SQ
Stored Procedures in Transact-SQL are very similar to methods in Java. They can be called repeatedly. After the stored procedure is executed once, you can cache the statements so that the statements in the cache are directly used for the next execution. This improves the performance of stored procedures.
Concept of Stored Procedure
Stored Procedure is a set of SQL statements for specific functions. Compiled and stored in the database. You can specify the name of the stored Procedure and provide parameters for execution.
Stored Procedures can contain logical control statements and data manipulation statements. They can accept parameters, output parameters, return one or more result sets, and return values.
Since the stored procedure is compiled on the database server and stored in the database at the time of creation, the stored procedure runs faster than a single SQL statement block. At the same time, because you only need to provide the stored procedure name and necessary parameter information during the call, it can also reduce network traffic and simple network burden to a certain extent.
1. Advantages of Stored Procedures
A. stored procedures allow standard component programming
After a stored procedure is created, it can be called and executed multiple times in the program without re-writing the SQL statement of the stored procedure. Database professionals can modify the stored procedure at any time, but it has no impact on the application source code, which greatly improves the program portability.
B. Fast execution of Stored Procedures
If an operation contains a large number of T-SQL statement codes that are executed multiple times, the stored procedure is much faster than the batch execution. Because the stored procedure is pre-compiled, when a stored procedure is run for the first time, the query optimizer analyzes and optimizes the stored procedure and provides the storage plan that is finally stored in the system table. The T-SQL Statement of batch processing needs to be pre-compiled and optimized every time, so the speed will be slower.
C. reduce network traffic through stored procedures
For the same database object operation, if the T-SQL statements involved in this operation are organized into a stored procedure, when the stored procedure is called on the client, only this call statement is passed in the network; otherwise, multiple SQL statements are passed. This reduces network traffic and network load.
D. stored procedures can be fully utilized as a security mechanism
The system administrator can restrict the permissions of a stored procedure to prevent unauthorized users from accessing data and ensure data security.
System stored procedures
A system stored procedure is a stored procedure created by the system. It is designed to conveniently query information from the system table or complete management tasks related to updating database tables or other system management tasks. The system stored procedures are mainly stored in the master database and stored procedures starting with "sp. Although these system stored procedures are in the master database, we can still call the system stored procedures in other databases. Some system stored procedures are automatically created in the current database when a new database is created.
Common system stored procedures include:
Exec sp_databases; -- View database exec sp_tables; -- View table exec sp_columns student; -- View column exec sp_helpIndex student; -- View index exec sp_helpConstraint student; -- restrict exec sp_stored_procedures; exec sp_helptext 'SP _ stored_procedures '; -- view the stored procedure creation and Definition Statement exec sp_rename student, stuInfo; -- modify the table, index, and column name exec sp_renamedb myTempDB, myDB; -- change the database name exec sp_defaultdb 'master', 'mydb'; -- change the default database exec sp_helpdb for the login name; -- Database Help, query the database information exec sp_helpdb master;
System Stored Procedure example:
-- Rename the table exec sp_rename 'stu', 'stud'; select * from stud;
-- Rename the exec sp_rename 'stud. name', 'sname', 'column'; exec sp_help 'stud ';
-- Rename the index exec sp_rename N 'student. idx_cid ', N 'idx _ cid', N 'index'; exec sp_help 'student ';
-- Query all stored procedures select * from sys. objects where type = 'P'; select * from sys. objects where type_desc like '% pro %' and name like 'SP % ';
User-Defined stored procedures
1. Create a syntax
Create proc | procedure pro_name
[{@ Parameter data type} [= default value] [output],
{@ Parameter data type} [= default value] [output],
....
] As SQL _statements
2. Create a stored procedure without Parameters
-- Create a stored procedure 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;
-- Call and execute the Stored Procedure exec proc_get_student;
3. Modify the Stored Procedure
-- Modify the Stored Procedure alter proc proc_get_student as select * from student;
4. Stored Procedure with Parameters
-- 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. Stored Procedures with wildcard Parameters
-- 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. Stored Procedure with output parameters
If (object_id ('proc _ getStudentRecord ', 'P') is not null) drop proc proc_getStudentRecord go create proc proc_getStudentRecord (
@ Id int, -- default input parameter
@ Name varchar (20) out, -- output parameter
@ Age varchar (20) output -- Input and output Parameters
) 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. Non-Cache stored procedures
-- 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. Encrypted storage process