Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Dynamic update statement Expand / Collapse
Author
Message
Posted Tuesday, December 02, 2008 2:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 22, 2009 8:17 AM
Points: 10, 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
Post #611880
Posted Tuesday, December 02, 2008 2:14 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 2:32 AM
Points: 298, Visits: 473
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
Post #611884
Posted Tuesday, December 02, 2008 2:21 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 22, 2009 8:17 AM
Points: 10, 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
Post #611889
Posted Tuesday, December 02, 2008 2:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 2:32 AM
Points: 298, Visits: 473
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.
Post #611894
Posted Tuesday, December 02, 2008 2:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 22, 2009 8:17 AM
Points: 10, 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
Post #611899
Posted Tuesday, December 02, 2008 3:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 2:32 AM
Points: 298, Visits: 473
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.
Post #611916
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse