存储过程输入参数验证

时间:2017-08-04 15:22:00

标签: sql sql-server tsql stored-procedures

我有以下存储过程:

CREATE procedure usp_insert_abm_tctrol_ch   
    (@cod_suc numeric(3), 
     @id_bco numeric(3),
     @cod_banco numeric(3),
     @nro_cta VARCHAR(15),
     @fec_activa SMALLDATETIME,
     @fec_inactiva SMALLDATETIME,
     @nro_ch_desde numeric(9),
     @nro_ch_hasta numeric(9),
     @ult_nro_ch numeric(9),
     @sn_ch_inicio numeric(3),
     @tipo_chequera VARCHAR(2), 
     @sn_estadoi_ch INT,
     @sn_estadof_ch  INT)
as  
begin 
    declare @error varchar(500)

    begin try
    begin tran 
        set @error = ''
        set @error = 'hubo un error'

        if @error <> ''  
        begin    
            RAISERROR(@Error, 16, 10)  
        end   

        insert into tctrol_ch (cod_suc, id_bco, cod_banco, nro_cta, fec_activa, fec_inactiva, nro_ch_desde, nro_ch_hasta, ult_nro_ch, sn_ch_inicio, tipo_chequera, sn_estadoi_ch, sn_estadof_ch) 
            select 
                @cod_suc, @id_bco, @cod_banco, @nro_cta, @fec_activa,
                @fec_inactiva, @nro_ch_desde, @nro_ch_hasta, @ult_nro_ch,
                @sn_ch_inicio, @tipo_chequera, @sn_estadoi_ch, @sn_estadof_ch 
        commit tran 
     end try 
     begin catch
        rollback tran

        declare @Err varchar(max)

        set @Err = ERROR_MESSAGE()
        RAISERROR(@Err , 16, 10)  
     end catch

我想验证@nro_cta(号码帐户)是否属于当前银行(@id_bco)。我怎样才能做到这一点?

我有一张桌子&#34; Bank&#34;列名称为#34; nro_cta&#34;。我想用if语句验证它。我是存储过程的初学者,谢谢!

1 个答案:

答案 0 :(得分:4)

您需要添加类似

的内容
declare @testbank numeric(3)

SET @testbank = (SELECT id_bco from Bank WHERE nro_cta = @nro_cta)

If @testbank <> @id_bco OR @testbank IS NULL
    BEGIN
        RAISERROR('Wrong bank', 16, 10)
    END

我不能完全确定,因为虽然你说Bank有一个字段nro_cta,但是你没有说出银行id被调用的字段,所以为了一致起见我猜它是id_bco。

此外,我假设id_bco和nro_cta与@id_bco和@nro_cta的数据类型相同。