存储过程包含输入和输出参数

时间:2012-01-17 00:03:19

标签: sql sql-server stored-procedures

我有一个包含3列的表:usernamepasswordpermission。我正在尝试编写一个存储过程来接受@username作为输入参数,然后输出参数@permission。我该怎么做?

3 个答案:

答案 0 :(得分:9)

可能需要更多,但根据您的问题,这是代码:

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

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

END;

编辑:

另一种选择是创建一个函数,例如:

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;

答案 1 :(得分:0)

只需添加到Gustavo F点,输出参数的ParameterDirection应设置为ParameterDirection.Output。

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

答案 2 :(得分: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