Unix Technical Forum

SEO

vBulletin Search Engine Optimization


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:48 AM
jimserac@yahoo.com
 
Posts: n/a
Default 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\UpdateFinal1.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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:49 AM
John Bell
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

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\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
8.0;database=c:\excel\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
....

John




<jimserac@yahoo.com> wrote in message
news:1117817010.651125.209050@f14g2000cwb.googlegr oups.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\UpdateFinal1.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
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:49 AM
jimserac@yahoo.com
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

John Bell wrote:
> Hi


> UPDATE C1R0
> SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
> 8.0;database=c:\excel\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
> col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
> 8.0;database=c:\excel\UpdateFinal1.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






> <jimserac@yahoo.com> wrote in message
> news:1117817010.651125.209050@f14g2000cwb.googlegr oups.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\UpdateFinal1.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
> >


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:49 AM
John Bell
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

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

<jimserac@yahoo.com> wrote in message
news:1117818971.431454.223740@z14g2000cwz.googlegr oups.com...
> John Bell wrote:
>> Hi

>
>> UPDATE C1R0
>> SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
>> 8.0;database=c:\excel\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
>> col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
>> 8.0;database=c:\excel\UpdateFinal1.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
>
>
>
>
>
>
>> <jimserac@yahoo.com> wrote in message
>> news:1117817010.651125.209050@f14g2000cwb.googlegr oups.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\UpdateFinal1.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
>> >

>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 07:49 AM
Stu
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 07:49 AM
jimserac@yahoo.com
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

John Bell wrote:

> You can also use something like:
>
> UPDATE C1R0
> SET col1 = ( SELECT col1 FROM [C1R0$] IN 'Excel
> 8.0;database=c:\excel\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
> col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
> 8.0;database=c:\excel\UpdateFinal1.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;DATABASE=c:\excelxxx\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\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
> col2 = ( SELECT col2 FROM [C1R0$] IN 'Excel
> 8.0;database=c:\excel\UpdateFinal1.xls'; WHERE [C1R0$].PK = C1R0.PK ),
> ...
>
> John
>
>
>
>
> <jimserac@yahoo.com> wrote in message
> news:1117817010.651125.209050@f14g2000cwb.googlegr oups.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\UpdateFinal1.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
> >


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 07:49 AM
John Bell
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 07:50 AM
jimserac@yahoo.com
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

>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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 07:50 AM
Stu
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-29-2008, 07:50 AM
jimserac@yahoo.com
 
Posts: n/a
Default Re: SQL UPDATE Database from Excel Table

>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

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 08:29 PM.


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559