Dynamic update statement

  • Hi

    I would like to create a dynamic SQL update/insert statement based on some parsed data in a table in SQL and run this against an existing table in the same database. The issue I'm having is that some fields in the parsed datatable may not exist as fields in the copy-to table which will kill the statement. How can I check before hand?

    i.e. parsed data table:

    fieldname value

    "address" "123 sample rd"

    "Name" "Bob"

    "phone" "12345678"

    Dynamic SQL along the lines of:

    sql += "update tblCopyTo set "

    for each row in parsed datatable

    sql+= fieldname + "=" + value

    end for

    Possible problem:

    tblCopyTo fields:

    address

    customer

    customerphone

    so "phone" from dynamic SQL does not exist in tblCopyTo and the script will fail and not update any other parameters. Since the parsed data is not static, I need to be able to remove the non-existant fields before running the update script.

    Can someone help me with the basic logic?

    thanks

    Pooja

  • You can use system views sys.objects and sys.columns to check, if the columns exist in your tables

    Information on views:

    http://msdn.microsoft.com/en-us/library/ms190324(SQL.90).aspx

    http://msdn.microsoft.com/en-us/library/ms176106(SQL.90).aspx

  • Thanks Vic.K

    I have one more query. If I need to update the columns inspite of having a different name from the parsed values table, I was thinking of storing such mappings in a file and fetching the corresponding column to be updated. Is there any other way I can achieve this?

    For eg: I can have a file which has the column level mapping like

    Destination column Parsed column name

    Address address

    Customer Name

    CustomerPhone Phone

    How can I fetch the column name as Customer whenever i have the parsed value for Name?

    Regards

    Pooja

  • I don't understand your question. If you have a file (or a table in your database) with column mappings, then you have to seek in this table for a corresponding column. If you don't find a column in sys.columns and in your mapping table, then you don't include this column in your update statement.

  • Yes I understood that part. As you said if the column does not exist in sys. columns i can exclude it from teh update. What if i want to include it in the update? how can I do that?

    Eg:The parsed values table has

    FieldName value

    "Name" "Bob"

    The destination table has column "CustomerName". In this case, the value "Bob" should be updated to the column "CustomerName" but the Parsed table FieldName is "Name". How can I use the columnname "CustomerName" in my dynamic query whenever there is a fieldname "Name" in the parsed table? Can I have a file which will have data like

    DestTableColumnName ParsedTableColumnName

    "CustomerName" "Name"

    Can I read from such a file or table and update the corresponding column?

    Pooja

  • 1. You check the system views, whether the column exists on the table, if yes -- OK.

    2. If the column does not exists you check for it in your mapping table, like this:

    set @NewColumn = NULL

    select @NewColumn = ParsedTableColumnName from mapping table where DestTableColumnName = 'your value'

    3. if @NewColumn is not NULL then you have your parsed value, if not you can't do the update on this column.

    You can insert this unparsed column in your mapping table with ParsedTableColumnName = NULL and update these rows manually, setting a value for ParsedTableColumnName.

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

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