CREATE { PROCEDURE | PROC } [schema_name.]procedure_name
[ @parameter [type_schema_name.] datatype
[ VARYING ] [ = default ] [ OUT | OUTPUT | READONLY ]
, @parameter [type_schema_name.] datatype
[ VARYING ] [ = default ] [ OUT | OUTPUT | READONLY ] ]
[ WITH { ENCRYPTION | RECOMPILE | EXECUTE AS Clause } ]
[ FOR REPLICATION ]
AS
BEGIN
[declaration_section]
executable_section
END;
Description:
schema_name
The name of the schema that owns the stored procedure.
procedure_name
The name to assign to this procedure in SQL Server.
@parameter
One or more parameters passed into the procedure.
type_schema_name
The schema that owns the data type, if applicable.
datatype
The data type for @parameter.
VARYING
It is specified for cursor parameters when the result set is an output parameter.
default
The default value to assign to @parameter.
OUT
It means that @parameter is an output parameter.
OUTPUT
It means that @parameter is an output parameter.
READONLY
It means that @parameter can not be overwritten by the stored procedure.
ENCRYPTION
It means that the source for the stored procedure will not be stored as plain text in the system views in SQL Server.
RECOMPILE
It means that a query plan will not be cached for this stored procedure.
EXECUTE AS clause
It sets the security context to execute the stored procedure.
FOR REPLICATION
It means that the stored procedure is executed only during replication.
Example :
CREATE PROCEDURE ExamplePro
@result VARCHAR(50) OUT
AS
BEGIN
DECLARE @id INT;
SET @id = 8;
IF @id < 10
SET @result = 'This is Sample';
ELSE
SET @result = 'This also sample';
END;
Functions
Syntax:
CREATE FUNCTION [schema_name.]function_name
( [ @parameter [ AS ] [type_schema_name.] datatype
[ = default ] [ READONLY ]
, @parameter [ AS ] [type_schema_name.] datatype
[ = default ] [ READONLY ] ]
)
RETURNS return_datatype
[ WITH { ENCRYPTION
| SCHEMABINDING
| RETURNS NULL ON NULL INPUT
| CALLED ON NULL INPUT
| EXECUTE AS Clause ]
[ AS ]
BEGIN
[declaration_section]
executable_section
RETURN return_value
END;
Three types of user defined functions.
1.Scalar Function
User defined scalar function also returns single value as a result of actions perform by function. We return any datatype value from function.
2.Inline Table-Valued Function
User defined inline table-valued function returns a table variable as a result of actions perform by function. The value of table variable should be derived from a single SELECT statement.
3.Multi-statement table-valued function
User defined multi-statement table-valued function returns a table variable as a result of actions perform by function. In this a table variable must be explicitly declared and defined whose value can be derived from a multiple sql statements.
In this blog, I tried to explain the difference between the stored procedure and the function. I hope after reading this blog, you will be able to understand the difference between the stored procedure and functions. The comments, criticism and suggestions are always welcome.