Unix Technical Forum

How to update selected columns of a table in SQL server db using data from a Excel file?

This is a discussion on How to update selected columns of a table in SQL server db using data from a Excel file? within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I have an Excel file with 400 rows of old values and the corresponding new values. My table ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:48 PM
urprettyfriend
 
Posts: n/a
Default How to update selected columns of a table in SQL server db using data from a Excel file?

Hi,

I have an Excel file with 400 rows of old values and the corresponding
new values. My table currently has 10 columns out of which 3 columns
use the old value specified in the excel file. I need to update those
old values in the columns with the new values from the Excel file.
Please guide me as to how to proceed with this.

Thanks in advance!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:48 PM
Ed Murphy
 
Posts: n/a
Default Re: How to update selected columns of a table in SQL server db usingdata from a Excel file?

urprettyfriend wrote:

> I have an Excel file with 400 rows of old values and the corresponding
> new values. My table currently has 10 columns out of which 3 columns
> use the old value specified in the excel file. I need to update those
> old values in the columns with the new values from the Excel file.
> Please guide me as to how to proceed with this.


Import the data into a second table, then do something like this:

update Table1
set t1.c4 = t2.c4,
t1.c5 = t2.c5,
t1.c6 = t2.c6,
t1.c7 = t2.c7,
t1.c8 = t2.c8,
t1.c9 = t2.c9,
t1.c10 = t2.c10
from Table1 t1
join Table2 t2 on t1.c1 = t2.c1
and t1.c2 = t2.c2
and t1.c3 = t2.c3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:49 PM
urprettyfriend
 
Posts: n/a
Default Re: How to update selected columns of a table in SQL server db using data from a Excel file?

On Apr 3, 11:55 pm, Ed Murphy <emurph...@socal.rr.com> wrote:
> urprettyfriend wrote:
> > I have an Excel file with 400 rows of old values and the corresponding
> > new values. My table currently has 10 columns out of which 3 columns
> > use the old value specified in the excel file. I need to update those
> > old values in the columns with the new values from the Excel file.
> > Please guide me as to how to proceed with this.

>
> Import the data into a second table, then do something like this:
>
> update Table1
> set t1.c4 = t2.c4,
> t1.c5 = t2.c5,
> t1.c6 = t2.c6,
> t1.c7 = t2.c7,
> t1.c8 = t2.c8,
> t1.c9 = t2.c9,
> t1.c10 = t2.c10
> from Table1 t1
> join Table2 t2 on t1.c1 = t2.c1
> and t1.c2 = t2.c2
> and t1.c3 = t2.c3


Ed,

Thanks for ur solution. But I can't create a temp table in the db....I
already asked if I can do that, Unfortunately, I am not allowed to do
that. Please tell me if there is any other way to do this.

Thanks!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:49 PM
Ed Murphy
 
Posts: n/a
Default Re: How to update selected columns of a table in SQL server db usingdata from a Excel file?

urprettyfriend wrote:

> Thanks for ur solution. But I can't create a temp table in the db....I
> already asked if I can do that, Unfortunately, I am not allowed to do
> that. Please tell me if there is any other way to do this.


Try creating a temp table whose name starts with a # (it will go away
automatically when your session closes). They might let you do that.

Failing that, I've used this method on small files:

1) In Excel, move the three columns to the end
2) Insert a blank column between each pair of data columns
3) Edit the new blank cells in row 1 so that it looks like this:

[A1] update Table1 set c4 = '
[A2] <data>
[A3] '', c5 = '
[A4] <data>
(similarly for c6 through c10)
[A15] '' where c1 = '
[A16] <data>
[A17] '' and c2 = '
[A18] <data>
[A19] '' and c3 = '
[A20]
[A21] ''

4) Copy+paste these to the other rows
5) Copy+paste the whole thing into Notepad
6) Use search+replace to strip out all the tabs
7) Copy+paste the result into Query Analyzer and execute it

Note that you'll have to manually escape things like ' within data
fields.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 02:49 PM
thelawrencebishop@gmail.com
 
Posts: n/a
Default Re: How to update selected columns of a table in SQL server db using data from a Excel file?

On Apr 4, 8:44 am, Ed Murphy <emurph...@socal.rr.com> wrote:
> urprettyfriend wrote:
> > Thanks for ur solution. But I can't create a temp table in the db....I
> > already asked if I can do that, Unfortunately, I am not allowed to do
> > that. Please tell me if there is any other way to do this.

>
> Try creating a temp table whose name starts with a # (it will go away
> automatically when your session closes). They might let you do that.
>


Assuming DTS is being used to import the Excel workbook, a temp #table
won't work. DTS (as well as BCP and BULK INSERT) require a physical/
persistent table as a destination.

If you're not able to create a table, have the admins create a table
for you that you can use as a destination during the DTS import.

OR

Have the admins import the workbook into the database for you which
you can later use in your UPDATE.

> Failing that, I've used this method on small files:
>
> 1) In Excel, move the three columns to the end
> 2) Insert a blank column between each pair of data columns
> 3) Edit the new blank cells in row 1 so that it looks like this:
>
> [A1] update Table1 set c4 = '
> [A2] <data>
> [A3] '', c5 = '
> [A4] <data>
> (similarly for c6 through c10)
> [A15] '' where c1 = '
> [A16] <data>
> [A17] '' and c2 = '
> [A18] <data>
> [A19] '' and c3 = '
> [A20]
> [A21] ''
>
> 4) Copy+paste these to the other rows
> 5) Copy+paste the whole thing into Notepad
> 6) Use search+replace to strip out all the tabs
> 7) Copy+paste the result into Query Analyzer and execute it
>
> Note that you'll have to manually escape things like ' within data
> fields.


Creative solution, but a lot of unnecessary work. An admin can push
the data into the database in 30 seconds using DTS.

All the best,
Lawrence Bishop

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com