SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic update statement


Dynamic update statement

Author
Message
Pooja_prasad
Pooja_prasad
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 25
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
Vic.K
Vic.K
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 478
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
Pooja_prasad
Pooja_prasad
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 25
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
Vic.K
Vic.K
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 478
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.
Pooja_prasad
Pooja_prasad
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 25
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
Vic.K
Vic.K
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 478
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search