First, create a stored procedure with parameters
to create a stored procedure with parameters must first declare the parameter in a stored procedure, each stored procedure parameters must be defined with a unique name. T-SQL variables with the same parameter name must be prefixed with @ and follow the rules for identifiers. When the user does not provide a value for this parameter may be used instead of a default value.
1.不带默认值的参数
创建一个参数不带默认值的存储过程,在调用该存储过程时,必须对存储过程中的所有参数进行赋值,如果有一个参数没有赋值,则无法调用该存储过程。例如:
[sql]
use db_student
- Create a stored procedure
create procedure proc_group
@ Course category varchar (20), - define the parameters
Credit int @
as
select * from course
where = @ Course category Course category and credits> Credits!
Execute the stored procedure with no parameters it is:
[sql]
use db_student
exec proc_group 'song', 8
If you do not follow the order of assignment can be written as:
[sql]
use db_student
exec proc_group @ = 8 credits, Course @ category = 'Basketball lesson'
2. parameters with default values
In SQL, we can constrain the default value for the field, during storage can also create the default parameter values. After adding an equal sign as long as the definition of the parameters, and default values can be written after the equal sign.
[sql]
- Create a stored procedure
use db_student
create procedure proc_group
@ Course category varchar (20) = 'physical education'
@ Credit int = 6
as
select * from course where Course category Course category and credits = @> @ credits
Performed with default values stored procedure parameter
[sql]
use db_student
exec proc_group @ = 8 credits
3. With the return parameter stored procedure
When creating a stored procedure, the output parameter can be used to create a stored procedure return value, for example:
[sql]
@a int output
If you create a stored procedure with a return parameter proc_group, then the SQL statement is as follows:
[sql]
- create a stored procedure with return value
create procedure proc_group
@ Course category carchar (20),
@ GPA int output - set the parameters with a return value
as