如题
例:
Create Table A(str varchar(50))insert  into A(str)
select '30'
insert  into A(str)
select '-' -- 减号
insert  into A(str)
select '20'
insert  into A(str)
select '+'
insert  into A(str)
select '+'我想在别处引用表A中的内容,但这时表A中的内容组合后的表达式为 30-20++,
有没有函数或方法先判断此表达式是否正确,待正确后再引用

解决方案 »

  1.   


    if OBJECT_ID('usp_parse_exp') is not null
     drop procedure usp_parse_exp;
    go
    create procedure usp_parse_exp @exp varchar(100)
    as
    declare @ret int;
    set @ret=0;begin try
     exec('set parseonly on select '+@exp);
    end try
    begin catch
     set @ret=error_number();
    end catchreturn @ret;
    go-- 表达式正确 @err=0,否则为非零值
    declare @err int;
    exec @err=usp_parse_exp '30-20++';
    print @err;
      

  2.   


    -- SQL Server 2000
    -- 可惜不能消除 SQL Server 返回的错误信息
    if OBJECT_ID('usp_parse_exp') is not null
     drop procedure usp_parse_exp;
    go
    create procedure usp_parse_exp @exp varchar(100)
    as
    exec('set parseonly on select '+@exp);
    return @@error;
    go-- 表达式正确 @err=0,否则为非零值
    declare @exp varchar(100);
    set @exp='30-20+';declare @err int;
    exec @err=usp_parse_exp @exp;
    if @err=0  exec('select '+@exp);
    else print 'invalid expression';
      

  3.   

    To:#5楼,
    你用的是2005吧。
    好像在2000下没有try这个语法?