Tsql SQL Server 2000错误处理

Tsql SQL Server 2000错误处理,tsql,sql-server-2000,Tsql,Sql Server 2000,我在SQL中有一个自定义函数,如果它报告错误而不中断或停止使用该函数的过程,我希望返回NULL值 我该怎么做 我知道错误处理应该在函数内部完成,但是我应该在函数中写什么呢 例如: 过程名为PROC ,函数名为FUNC 代码: 您可以尝试检查要传递给函数的值是否为数字: drop procedure sp_test go drop function fn_test go drop table t_test go create table t

我在SQL中有一个自定义函数,如果它报告错误而不中断或停止使用该函数的过程,我希望返回NULL值

我该怎么做

我知道错误处理应该在函数内部完成,但是我应该在函数中写什么呢

例如: 过程名为PROC ,函数名为FUNC

代码:


您可以尝试检查要传递给函数的值是否为数字:

    drop procedure sp_test
    go
    drop function fn_test
    go
    drop table t_test
    go

    create table t_test (id int, name varchar(255))
    go
    insert into t_test values(1,'coco')
    insert into t_test values(2,'cucu')
    insert into t_test values(2,'10')
    insert into t_test values(2,'10.5')
    go

    create function fn_test(@p decimal(18,6)) returns int
    as
    begin

            return @p * 10

    end
    go

    create procedure sp_test
    as
    begin

            select id, name, 
                /* when is a number you call the function */
                case when isnumeric(name)<>0 then dbo.fn_test(name) 
                /* if isn't a number you return NULL */
                else null end as call_to_fn_test 
            from t_test

    end
    go

    sp_test

SQL Server 2000没有TRY/CATCH,因此无法处理错误,只能防止错误。另见。你能发布你的自定义项代码吗?你认为会出现什么样的错误?a缺少表/列b算术溢出、被零除等c硬件错误、磁盘已满等d违反函数e中定义的表变量中的约束其他类型的错误?预期的错误是当我在varchar类型上使用函数,而其设计用于浮点或int时,所以这个过程创建了一个表,其中一列使用了这个函数,但是当它传递一个包含字母的值时,整个过程就停止了
    drop procedure sp_test
    go
    drop function fn_test
    go
    drop table t_test
    go

    create table t_test (id int, name varchar(255))
    go
    insert into t_test values(1,'coco')
    insert into t_test values(2,'cucu')
    insert into t_test values(2,'10')
    insert into t_test values(2,'10.5')
    go

    create function fn_test(@p decimal(18,6)) returns int
    as
    begin

            return @p * 10

    end
    go

    create procedure sp_test
    as
    begin

            select id, name, 
                /* when is a number you call the function */
                case when isnumeric(name)<>0 then dbo.fn_test(name) 
                /* if isn't a number you return NULL */
                else null end as call_to_fn_test 
            from t_test

    end
    go

    sp_test