473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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!

Apr 3 '07 #1
4 5264
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
Apr 4 '07 #2
On Apr 3, 11:55 pm, Ed Murphy <emurph...@socal.rr.comwrote:
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!

Apr 4 '07 #3
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.
Apr 4 '07 #4
On Apr 4, 8:44 am, Ed Murphy <emurph...@socal.rr.comwrote:
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

Apr 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex | last post by:
Hello, I have a rather large table in MS SQL 2000 that I'm writing reports in Crystal from, but I'm unsure how to get the various data files (from different departments) into the table. Below...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
5
by: A.Dagostino | last post by:
hi i need to update an SQL Table when user select or unselect a checkbox control. How Can i do? Thanks Alex
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
8
by: carlospedr | last post by:
I have to insert data from about 30 tables into a single table (Users), to do so i used a cursor and a bit of dynamic sql, this should work fine if the tables have to do the select from had the...
5
by: njb35 | last post by:
Hi all I'm beginning my foray from VBA into VB 2005 Express, and enjoying some of the efficiencies it provides! I'm stuck with some dataset handling however that I _think_ can be automated but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.