473,729 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL UPDATE Database from Excel Table

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 Server
database that we have.

Question follows:

The following SQL statement, used in VBScript,
will COPY a table from Excel to an Access mdb.

SQL = "SELECT * INTO C1R0" & _
" FROM [C1R0$] IN ''" & _
" 'Excel 8.0;database=c: \excel\UpdateFi nal1.xls';"

What is the SQL statement that will
UPDATE an already existing Access table
with all rows from Excel spreadsheet?

The columns of both Spreadsheet and database are the
same.

Thanks
Jim

Jul 23 '05 #1
12 22943
Hi

They are normally good on this in the access forums! So you will have to
make do with a non-access person's reply!!

In SQL Server you have

UPDATE A
SET col1 = E.col1,
col2 = E.col2,
.....
FROM C1R0 A
JOIN [C1R0$] E ON E.PK = A.PK

I am not sure where the filename would go!!

You can also use something like:

UPDATE C1R0
SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
....

John


<ji******@yahoo .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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 Server
database that we have.

Question follows:

The following SQL statement, used in VBScript,
will COPY a table from Excel to an Access mdb.

SQL = "SELECT * INTO C1R0" & _
" FROM [C1R0$] IN ''" & _
" 'Excel 8.0;database=c: \excel\UpdateFi nal1.xls';"

What is the SQL statement that will
UPDATE an already existing Access table
with all rows from Excel spreadsheet?

The columns of both Spreadsheet and database are the
same.

Thanks
Jim

Jul 23 '05 #2
John Bell wrote:
Hi UPDATE C1R0
SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
Many thanks, I will give this a shot.

I think that the place for referencing the location of the
Excel spreadsheet is provided for by an extension
to SQL (Transact-SQL?) but I have not done a whole
heck of alot of SQLing to be sure of all the details
and am learning it now.

I was hoping, given the elegant simplicity of the SQL
table copy code, that there might be an equally elegant
UPDATE SQL, particularly since I wanted to update the entire
table.

Will post if I find it.

Thanks again
Jim

<ji******@yahoo .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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 Server
database that we have.

Question follows:

The following SQL statement, used in VBScript,
will COPY a table from Excel to an Access mdb.

SQL = "SELECT * INTO C1R0" & _
" FROM [C1R0$] IN ''" & _
" 'Excel 8.0;database=c: \excel\UpdateFi nal1.xls';"

What is the SQL statement that will
UPDATE an already existing Access table
with all rows from Excel spreadsheet?

The columns of both Spreadsheet and database are the
same.

Thanks
Jim


Jul 23 '05 #3
Hi

Transac-SQL is for SQL Server I believe it may be called Access-SQL for
access!

One thing you may want to do is to create a staging table using you original
statement and then us that table to do the updates, that may avoid any
restrictions with the extensions.

John

<ji******@yahoo .com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
John Bell wrote:
Hi

UPDATE C1R0
SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),


Many thanks, I will give this a shot.

I think that the place for referencing the location of the
Excel spreadsheet is provided for by an extension
to SQL (Transact-SQL?) but I have not done a whole
heck of alot of SQLing to be sure of all the details
and am learning it now.

I was hoping, given the elegant simplicity of the SQL
table copy code, that there might be an equally elegant
UPDATE SQL, particularly since I wanted to update the entire
table.

Will post if I find it.

Thanks again
Jim

<ji******@yahoo .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
>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 Server
> database that we have.
>
> Question follows:
>
> The following SQL statement, used in VBScript,
> will COPY a table from Excel to an Access mdb.
>
> SQL = "SELECT * INTO C1R0" & _
> " FROM [C1R0$] IN ''" & _
> " 'Excel 8.0;database=c: \excel\UpdateFi nal1.xls';"
>
> What is the SQL statement that will
> UPDATE an already existing Access table
> with all rows from Excel spreadsheet?
>
> The columns of both Spreadsheet and database are the
> same.
>
> Thanks
> Jim
>

Jul 23 '05 #4
Stu
It doesn't sound as if you are doing an UPDATE, but rather a "replace
all the contents" move. An UPDATE statement assumes that you have
matching data between your source and targets, and can link to change
the vaules of that data. It sounds as if you just want to delete the
data, and start over.

You have a couple of options, but they will differ between Access and
SQL Server. SQL Server will allow you to issue multiple SQL statements
in a single transaction by using the .; seperator. I don't believe
that Access will.

In either case, you could either DROP the table, and then recreate it
using the SELECT...INTO syntax, or simply DELETE the data, and INSERT
the new data. Your choice may depend on what rights you have, how much
data there is, whether there are indexes on the table, etc...

Just offering some ideas.

Jul 23 '05 #5
John Bell wrote:
You can also use something like:

UPDATE C1R0
SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK )

Well I thought I would just be conservative and try and update
only one column. It gets past the SQL syntax parser OK but then
craps out with "Invalid Filename" error message (see next line).

UPDATE C1R0 SET QTY = (SELECT QTY FROM [C1R0$] IN 'Excel
8.0;HDR=Yes;DAT ABASE=c:\excelx xx\UpdateFinal1 .xls')

I'll be damned if I can see anything wrong with the file name.
Just to be safe, I changed the name of the folder to excelxxx but
it still does not like the file name.

I guess I'm in the realm of vendor specific SQL extension syntax
tricks - just where microsoft wants me.

Thanks
Jim
John Bell wrote: Hi

They are normally good on this in the access forums! So you will have to
make do with a non-access person's reply!!

In SQL Server you have

UPDATE A
SET col1 = E.col1,
col2 = E.col2,
....
FROM C1R0 A
JOIN [C1R0$] E ON E.PK = A.PK

I am not sure where the filename would go!!

You can also use something like:

UPDATE C1R0
SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
8.0;database=c: \excel\UpdateFi nal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
...

John


<ji******@yahoo .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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 Server
database that we have.

Question follows:

The following SQL statement, used in VBScript,
will COPY a table from Excel to an Access mdb.

SQL = "SELECT * INTO C1R0" & _
" FROM [C1R0$] IN ''" & _
" 'Excel 8.0;database=c: \excel\UpdateFi nal1.xls';"

What is the SQL statement that will
UPDATE an already existing Access table
with all rows from Excel spreadsheet?

The columns of both Spreadsheet and database are the
same.

Thanks
Jim


Jul 23 '05 #6
Hi

In that case you may want to do as I suggested in my other post and
load the data into a staging table and work from there.

John

Jul 23 '05 #7
>It doesn't sound as if you are doing an UPDATE, but rather a "replace
all the contents" move. An UPDATE statement assumes that you have
matching data between your source and targets, and can link to change
the vaules of that data. It sounds as if you just want to delete the
data, and start over. You have a couple of options, but they will differ between Access and
SQL Server. SQL Server will allow you to issue multiple SQL statements
in a single transaction by using the .; seperator. I don't believe
that Access will.


Many thanks for the ideas.

I am indeed doing an UPDATE of all rows and columns
since the update excel sheet will never have ALL of the
rows that the Access database to be updated has.

SQL should handle it in one gulp (theoretically, heh heh)
rather than need mulitple queries. As the original
post shows, I can sure copy a table from Excel to Access
in one shot - so why not update it that easily (with the
proviso that I'm updating everything) too?

Thanks Again.
J

Jul 23 '05 #8
Stu
SQL doesn't work like that; what you call a copy of a table is actually
a combination of two seperate SQL commands: a CREATE TABLE and an
INSERT statement. SQL Server and Access have created a shortcut syntax
for these two commands (the SELECT...INTO). SELECT... INTO assumes
that the target table does not exist, and that you want to create it.
If it doesn exist, it will fail.

In the scenario you're describing, it's not an UPDATE. An UPDATE SQL
statement assumes that you're going to find a particular piece (or
pieces) of data, and replace an old value with a new value. You're
not adding rows when you execute an UPDATE statement; you are simply
finding a value and replace it.

If you wat to replace all of the rows of a target table AND add new
rows, there is no one-shot command to do it. You have to a) empty the
table and add all the rows you want, or b) destroy the table, and
reissue the SELECT...INTO shortcut syntax to recreate the table with
the new data set. You could do as John suggested and UPDATE each
column in your target table with matching data from your source table,
but you have to match a key column between the two tables (or your
results will be unpredictable), and an UPDATE will not add any new
data.

I hope that clarifies a bit.

Stu

Jul 23 '05 #9
>SQL doesn't work like that; what you call a copy of a table is actually
a combination of two seperate SQL commands: a CREATE TABLE and an
INSERT statement. SQL Server and Access have created a shortcut syntax
for these two commands (the SELECT...INTO). SELECT... INTO assumes
that the target table does not exist, and that you want to create it.
If it doesn exist, it will fail.
Thanks again for most helpful clarification!! This is helping me
think of it properly.
In the scenario you're describing, it's not an UPDATE. An UPDATE SQL
statement assumes that you're going to find a particular piece (or
pieces) of data, and replace an old value with a new value. You're
not adding rows when you execute an UPDATE statement; you are simply
finding a value and replace it.


I am doing UPDATE but probably said it wrong in my posting.
I have, say 1000 rows of data in Access table.
The Excel spreadsheet has maybe 20 rows of updated data -> and
each of the 20 rows has index number which already exists in key field
of Access,
so no new records are comming in - just updates. Of the 20 rows in
Excel spreadsheet, pehaps 3 or 4 of the 32 columns (or fields) in each
of the 20 rows has changed data which must update the corresponding
fields in the corresponding rows of the Access database and replacing
an entire row in the update is just fine because the other fields that
did not change are exactly the same between Excel and Access.

I am looking at using the approach suggested in this thread as being
the most viable - use SQL to copy excel data to Access Temporary table
and then use a 2d SQL operation to update main table from temporary
one.

I am not entirely sure how to use SQL to update one table from another
but will figure it out.

I am doing this from a VBScript and wanted to avoid recordsets and the
goofy necessity of naming every single one of the 32 columns in the
update code
but apparently even in SQL this will be necessary - I would have
thought that there would be more sophisticated way since I am OK with
updating ALL the fields of the change rows. Again, I just looked at
the elegant simplicity of the "SELECT *" operator
in copying all the table to another place and thought that there should
be an equally elegant UPDATE operation so I'm just letting my
imagination get ahead of reality.
Again many thanks to you and everyone for the assistance.

Jimserac

Jul 23 '05 #10

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

Similar topics

1
16856
by: Nono | last post by:
Hello, I am trying to update records in my database from excel data using vba editor within excel. In order to launch a query, I use SQL langage in ADO as follwing: ------------------------------------------------------------ Dim adoConn As ADODB.Connection Dim adoRs As ADODB.Recordset Dim sConn As String Dim sSql As String Dim sOutput As String
1
5295
by: jimserac | last post by:
The following SQL statement, used in VBScript, will COPY a table from Excel to an Access mdb. SQL = "SELECT * INTO C1R0" & _ " FROM IN ''" & _ " 'Excel 8.0;database=c:\excel\UpdateFinal1.xls';" What is the SQL statement that will UPDATE an already existing Access table
2
1950
by: Chris Fink | last post by:
Any idea why the database table is not being updated with the contents of the excel file loaded into the dataset? Any help is appreciated! // load database table, shell only string cnString = "DSN=XXX D5200"; OdbcConnection myConnection = new OdbcConnection(cnString); OdbcDataAdapter da = new OdbcDataAdapter("select * from DB where 1=2;", myConnection); DataSet ds = new DataSet(); da.Fill(ds, "ccginput"); // load excel file into...
3
2339
by: Roy | last post by:
Hi Access gurus, I have a A2K application.The data in the database is updated daily by a excel download.I have a master n related tables keyed in by a OrderID.I have a problem in updating data.If it is a one to one update,i face no problem as I update every fields.But let's say if there is a master record with ID and three corresponding related entries for this on day 1.But on the next day,there was a change on related records 2 & 3 but...
2
7151
by: ruthboaz | last post by:
Hi, I am exporting a table with data to an excel file.. Some fields are empty in the exported file while the others and the key field, has values. The empty field(s) in excel, will be entered with values by another department. I require to update the access db from the updated excel fields to the respective fields in the database.. While using import from the file menu, I see that the information is append the table..
5
1933
by: Chris | last post by:
I got got a pile of bad email addresses to update in our SQL database. I know how to do this for individual records using the update command. Is there a way to execute an update using the list of addresses in an excel spreadsheet or some other form of list? This kind of scripting is new territory for me.
4
5283
by: urprettyfriend | last post by:
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!
7
5767
by: semijoyful | last post by:
OS: Win XP SP2 Access version: 2003 Excel version: 2003 I am new at this, as I am sure you have gathered from this post title:) I am working on a form where users can input data in Access and at the end of the day be able to press a button on the form that would update that data to a single exel spreadsheet. The following is the code for the module. When I click on the button in the form, It takes me to VBS Debugger. I then type...
2
2578
by: LadyRed2 | last post by:
Hello, I am new to Excel and SQL server, so please be patient with me. I have written code with the help of others to update a table in a sql database from an excel form. The connection code works as I have used it for a query before. Here is the code Private Sub CommandButton3_Click() 'to Update or Insert new into the form
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.