SQLServerCentral.com / SQL Server 2005 General Discussion / SQL Server 2005 / Dynamic update statement / Latest PostsInstantForum.NET v99.99.99SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comFri, 09 Dec 2016 22:00:19 GMT20RE: Dynamic update statementhttp://www.sqlservercentral.com/Forums/Topic611880-149-1.aspx1. 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:[code]set @NewColumn = NULLselect @NewColumn = ParsedTableColumnName from mapping table where DestTableColumnName = 'your value'[/code]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.Tue, 02 Dec 2008 03:01:50 GMTVic.KRE: Dynamic update statementhttp://www.sqlservercentral.com/Forums/Topic611880-149-1.aspxYes 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 hasFieldName 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 likeDestTableColumnName ParsedTableColumnName"CustomerName" "Name"Can I read from such a file or table and update the corresponding column?PoojaTue, 02 Dec 2008 02:44:43 GMTPooja_prasadRE: Dynamic update statementhttp://www.sqlservercentral.com/Forums/Topic611880-149-1.aspxI 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.Tue, 02 Dec 2008 02:33:08 GMTVic.KRE: Dynamic update statementhttp://www.sqlservercentral.com/Forums/Topic611880-149-1.aspxThanks Vic.KI 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 nameAddress addressCustomer NameCustomerPhone PhoneHow can I fetch the column name as Customer whenever i have the parsed value for Name?RegardsPoojaTue, 02 Dec 2008 02:21:54 GMTPooja_prasadRE: Dynamic update statementhttp://www.sqlservercentral.com/Forums/Topic611880-149-1.aspxYou can use system views sys.objects and sys.columns to check, if the columns exist in your tablesInformation on views:[url]http://msdn.microsoft.com/en-us/library/ms190324(SQL.90).aspx[/url][url]http://msdn.microsoft.com/en-us/library/ms176106(SQL.90).aspx[/url]Tue, 02 Dec 2008 02:14:34 GMTVic.KDynamic update statementhttp://www.sqlservercentral.com/Forums/Topic611880-149-1.aspxHiI 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 + "=" + valueend forPossible problem:tblCopyTo fields:addresscustomercustomerphoneso "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?thanksPoojaTue, 02 Dec 2008 02:05:02 GMTPooja_prasad