Searching a Table

  • I have a table containing a number of columns, one column contains the record numbers for the records being inserted. The record numbers have the format: 2005/a/b/001, 2005/a/b/002 ..... and so on.

    There is a possibility that some records may be added in duplicate but throwing an error. What I want to do is search the numbers column before the new record is inserted to see if that record already exists and if it does then I want ot generate new number.

    I know how to generate new number but I need help to search the column.

    Anyone know how to search the column to see if the record already exists?

    Thanks.

  • Can you post the tables definition with some sample data, along with the expected output. Also the code to generate the newid would be nice. It'll be easier to help you then.

  • 2005/a/b/001

    Life would be easier when you would break this into separate columns. Is there a specific reason why you want to maintain such a sequence at all within your data?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • "2005/a/b/001" Doh!!, read this one too fast.

    I would tend to agree with Frank on this one and ask the same thing??

  • I would assume '2005/a/b/001' is record number and is in one column.

    Next recorde would be some thing like '2005/a/b/002'  the '/' in the data is confusing.

    IF EXISTS (SELECT * FROM Table WHERE Column = '2005/a/b/001' )

     -- Logic to generate a new Code

    ELSE

     -- Do an update (UPDATE Table set Field = newvale Where Column = '2005/a/b/001' )

     -- or Return

    Regards,
    gova

Viewing 5 posts - 1 through 4 (of 4 total)

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