SQL 存储过程不返回字符串值
SQL stored procedure not returning string value
我有以下存储过程代码。我只想 return 如果消息已经存在于数据库中,它应该 return 重复消息,否则插入数据并显示消息完成
SQL 存储过程
ALTER PROCEDURE [dbo].[SP_AddProjectManager]
-- Add the parameters for the stored procedure here
@ProjectId int,
@EmployeeId int,
@uid nvarchar(128)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Organization_Id int = (select Organization_Id from Employees where User_Account_Id = @uid);
declare @Office_Id int = (select Office_Id from Employees where User_Account_Id = @uid);
declare @output nvarchar(50);
declare @dup int =(select count(*) from Project_Managers where EmployeeId=@EmployeeId and ProjectId=@ProjectId);
if(@dup=0)
begin
INSERT INTO [dbo].[Project_Managers]
([EmployeeId]
,[ProjectId]
,[User_Account_Id]
,[Office_Id]
,[Organization_Id]
,[CreatedOn]
,[UpdatedOn]
,[CreatedBy]
,[UpdatedBy]
,[Is_active]
)
VALUES(@EmployeeId,@ProjectId,@uid,@Office_Id,@Organization_Id,GETDATE(),GETDATE(),@uid,@uid,1);
SET @output= 'Done';
end
else
begin
SET @output ='Repeated';
end
return convert(varchar(10),@output)
END
不能return它显示以下错误的字符串值:
Msg 245, Level 16, State 1, Procedure SP_AddProjectManager, Line 43 [Batch Start Line 2]
Conversion failed when converting the varchar value 'Repeated' to data type int.
C#代码
public static string Add_ProjectManager(ProjectManagers PM)
{
using (SqlCommand cmd_insertion = new SqlCommand())
{
conn c = new conn();
SqlConnection _Con = c.conect();
cmd_insertion.Connection = _Con;
_Con.Open();
cmd_insertion.CommandType = System.Data.CommandType.StoredProcedure;
cmd_insertion.CommandText = "SP_AddProjectManager";
cmd_insertion.Parameters.AddWithValue("@ProjectId", PM.ProjectId);
cmd_insertion.Parameters.AddWithValue("@EmployeeId", PM.EmployeeId);
cmd_insertion.Parameters.AddWithValue("@uid", PM.userId);
SqlParameter retval = cmd_insertion.Parameters.Add("@output", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;
cmd_insertion.ExecuteNonQuery();
string status = (string)cmd_insertion.Parameters["@output"].Value;
_Con.Close();
cmd_insertion.Parameters.Clear();
return status;
}
}
使用 OUTPUT 参数,像这样...(免责声明,不在 sql 编辑器前)
ALTER PROCEDURE [dbo].[SP_AddProjectManager]
-- Add the parameters for the stored procedure here
@ProjectId int,
@EmployeeId int,
@uid nvarchar(128),
@output nvarchar(50) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Organization_Id int = (select Organization_Id from Employees where User_Account_Id = @uid);
declare @Office_Id int = (select Office_Id from Employees where User_Account_Id = @uid);
declare @output nvarchar(50);
declare @dup int =(select count(*) from Project_Managers where EmployeeId=@EmployeeId and ProjectId=@ProjectId);
if(@dup=0)
begin
INSERT INTO [dbo].[Project_Managers]
([EmployeeId]
,[ProjectId]
,[User_Account_Id]
,[Office_Id]
,[Organization_Id]
,[CreatedOn]
,[UpdatedOn]
,[CreatedBy]
,[UpdatedBy]
,[Is_active]
)
VALUES(@EmployeeId,@ProjectId,@uid,@Office_Id,@Organization_Id,GETDATE(),GETDATE(),@uid,@uid,1);
SET @output= 'Done';
end
else
begin
SET @output ='Repeated';
end
END
你会这样称呼它...
DECLARE @output varchar(50);
EXEC [dbo].[SP_AddProjectManager] 6, 66, '81873272', @output OUTPUT
SELECT @output
-- OR
PRINT @Output
或者,如果您不需要输出参数,请将您的“RETURN”语句替换为“SELECT”
你的 C# 代码,又一次,没有使用编辑器,所以我没想到...
public static string Add_ProjectManager(ProjectManagers PM)
{
using (SqlCommand cmd_insertion = new SqlCommand())
{
using (conn c = new conn()) // Whatever this is
{
using (SqlConnection _Con = c.conect())
{
cmd_insertion.Connection = _Con;
cmd_insertion.CommandType = System.Data.CommandType.StoredProcedure;
cmd_insertion.CommandText = "SP_AddProjectManager";
cmd_insertion.Parameters.AddWithValue("@ProjectId", PM.ProjectId); // .AddWithValue() is bad
cmd_insertion.Parameters.AddWithValue("@EmployeeId", PM.EmployeeId);
cmd_insertion.Parameters.AddWithValue("@uid", PM.userId);
SqlParameter retval = cmd_insertion.Parameters.Add("@output", SqlDbType.VarChar);
retval.Direction = ParameterDirection.Output;
_Con.Open();
cmd_insertion.ExecuteNonQuery();
string status = retval.Value;
_Con.Close();
return status;
}
}
}
}
我有以下存储过程代码。我只想 return 如果消息已经存在于数据库中,它应该 return 重复消息,否则插入数据并显示消息完成
SQL 存储过程
ALTER PROCEDURE [dbo].[SP_AddProjectManager]
-- Add the parameters for the stored procedure here
@ProjectId int,
@EmployeeId int,
@uid nvarchar(128)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Organization_Id int = (select Organization_Id from Employees where User_Account_Id = @uid);
declare @Office_Id int = (select Office_Id from Employees where User_Account_Id = @uid);
declare @output nvarchar(50);
declare @dup int =(select count(*) from Project_Managers where EmployeeId=@EmployeeId and ProjectId=@ProjectId);
if(@dup=0)
begin
INSERT INTO [dbo].[Project_Managers]
([EmployeeId]
,[ProjectId]
,[User_Account_Id]
,[Office_Id]
,[Organization_Id]
,[CreatedOn]
,[UpdatedOn]
,[CreatedBy]
,[UpdatedBy]
,[Is_active]
)
VALUES(@EmployeeId,@ProjectId,@uid,@Office_Id,@Organization_Id,GETDATE(),GETDATE(),@uid,@uid,1);
SET @output= 'Done';
end
else
begin
SET @output ='Repeated';
end
return convert(varchar(10),@output)
END
不能return它显示以下错误的字符串值:
Msg 245, Level 16, State 1, Procedure SP_AddProjectManager, Line 43 [Batch Start Line 2]
Conversion failed when converting the varchar value 'Repeated' to data type int.
C#代码
public static string Add_ProjectManager(ProjectManagers PM)
{
using (SqlCommand cmd_insertion = new SqlCommand())
{
conn c = new conn();
SqlConnection _Con = c.conect();
cmd_insertion.Connection = _Con;
_Con.Open();
cmd_insertion.CommandType = System.Data.CommandType.StoredProcedure;
cmd_insertion.CommandText = "SP_AddProjectManager";
cmd_insertion.Parameters.AddWithValue("@ProjectId", PM.ProjectId);
cmd_insertion.Parameters.AddWithValue("@EmployeeId", PM.EmployeeId);
cmd_insertion.Parameters.AddWithValue("@uid", PM.userId);
SqlParameter retval = cmd_insertion.Parameters.Add("@output", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;
cmd_insertion.ExecuteNonQuery();
string status = (string)cmd_insertion.Parameters["@output"].Value;
_Con.Close();
cmd_insertion.Parameters.Clear();
return status;
}
}
使用 OUTPUT 参数,像这样...(免责声明,不在 sql 编辑器前)
ALTER PROCEDURE [dbo].[SP_AddProjectManager]
-- Add the parameters for the stored procedure here
@ProjectId int,
@EmployeeId int,
@uid nvarchar(128),
@output nvarchar(50) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Organization_Id int = (select Organization_Id from Employees where User_Account_Id = @uid);
declare @Office_Id int = (select Office_Id from Employees where User_Account_Id = @uid);
declare @output nvarchar(50);
declare @dup int =(select count(*) from Project_Managers where EmployeeId=@EmployeeId and ProjectId=@ProjectId);
if(@dup=0)
begin
INSERT INTO [dbo].[Project_Managers]
([EmployeeId]
,[ProjectId]
,[User_Account_Id]
,[Office_Id]
,[Organization_Id]
,[CreatedOn]
,[UpdatedOn]
,[CreatedBy]
,[UpdatedBy]
,[Is_active]
)
VALUES(@EmployeeId,@ProjectId,@uid,@Office_Id,@Organization_Id,GETDATE(),GETDATE(),@uid,@uid,1);
SET @output= 'Done';
end
else
begin
SET @output ='Repeated';
end
END
你会这样称呼它...
DECLARE @output varchar(50);
EXEC [dbo].[SP_AddProjectManager] 6, 66, '81873272', @output OUTPUT
SELECT @output
-- OR
PRINT @Output
或者,如果您不需要输出参数,请将您的“RETURN”语句替换为“SELECT”
你的 C# 代码,又一次,没有使用编辑器,所以我没想到...
public static string Add_ProjectManager(ProjectManagers PM)
{
using (SqlCommand cmd_insertion = new SqlCommand())
{
using (conn c = new conn()) // Whatever this is
{
using (SqlConnection _Con = c.conect())
{
cmd_insertion.Connection = _Con;
cmd_insertion.CommandType = System.Data.CommandType.StoredProcedure;
cmd_insertion.CommandText = "SP_AddProjectManager";
cmd_insertion.Parameters.AddWithValue("@ProjectId", PM.ProjectId); // .AddWithValue() is bad
cmd_insertion.Parameters.AddWithValue("@EmployeeId", PM.EmployeeId);
cmd_insertion.Parameters.AddWithValue("@uid", PM.userId);
SqlParameter retval = cmd_insertion.Parameters.Add("@output", SqlDbType.VarChar);
retval.Direction = ParameterDirection.Output;
_Con.Open();
cmd_insertion.ExecuteNonQuery();
string status = retval.Value;
_Con.Close();
return status;
}
}
}
}