- Input
- Output or Out
- InputOutput
- Return
But we can specify only OUT or OUTPUT as parameter direction for the Procedure or Function.
RETURN at the last statement of the Procedure or Function and not in the parameter declaration of the Procedure or Function.
IN
DECLARE @ParentID int SET @ParentID =6 exec [dbo].[GetTestTable] @ParentID ALTER PROCEDURE [dbo].[GetTestTable]( @ParentID INT) AS BEGIN SET @ParentID =10 SELECT @ParentID END
We can’t able to specify in any where during the execution or in the SP parameter direction.
DECLARE @ParentID int SET @ParentID =6 exec [dbo].[GetTestTable] @ParentID IN –Not allowed ALTER PROCEDURE [dbo].[GetTestTable]( @ParentID INT IN) -–Not allowed AS BEGIN SELECT @ParentID END
Incorrect syntax near the keyword 'in'.
OUTPUT
Output is not only for sending value back to the caller, it also accepts input from the caller. Since by default OUT implies both IN and OUT.
E.g. 1
ALTER PROCEDURE [dbo].[GetTestTable]( @ParentID INT OUTPUT) AS BEGIN SET @ParentID = @ParentID + 5 END DECLARE @ParentID int SET @ParentID =6 exec [dbo].[GetTestTable] @ParentID OUT PRINT @ParentID
Result: 11
There is no need of RETURN or SELECT statement for the OUT direction parameter. Just assing the value, we will get the result.
If we didn’t assing a new value, we will get only the value we sent during execution of the Procedure or Function.(i.e. 6 for the above condion.)
If we didn’t sent any value during execution of the Procedure or Function we will get only nothing for the below condion.
DECLARE @ParentID int exec [dbo].[GetTestTable] @ParentID OUT PRINT @ParentID
E.g. 2
ALTER PROCEDURE [dbo].[GetTestTable]( @ParentID INT OUTPUT) AS BEGIN SET @ParentID =10 SELECT @ParentID ENDNo need to pass value. Just specify the keyword OUT near the parameter.
DECLARE @ParentID INT EXEC [dbo].[GetTestTable] @ParentID OUT PRINT @ParentID
Result: 10
For the above Procedure or Function we are assigning value to the parameter and also we are using SELECT statement. Here we will get a result-set and aslo the value assigned to our argument.
RETURN
There should be a RETURN statement at the end of the Procedure or Function.
ALTER PROCEDURE [dbo].[GetTestTable]( @ParentID INT OUTPUT) AS BEGIN SET @ParentID =10 RETURN @ParentID END DECLARE @ParentID INT DECLARE @ReturnValue INT SET @ParentID =6 EXEC @ReturnValue = [dbo].[GetTestTable] @ParentID PRINT @ReturnValue
If there is no RETURN statemnt at the end of the Procedure or Function, we will get only the default value of the RETURN argument datatype.
For example there is no RETURN statemnt for the above Procedure the result will be 0.
We can’t able to return a table variable from a RETURN statement.
Note:
- By default OUT implies both IN and OUT.
- Since our parameter is OUT we can pass value, since OUT implies both IN and OUT.
- The default parameter direction is IN.
- If the last statement is just RETURN without any value or a variable we will get
nothing (if we didn’t pass value to the parameter of assign) or default value (if
we assign value when passing). This is default for all the parameter direction.
These all are wrong.
@ReturnValue EXEC [dbo].[GetTestTable] @ParentID
Line 4: Incorrect syntax near '@ReturnValue'.
@ReturnValue = EXEC [dbo].[GetTestTable] @ParentID
Line 4: Incorrect syntax near '@ReturnValue'.
EXEC [dbo].[GetTestTable] @ParentID, @ReturnValue RETURN
Procedure or Function GetTestTable has too many arguments specified.
No comments:
Post a Comment