如何编写if else语句来插入查询以显示存储过程中的错误

时间:2010-07-05 05:41:12

标签: sql-server-2005

如何编写if else语句来插入查询以显示存储过程中的错误

下面是我的存储过程。我想显示一个错误消息,当已经插入student_id再次插入..Student_id是主键所以它在我的代码中显示错误,但我不知道如何得到该错误并显示....怎么做朋友PLZ帮帮我......

ALTER PROCEDURE [dbo].[spinsertstudentapplication] 
    -- Add the parameters for the stored procedure here
    @Student_id             nvarchar(50),   
    @Select_Country         nvarchar(50),
    @Select_State           nvarchar(50),
    @Select_Franchise       nvarchar(50),
    @Select_Sensei          nvarchar(50),
    @Enter_Student_Name     nvarchar(50),
    @Enter_Student_Address  nvarchar(50),
    @Students_Father_Name   nvarchar(50),
    @Student_DOB            datetime,
    @Gender                 bit,
    @Group                  nvarchar(50),
    @Enter_Kyu              nvarchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here 
    insert into StudentApplication(Student_id,Select_Country,Select_State,Select_Franchise,Select_Sensei,Enter_Student_Name,Enter_Student_Address,Students_Father_Name,Student_DOB,Gender,[Group],Enter_Kyu)values(@Student_id,@Select_Country,@Select_State,@Select_Franchise,@Select_Sensei,@Enter_Student_Name,@Enter_Student_Address,@Students_Father_Name,@Student_DOB,@Gender,@Group,@Enter_Kyu)

END

3 个答案:

答案 0 :(得分:0)

您可以在SP顶部添加输出参数:

@ErrorOutput INT OUTPUT

然后添加:

IF EXISTS (SELECT 1 FROM StudentApplication WHERE Student_id=@Student_id)
    SET @ErrorOutput = -1;
    RETURN @ErrorOutput;
ELSE
    -- Insert statement

答案 1 :(得分:0)

正如Dean'codeka'Harding在评论中提到的那样,了解你如何调用存储过程会很有帮助。但这里有一些一般性的建议。

首先,存储过程的一般约定是成功时返回0,错误时返回非零值(可以使用输出参数表示错误代码,但这有点多余)。

其次,在尝试插入值之前,应检查它是否已存在。例如。这是一些伪代码:

if exists (select 1 from StudentApplication where Student_ID = @Student_ID)
begin
  raiserror('Student ID already exists.', 16, 1)
  return 1 -- Your caller would need to know that 1 identifies existing record
end

请注意,在此示例中,调用raiserror后退出T-SQL代码,因此如果从C#/ VB.NET / etc调用存储过程,则需要将其作为异常处理。或者,您可以省略raiserror调用,只是让存储过程返回预期的(由客户端)错误代码。

然后仍然有一个小的可能性,即插入一个副本,但我认为它会产生一个致命的错误,你需要在客户端代码中处理(错误处理依赖于客户端;在C#中,你可能会得到一个SqlException,您可以查询特定的代码。)

另一种选择是在检查现有记录的代码周围放置一个transaction,然后插入一个新记录。

如果你想在C#中处理错误,你需要处理两件事。首先,检查返回代码并相应地处理非零值(C#客户端和存储过程必须就每个错误代码的含义达成一致)。然后,您还需要处理SqlExceptions。 SqlException对象的StateNumber属性可以帮助您识别问题。请记住,对于即时定义的错误消息(如我的示例所示),Number将总是返回50,000(我认为)。

答案 2 :(得分:0)

您可以在Sp中添加输出参数(int / bit),并将值设置为该参数是否存在记录。然后,您可以检查前端的输出参数值。下面我添加了一个输出参数@RecordExist作为bit并在记录已经存在时设置值1,否则设置为0.在前端,您可以在执行后从SqlCommand获取参数值SP。 (SqlCommand.Parameters["@RecordExist"].Value

ALTER PROCEDURE [dbo].[spinsertstudentapplication]   
    -- Add the parameters for the stored procedure here  
    @Student_id             nvarchar(50),     
    @Select_Country         nvarchar(50),  
    @Select_State           nvarchar(50),  
    @Select_Franchise       nvarchar(50),  
    @Select_Sensei          nvarchar(50),  
    @Enter_Student_Name     nvarchar(50),  
    @Enter_Student_Address  nvarchar(50),  
    @Students_Father_Name   nvarchar(50),  
    @Student_DOB            datetime,  
    @Gender                 bit,  
    @Group                  nvarchar(50),  
    @Enter_Kyu              nvarchar(50),
    @RecordExist            bit output  -- newly added parameter
 AS  
 BEGIN  
     -- SET NOCOUNT ON added to prevent extra result sets from
     -- interfering with SELECT statements.
     SET NOCOUNT ON;

     -- Insert statements for procedure here 

If Exists (Select * from StudentApplication where Student_id = @Student_id)
   Begin
      Select @RecordExist = 1
      return
   End

Else
  Begin
      insert into StudentApplication (Student_id, Select_Country, Select_State,  Select_Franchise, Select_Sensei, Enter_Student_Name, Enter_Student_Address, Students_Father_Name, Student_DOB, Gender, [Group], Enter_Kyu)

      Select @Student_id, @Select_Country, @Select_State, @Select_Franchise, @Select_Sensei, @Enter_Student_Name, @Enter_Student_Address, @Students_Father_Name, @Student_DOB, @Gender, @Group, @Enter_Kyu

      Select @RecordExist = 0
      return
  End

END