sql存储过程不返回字符串值

oprakyz7  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(386)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

10个月前关门了。
改进这个问题
我有下面的存储过程代码。我只想返回消息,如果已经在数据库中,它应该返回消息,它是重复的,否则插入数据,并显示一个消息完成
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

无法返回字符串值,它显示以下错误:

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;
            }
        }
ryoqjall

ryoqjall1#

使用输出参数,如下…(免责声明,不要在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;
            }
        }
    }
}

相关问题