April 25, 2005 at 10:06 am
I have a table that needs to get updated based on an excel file that is going to be given to me. For example, the fields would be: ID (Primary Key), price, and description. Several prices and descriptions need to be updated and they are doing so in an excel file that is going to be sent to me so I can update the appropriate IDs. I was going to import the excel file into another table and then run an update statement to update the price and description (and all other fields) by joining on the IDs from the new table and the table that needs updated. How could I create an update statement that would update ALL fields in a table without doing
update Table1
Set Price = p.price, desc = p.desc, etc.........from temp1 p where table1.id = p.id
Thanks!
April 25, 2005 at 10:27 am
Well if you're that lazy, and you know that all of the data is in the new( excell ) table (no columns missing), you could join the two table to perform a delete in the destination table and then an insert into/select from ...
Of course wrapping both with a single transaction...
But do yourself a favor and start using an editor that allows column cut/paste and customizeable macros. That way, you can be more correct, it'll be easier to code, and your code will be more readable.
April 26, 2005 at 2:59 am
Am assuming that the column names will be the same in both the new and old tables. You can use the below query then:
Declare @updateSQL varchar(1000)
Set @updateSQL = 'Update Table1 Set '
Select @updateSQL = @updateSQL + '['+column_name+'] = p.[' +column_name+'],' From information_schema.columns
where [table_name]='Temp' order by ordinal_position
Set @updateSQL = LEFT(@updateSQL,len(@updateSQL)-1)
Set @updateSQL = ' From Temp P,Table1 Where table1.id=P.id'
sp_ExecuteSQL @updateSQL
April 26, 2005 at 6:45 am
Why do you guys keep posting dynamic sql solution when he clearly doesn't need one????????????
If he's too lazy to write a 3 column update, and by the way the same he posted here, then he should definitly start seeking a new career.
April 26, 2005 at 7:38 am
I'm sorry, it's not a three column update, it's at least a 30 column update and I already found a separate solution that would cover this, sometimes I just like to get others opinions so I can increase my SQL skills. Extremely offended by your posting.
April 26, 2005 at 7:57 am
Still doesn't require dynamic sql. I'm not mad at you but at everybody who suggest these solution. Still 30 columns is not that long to write since you can use QA to generate the code for you. Sorry if I offended you.
April 26, 2005 at 8:05 am
Well, if anything, I appreciate everyone's replies because it always gives me insight on other issues and is a good way to enhance my SQL knowledge - which I think is one of the main reasons this forum exists.
April 26, 2005 at 8:10 am
JMeyer32,
You should not feel offended. Remi was really trying to disuade others from pointing you in the wrong direction. I know that sometimes it does not come accross as you expect but I sure that was not the intention. By the way I think 30 columns are not that many. I have worked with 300-400
My advice to you is to try to get out of the tools as much as you can!
For example:
- knowing that dragging the Columns folder on QA from under a table into the editor gives you a column list
- That Right clicking a table can generate CRUD statements for you ... etc
But Never ever sacrifice quality for typing length
* Noel
December 21, 2012 at 12:29 pm
JUser_DBA (4/26/2005)
I'm sorry, it's not a three column update, it's at least a 30 column update and I already found a separate solution that would cover this, sometimes I just like to get others opinions so I can increase my SQL skills. Extremely offended by your posting.
I'm with you Journeyman. I have a 75 columns table and I would rather write a 4-5 lines query then a monstrous one. Plus that if the table structure changes I would rather not go through all my queries to add the new column...
The answer from above, actually helped me...
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply