Query help needed

  • Hi,

    I want to make a query which can query a table and find out records with a specific value in any column.

    For ex:

    I have a table emp with 20 columns.

    I want to get all records which has -3 in any of the columns and the columnname for that column.

    Thanks

    Pankaj

  • I'm afraid you will need to create the condition expression using OR and list all the relevant columns. On the other hand you do not need to write this condition yourself, you can generate a skeleton by a simple statement

    DECLARE @a VARCHAR(1000)

    SET @a = ''

    SELECT @a = @a + name + '=-3 OR '

    FROM sys.columns

    WHERE object_id = OBJECT_ID('sometable')

    AND system_type_id = 56

    SELECT STUFF(@a, LEN(@a) - 2, 3, '')

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

Viewing 2 posts - 1 through 1 (of 1 total)

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