将SQL Server函数的异常抛出到存储过程

时间:2013-04-05 14:39:40

标签: sql sql-server exception-handling

我在SQL Server 2012中有存储过程说spXample和一个定标器值函数说fXample。 我从spXample调用函数fXample。 我可以在函数中抛出异常并在存储过程的Catch块中捕获它并重新抛出调用的C#代码吗?

更新

我写的功能如下:

CREATE FUNCTION dbo.fXample(@i INT)
RETURNS TINYINT
AS
BEGIN
  RETURN (SELECT CASE WHEN @i < 10 
    THEN THROW 51000,'Xample Exception',1;
    ELSE (SELECT @i) 
    END);
END
GO

我收到错误

  

Msg 443,Level 16,State 14,Procedure fXample,Line 46无效的使用   一个副作用操作员'THROW'在一个函数内。

如何编写替代代码以实现上述功能?

3 个答案:

答案 0 :(得分:9)

您可以通过在验证失败时强制出现错误条件来执行此操作,前提是这不是可能自然发生的错误。当您知道某个错误只能在验证失败时发生时,您可以通过检查catch块中的error_number以自定义方式处理该错误。 tempdb中的示例:

USE tempdb;
GO

CREATE FUNCTION dbo.fXample(@i INT)
RETURNS TINYINT
AS
BEGIN
  RETURN (SELECT CASE WHEN @i < 10 -- change this to your "validation failed" condition
    THEN 1/0         -- something that will generate an error
    ELSE (SELECT @i) -- (you'd have your actual retrieval code here)
    END);
END
GO

CREATE PROCEDURE dbo.spXample
  @i INT
AS
BEGIN
  SET NOCOUNT ON;
  BEGIN TRY
    SELECT dbo.fXample(@i);
  END TRY
  BEGIN CATCH
    IF ERROR_NUMBER() = 8134 -- divide by zero
    BEGIN
      THROW 50001, 'Your custom error message.', 1;
      -- you can throw any number > 50000 here
    END
    ELSE -- something else went wrong
    BEGIN
      THROW; -- throw original error
    END
  END CATCH
END
GO

现在尝试一下:

EXEC dbo.spXample @i = 10;  -- works fine
EXEC dbo.spXample @i = 6;   -- fails validation
EXEC dbo.spXample @i = 256; -- passes validation but overflows return

结果:

----
10
  

Msg 50001,Level 16,State 1,Procedure spXample,Line 12
  您的自定义错误消息。

     

Msg 220,Level 16,State 2,Procedure spXample,Line 7
  数据类型tinyint的算术溢出错误,值= 256。

答案 1 :(得分:2)

尽管这已解决,但我仍然应该分享。我认为这将是一个更简单的解决方案:

public class loops{
    public int a,b,c,d,e,f,g,h,i,j;
    public loops(){}

public void biggest(int a){
        String as = Integer.toString(a);
        int index=0;
        a = 0;
        b = 0;
        c = 0;
        d = 0;
        e = 0;
        f = 0;
        g = 0;
        h = 0;
        i = 0;
        j = 0;
        int asl = as.length();
        while(index<asl){
            String num3 = as.substring(index,(index+1));
            int con3 = Integer.parseInt(num3);
            if(con3==9){a++;}
            if(con3==8){b++;}
            if(con3==7){c++;}
            if(con3==6){d++;}
            if(con3==5){e++;}
            if(con3==4){f++;}
            if(con3==3){g++;}
            if(con3==2){h++;}
            if(con3==1){i++;}
            if(con3==0){j++;}
               index++;
            }

         for(int z=0;z<a;z++){
          System.out.print("9");
          }
          for(int y=0;y<b;y++){
          System.out.print("8");
          }
          for(int x=0;x<c;x++){
          System.out.print("7");
          }
          for(int w=0;w<d;w++){
          System.out.print("6");
          }
          for(int v=0;v<e;v++){
          System.out.print("5");
          }
          for(int u=0;u<f;u++){
          System.out.print("4");
          }
          for(int t=0;t<g;t++){
          System.out.print("3");
          }
          for(int s=0;s<h;s++){
          System.out.print("2");
          }
          for(int r=0;r<i;r++){
          System.out.print("1");
          }
          for(int q=0;q<j;q++){
          System.out.print("0");
          }
public static void main(String []args){
        loops x = new loops();
        x.biggest(45683408); 
     }
   }

答案 2 :(得分:1)

/*  *** EXAMPLES ***
    SELECT dbo.fnDoSomething(500) -- RETURNS TRUE
    
    SELECT dbo.fnDoSomething(5000) -- THROWS ERROR
        Msg 245, Level 16, State 1, Line 4
        Conversion failed when converting the varchar value 
        'Function Error - [dbo].[fnDoSomething] - Value is greater than 1000' to data type bit.
*/
CREATE FUNCTION dbo.fnDoSomething
(
    @SomeValue      INT
)
RETURNS BIT
AS
BEGIN
    IF @SomeValue > 1000
    BEGIN
        DECLARE @FunctionName AS SYSNAME = QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)) + '.' + QUOTENAME(OBJECT_NAME(@@PROCID))
        DECLARE @Error AS VARCHAR(200) = 'Function Error - '+ @FunctionName + ' - Value is greater than 1000'
        RETURN  CONVERT(BIT,@Error)
    END

    RETURN 1
END
GO