[英]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".在这个过程中,我想接受变量“k”“位置”。 "position" is varchar.
“位置”是 varchar。 When i try use it procedure give error.
当我尝试使用它时,程序会出错。 I can't output variable "k" because RETURN can't output not int variable
我无法输出变量“k”,因为 RETURN 无法输出非 int 变量
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#.这是我在 c# 中的代码。 i need to get a variable in another variable string on c#
我需要在 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.
使用它,您将能够在 SQL Server 中创建一个接受参数并可以返回值的函数。 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:使用它,您可以让您的 c# 代码执行以下查询:
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.