简体   繁体   中英

stored procedure returns varchar c# sql server

I need input one variable and output another

CREATE PROC CheckLogPas2
@log varchar(Max)
as
BEGIN
SET NOCOUNT ON;
DECLARE @k varchar(50);

set  @k = (SELECT position From sotrud_users where login = @log);

RETURN @k;
END

in this procedure i want take in variable "k" "position". "position" is varchar. When i try use it procedure give error. I can't output variable "k" because RETURN can't output not int variable

        SqlCommand sqlcmd = new SqlCommand("[CheckLogPas]", conn);
        sqlcmd.CommandType = CommandType.StoredProcedure;

        sqlcmd.Parameters.AddWithValue("@log", SqlDbType.Int);
        sqlcmd.Parameters["@UserID"].Value = DB_values.check_log;

        SqlParameter param = new SqlParameter();
        param.ParameterName = "@k";
        param.SqlDbType = SqlDbType.NVarChar;
        param.Direction = ParameterDirection.ReturnValue;

        sqlcmd.ExecuteNonQuery();
        comm.Parameters.Add(param);

        conn.Open();
        comm.ExecuteNonQuery();

        pos = param.Value;

and this my code in c#. i need to get a variable in another variable string on c#

I believe a Scalar-valued Function would solve your problem, instead of a Stored Procedure. Using this you would be able to create a function in SQL Server that accepts parameters, and can return a value. If you wish to return more than one value from your function, you would need to use a Table-valued Function instead.

Here is the structure for a Scalar-valued function:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION <Scalar_Function_Name, sysname, FunctionName> 
(
    -- Add the parameters for the function here
    <@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
)
RETURNS <Function_Data_Type, ,int>
AS
BEGIN
    -- Declare the return variable here
    DECLARE <@ResultVar, sysname, @Result> <Function_Data_Type, ,int>

    -- Add the T-SQL statements to compute the return value here
    SELECT <@ResultVar, sysname, @Result> = <@Param1, sysname, @p1>

    -- Return the result of the function
    RETURN <@ResultVar, sysname, @Result>

END
GO

And here is how a Table-valued Function would be structured:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION <Inline_Function_Name, sysname, FunctionName> 
(   
    -- Add the parameters for the function here
    <@param1, sysname, @p1> <Data_Type_For_Param1, , int>, 
    <@param2, sysname, @p2> <Data_Type_For_Param2, , char>
)
RETURNS TABLE 
AS
RETURN 
(
    -- Add the SELECT statement with parameter references here
    SELECT 0
)
GO

Your current stored procedure:

CREATE PROC CheckLogPas2
@log varchar(Max)
as
BEGIN
SET NOCOUNT ON;
DECLARE @k varchar(50);

set  @k = (SELECT position From sotrud_users where login = @log);

RETURN @k;
END

Could be re-written as a Scalar-valued function like so:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION CheckLogPas2 
(
    -- Add the parameters for the function here
    @log    nvarchar(max)
)
RETURNS nvarchar(max)
AS
BEGIN
    
    RETURN (SELECT position From sotrud_users where login = @log)

END
GO

Using that, you could instead have your c# code execute the following query:

SELECT [dbo].[CheckLogPas2](@log);

Like so:

// using a command to retrieve
using (SqlCommand cmd = new SqlCommand("SELECT [dbo].[CheckLogPas2](@log);", conn))
{
     // format command
     cmd.Parameters.Add("@log", SqlDbType.NVarChar).Value = yourLogVariable;

     // get a sqlreader with the results from our query
     using (SqlDataReader reader = cmd.ExecuteReader())
     {
                            
          if(reader.Read())
          {
              string desiredStringResult = reader.GetString(0);
          }

     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM