Testing multiple columns in a table

  • Afternoon all,

    I need to be able to test columns in a table to see if they contain the correct data, if not send an email.

    A bit of background, I populate a table with order numbers, pallet counts, addresses, etc. This then gets output to a text file and ftp'd to a third party.

    The issue I'm trying to resolve is that some of the columns contain manditory data, in set formats, if one is incorrect, say, a pallet count of zero, the third party return the file, I want to check prior to sending, thus saving time and effort.

    I could write a bunch of IF statements, one for each field or as I am currently trying, having two queries producing CHECKSUMs one just including all my manditory columns and one surrounding the manditory columns with NULLIF, which if a column is wrong will produce a different CHECKSUM

    Is there a better way?

    Ta

    David

    ============================================================
    David

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • Hi David welcome to the forums. Please read the article in my signature about posting questions to the forum this will help us in solving your question. Without knowledge of your table structure or the columns that we are comparing or the valid vs. invalid data you could do something like this:

    select

    TableID

    ,case when column data is invalid then 1 else 0 end as Col1Wrong

    ,... as Col2Wrong

    ,... etc

    from table

    where

    col1 data is invalid

    or

    col2 data is invalid

    or

    ... etc



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Hi Keith,

    I'll get on and read the artical.. 🙂

    In the meantime I have been working with this as an example. The first row is correct, second has no order number, third has no pallet count and the fourth has no weight. If i run the SELECT at the bottom, the CHECKSUMs are different, I could use this as the criteria for sending an email.

    CREATE TABLE OrdersTest(OrderNo CHAR(10),

    PalletCount NUMERIC,

    WeightKG NUMERIC)

    INSERT INTO OrdersTest VALUES ('abc1234567', 10, 1000)

    INSERT INTO OrdersTest VALUES (' ', 10, 1000)

    INSERT INTO OrdersTest VALUES ('def1234567', 0, 1000)

    INSERT INTO OrdersTest VALUES ('ghi1234567', 10, 0)

    SELECT'Checksum' = CHECKSUM(OrderNo,PalletCount, WeightKG),

    'TestedChecksum' = CHECKSUM(NULLIF(OrderNo,' '),NULLIF(PalletCount, 0), NULLIF(WeightKG, 0))

    FROM OrdersTest

    Is there a better way of doing this?

    ============================================================
    David

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • first stab at the issue:

    SELECT OrdersTest.*,

    CASE

    WHEN RTRIM(ISNULL(OrderNo,'')) = ''

    THEN 'Blank Order Number'

    ELSE

    ''

    END AS OrderNumberIncorrect,

    CASE

    WHEN ISNULL(PalletCount,0) = 0

    THEN 'Pallet Count Incorrect'

    ELSE ''

    END AS PalletCountIncorrect,

    CASE

    WHEN ISNULL(WeightKG,0) = 0

    THEN 'WeightKG Incorrect'

    ELSE

    ''

    END AS WeightKGIncorrect

    FROM OrdersTest WHERE RTRIM(ISNULL(OrderNo,'')) = ''

    OR ISNULL(PalletCount,0) = 0

    OR ISNULL(WeightKG,0) = 0

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I agree with Lowell's approach



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • performance wise, this would be a little better, since the OR statement forces a table scan, and this would/could take advantage of existing indexes instead.

    but it's effectively the same approach.

    SELECT

    OrdersTest.*,

    CASE

    WHEN RTRIM(ISNULL(OrderNo, '')) = ''

    THEN

    'Blank Order Number'

    ELSE

    ''

    END AS Problem

    FROM OrdersTest

    WHERE RTRIM(ISNULL(OrderNo, '')) = ''

    UNION ALL

    SELECT

    OrdersTest.*,

    CASE

    WHEN ISNULL(PalletCount, 0) = 0

    THEN

    'Pallet Count Incorrect'

    ELSE

    ''

    END AS Problem

    FROM OrdersTest

    WHERE ISNULL(PalletCount, 0) = 0

    UNION ALL

    SELECT

    OrdersTest.*,

    CASE

    WHEN ISNULL(WeightKG, 0) = 0

    THEN

    'WeightKG Incorrect'

    ELSE

    ''

    END AS Problem

    FROM OrdersTest

    WHERE ISNULL(WeightKG, 0) = 0

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell and Keith,

    Thanks for your input, I have modified my proc using Lowell's last script and it performs well.

    Thanks for your time

    Ta

    David

    ============================================================
    David

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

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

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