SQL Server 2005 Create a simple stored procedure--Summary analysis _mssql

Source: Internet
Author: User

Recently due to the need for work, a simple understanding of the next SQL Server 2005 database creation is simple in stored procedures.
First, explain how to create a stored procedure:
CREATE proceduer my_pro @inputDate varchar,
DECLARE input variable @Result varchar (255) output
Declaring output variables as declare @variable1 varchar (255)
Declaring varchar variable declare @variable2 int
Declare the shaping variable BEGIN IF ... Conditions BEGIN .... (Execution content) End ELSE BEGIN .... (Execution content) End End
The above is to create a simple stored procedure method.

Second, delete stored procedures drop PROCEDURE My_pro

Iii. Executing stored procedures
(1) Execution of stored procedures without parameters: Execute My_pro
(2) Execution of stored procedures with parameters:
EXECUTE my_pro ' input variable '
(1) Execution of stored procedures with parameters and return values (that is, output variables):
DECLARE @Result varchar (1024)
Declaring output variable EXECUTE my_pro ' input variable ' @Result output
To save the stored procedure output value to @result select @Result as Result
Query results return

The example below needs to query multiple records results, using a return value to return.
For example, in table test, where two fields are Test01 and Test02, the Test01 > 10000 records are merged and the query Test02 results merged into a single record to return.
The return result is: WSXEDCRFVTGB. Test01 Test02 10000 QAZ 10001 WSX 10002 EDC 10003 RFV 10004 Then, using a stored procedure, using a cursor in a stored procedure, a for loop can be used to merge multiple records. will be able to solve this problem.
The specific stored procedures are as follows:
DROP porcedure My_cursor
Habitually, before creating, first look at whether the stored procedure exists a create PROCEDURE my_cursor @id int,
Declaring input variables
@Result varchar (255) output
Declare output variable, note must have output keyword, otherwise the default is input variable as Declare city_cursor cursor for
– Declare a cursor variable Select [Test01] from Test where Test01 > @id
– Where @id is the input variable Set @Result = '
– Set Variable @result value Declare @Field int
– Declare the variable Open city_cursor that temporarily holds the query to Test01
– Open the cursor Fetch next from city_cursor into @Field
– Assign the actual Test01 to a variable, loop while (@ @fetch_status =0)
– Loop begins If @Result = ' BEGIN Select @Result = Test02 from Test where Test01 = @Field end ELSE BEGIN Select @Result = @Result + ', ' + Test02 from Test where Test01 = @Field end Fetch Next from City_cursor into @Field
– Loop next Test01 End Close City_cursor
– Close the cursor deallocate city_cursor
– Release cursor reference go (end) The following is execution Declare @Result varchar (1024) Execute My_pro 10000, @Result output
–output keyword must be written, if it is more than one input parameter, then use the "," number to distinguish the Select @Result as result– query results

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.