The stored procedure (stored procedure) is sometimes called sproc, and it is a real script-or, more precisely, a batch-it is stored in a database rather than in a faded file. In any case, this comparison is not very certain. The stored procedure has output parameters, input parameters and return values, and so on. The script does not have this content.
Stored procedure Basic syntax: CREATE procedure| PROC <sproc name>
[<parameter name> <data type> [Varying][<default value>] [OUTPUT]],
[<parameter name> <data type> [Varying][<default value>] [OUTPUT]]
[..., N]
[With pecompile| encryption| EXECUTE as{CALLER | Self| owner|< ' user name ';}]
[For REPLICATION]
As
<code>| EXTERNAL name<assembly name>.<assembly class>.<method>
Try the simplest basic stored procedure:
Use AdventureWorks GO -- switch to the AdventureWorks database CREATE PROCEDURE Sp_employee as SELECT * from HumanResources.Employee GO -- process the statement in advance. Prevent the following exec Sp_employee from throwing an error exec sp_employee
It seems to be not very simple. Returns a data table. Stored procedure with no parameters.
Use alter to modify the stored procedure.
In using T-SQL to edit stored procedures, it is important to note that this is a complete replacement of existing stored procedures. The only disadvantage of using the ALTER PROC or CREATE PROC statement includes the following points:
1.ALTER PROC: Expected to find an existing stored procedure, and create is not.
2.ALTER PROC: Retains any permissions that have been established on the stored procedure. It retains the same object ID in the system object and allows the retention of dependencies. For example, procedure A calls procedure B and removes and re-creates process B, so you cannot see the dependency between the two. If you use Alter, the dependency still exists.
3.ALTER PROC: Keep Any dependency information on other objects that might invoke the modified stored procedure (this is also the most important).
Delete a stored procedure: this is the simplest. or delete objects such as database, table, and view general statements,
DROP proc| PROCEDURE <sporcedure name> to complete the entire removal work.
Use parameterized stored procedures:
The declaration parameter is required for the following 2-4 information: name, data type, default value, reverse. Its syntax: @parameter_name [as] datatype [=defalut| NULL] [VARYING] [output| Out]
Create a stored procedure with a different version of the previous one
UseAdventureWorksGO --switch to the AdventureWorks databaseCREATE PROCEDUREsp_contact@LastName nvarchar( -) as SELECT * fromPerson.ContactWHERELastName like '%'+@LastName+'%'execSp_contact--No value provided,MSG 201, Level 16, State 4, procedure sp_contact, line No. 0 procedure or function ' Sp_contact ' requires parameter ' @LastName ', but the parameter is not provided. execSp_contact'AD' --Query completed a total of 233 information
How to get output parameters and return values: This is used in paging queries. Look at the stored procedure first
CREATE PROCEDURESp_contactpage@pageStart int,@pageEnd int,@pageCount intOUTPUT as begin SELECT @pageCount=Count(1) fromPerson.Contact;--gets the total number of bars. and set the value of the @pagecount withContact as(SelectRow_number () Over(Order byContactIDdesc) asRowNumber,* fromperson.contact)SELECT * fromContactwhereRowNumberbetween @pageStart and @pageEnd;--for paged queries, there is a simpler paging query in SQL Server 2012. This is supported after SQL SERVER 2005. 2000 subqueries are required. The missing subqueries and indexes are described later in the blog, which are SQL scripts. End Declare @rowcount int execSp_contactpage0, -,@rowcountOutputSelect @rowcount
View Code
How to use the return value to get the error result:
CREATE PROCEDURESp_errorlogin@username nvarchar( -), @userpass nvarchar( -) asbegin if(@username!='Admin') return -1;--indicates that the user name does not exist (for the convenience of not using the database) if(@userpass!='Admin') return 1;--Password Error return 0;--Login SuccessfulEndDeclare @status int ;exec @status=Sp_errorlogin'Admin','222'Select @status as 'Password Error'exec @status=Sp_errorlogin'222','222'Select @status as 'Account Error'exec @status=Sp_errorlogin'Admin','Admin'Select @status as 'Login Successful'
View Code
The following section deals with exception handling at @ @Error and try....cacth, and I put them in the business. Because a transaction error occurs, it rollback tran.
Recursion: Programming rarely uses recursion, and sometimes recursion is the most effective solution, such as a menu tree. Recursion refers to a situation where the code itself calls itself, and the danger is obvious-that is, the invocation of a dead loop. How the call depends on you or the code. SQL recursive calls can be called up to 32 times, and an exception is thrown over 32 times.
The most classic is factorial such as: 5 factorial 120 How it is implemented 5*4*3*2*1.
CREATE procSP_JX@size int ,@count intOutput as Declare @temp_s int ; Declare @temp_c int ; if @size >=1 begin Select @temp_s=@size-1; execSp_jx@temp_s,@temp_cOutputSelect @count= @size*@temp_c; End Else Select @count=1 return GODeclare @count int execSp_jx5,@countOutputSelect @countexecSp_jx +,@countOutputSelect @count
View Code
Time to use stored procedures: (Pros)
1. Generally better performance.
2. Can act as a security isolation layer (control data access and new ways)
3. Code can be reused
4. Dividing the Code
5. Flexible execution based on dynamics established at run time
Disadvantages :
Poor portability, no cross-platform migration
In some cases, it may be locked out due to the wrong execution plan (which actually affects performance).
Not to be continued.
SQL Getting Started Classic (eight) stored procedures