4

I have a table with 3 columns: username, password and permission. I am trying to write a stored procedure to accept @username as an input parameter and then output a parameter @permission. How do I do this?

0

3 Answers 3

9

More might be needed, but according to your question, this is the code:

CREATE PROCEDURE [dbo].[GetPermission]
    @userName varchar(50),
    @permission int output
AS
BEGIN

    select @permission = PERMISSION from USERS where UserName = @userName

END;

EDIT:

Another option is to create a function, example:

CREATE FUNCTION [dbo].[GetPermission](@userName [varchar(50)])
RETURNS [int] 
AS 
BEGIN

    declare @permission int

    select @permission = PERMISSION from USERS where UserName = @userName

    return @permission

END;
Sign up to request clarification or add additional context in comments.

4 Comments

would it be better to create a stored function with a returnvalue? Does it make much difference to return a resultSet instead?
btw. if you (pikk) want to compare the password I would advice you to store them hashed... link
Hm, i am trying to run in Visual Studio 2010 using C# and the Debugger says "Procedure or function 'sp_getPermission' expects parameter '@permission', which was not supplied.". But it is the output, what do I have to supply?
@pikk: you have to declare the parameter anyway, but, after the SP executes, the value will change.
0

Just to add to Gustavo F point, the ParameterDirection of the output parameter should be set to ParameterDirection.Output.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.direction%28v=vs.110%29.aspx

Comments

0
CREATE PROC SP_ORDERS AS BEGIN SELECT DISTINCT E.EmployeeID,E.FirstName+SPACE(3)+E.LastName AS CUTNAME,E.City,ET.TerritoryDescription,P.ProductName, OD.Discount,SUM(OD.Quantity*OD.UnitPrice)AS TOTAL FROM [DimOrder Details] OD  JOIN DimOrders O ON OD.OrderID=O.OrderID JOIN DimProducts P ON OD.ProductID=P.ProductID JOIN DimEmployees E ON O.EmployeeID=E.EmployeeID JOIN DimCustomers C ON O.CustomerID=C.CustomerID JOIN DimEmployeeTerritories ET ON E.EmployeeID=ET.EmployeeID GROUP BY E.EmployeeID,E.FirstName,E.LastName,E.City,ET.TerritoryDescription,P.ProductName,OD.Discount END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.