Column Check using T SQL

  • I would like to run a T SQL script that looks at the data within a table and specific column and generate an Error message if there is a Null Value present...

    Can this be done?

  • This should be what you are looking for

    If Exists(

    Select top 1 * From Tab where Col is Null

    )

    Begin

    Raiserror( msg.......)

    End

  • This should be what you are looking for

    If Exists(

    ---Check if such a row exists

    Select top 1 * From Tab where Col is Null

    )

    Begin

    --use the Raiserror function

    Raiserror( msg.......)

    End

  • Check if that will help you.

    use tempdb

    go

    create table #t1 (

    col1 int,

    col2 int

    )

    insert into #t1 values (1, 1)

    insert into #t1 values (2, null)

    if exists (select * from #t1 where col2 is null)

    raiserror('There is null value in col2',16,1)

    drop table #t1

  • Thanks for your help. I seem to have something basic working at the moment that I can now build on. I am a complete newbie to this stuff. C an I just ask you in your raiseerror you put the error message followed by ,16,1)

    what does the 16 and 1 do?

  • 16 means a severity level.

    1 means a state.

    that's from BOL:

    If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.

  • Thank you for you help...

    I seem to have got that working well now

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply