Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Select INTO, UNION

Reply
Thread Tools

Select INTO, UNION

 
 
kcalanyuan@yahoo.com.hk
Guest
Posts: n/a
 
      06-08-2009
I have two databases with same column name, I want to combine all the
data into a temp table in the fly, after display the result then drop
the table, like below

dbA
FNo FName Amount
101 BBB 100
102 CCC 200

dbB
FNo FName Amount
101 BBB 400
102 CCC 500

dbtemp
FNo FName Amount
101 BBB 500
102 CCC 700

I used the following clause but failed,
SQL = "CREATE TABLE dbtemp (FNo varchar(10), FName varchar(100),
DAmount INT)"
SQL = " And SELECT FNo, FName, Amount INTO dbtemp IN 'dbA.mdb' FROM
'dbA'"
SQL = " And SELECT FNo, FName, Amount INTO dbtemp IN 'dbB.mdb' FROM
'dbB'"
Set rs = GetMdbRecordset( "dbtemp", SQL)

Can anyone help me ? Thanks !
 
Reply With Quote
 
 
 
 
Mary
Guest
Posts: n/a
 
      06-09-2009
After trying one whole day, I find a solution to make it work :

Set conn = GetMdbConnection( "dbtemp.mdb")
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
SQL = "SELECT FNo, FName, Amount INTO Total From
d:\Sites\fund\db1.mdb.MData"

Cmd.CommandText = SQL
Cmd.Execute

It really copy all the data from db1.mdb.Mdata to dbtemp.Mdata, I really
see the data is inside. I know it is a very difficult job to be completed.

But then I try to copy another database using :
SQL= SQL & " Union All SELECT FNo, FName, Damount INTO Total From
d:\Sites\fund\MDataUnhcr.mdb.MData"

It says the table is existed. Error !

Then I try many many clauses, all error !

In fact, we have over twenty companys with individual database, I just need
to sum up all sales amount for each colleague in all databases, it seems
very simple, is it possible using ASP ? Or may be I need to open each
database, then while not rs.eof, then rs("FNo") = rs1("FNo"), ......
rs.movenext, rs.close...open another again, finally get the answer. Or
transform all databases to tables and wedge into a database in order to make
"Union All", "Join" clauses more easy ? But this database may large than
20GB because each of this table contains various data and structure.

A very simple simple job, please help me !




 
Reply With Quote
 
 
 
 
Daniel Crichton
Guest
Posts: n/a
 
      06-10-2009
Mary wrote on Wed, 10 Jun 2009 00:27:29 +0800:

> After trying one whole day, I find a solution to make it work :


> Set conn = GetMdbConnection( "dbtemp.mdb")
> Set cmd = Server.CreateObject("ADODB.Command")
> Set cmd.ActiveConnection = conn
> SQL = "SELECT FNo, FName, Amount INTO Total From
> d:\Sites\fund\db1.mdb.MData"


> Cmd.CommandText = SQL
> Cmd.Execute


> It really copy all the data from db1.mdb.Mdata to dbtemp.Mdata, I
> really see the data is inside. I know it is a very difficult job to be
> completed.


> But then I try to copy another database using :
> SQL= SQL & " Union All SELECT FNo, FName, Damount INTO Total From
> d:\Sites\fund\MDataUnhcr.mdb.MData"


> It says the table is existed. Error !


> Then I try many many clauses, all error !


SELECT ... INTO creates a new table - that's why you get an error, because
the first one creates it in your temp database and the subsequent ones
cannot do so.

> In fact, we have over twenty companys with individual database, I just
> need to sum up all sales amount for each colleague in all databases,
> it seems very simple, is it possible using ASP ? Or may be I need to
> open each database, then while not rs.eof, then rs("FNo") =
> rs1("FNo"), ...... rs.movenext, rs.close...open another again, finally get
> the answer. Or transform all databases to tables and wedge into a database
> in order to
> make "Union All", "Join" clauses more easy ? But this database may
> large than 20GB because each of this table contains various data and
> structure.



If you don't actually need the temp database table then you could use UNIONs
to select the data, eg.

SELECT FNo, FName, Damount FROM d:\Sites\fund\db1.mdb.MData
UNION ALL
SELECT FNo, FName, Damount FROM d:\Sites\fund\MDataUnhcr.mdb.MData


and if you need to do any aggregation on the data you can even use a
subquery, eg.


SELECT FNo, FName, SUM(Damount) FROM
(
SELECT FNo, FName, Damount FROM d:\Sites\fund\db1.mdb.MData
UNION ALL
SELECT FNo, FName, Damount FROM d:\Sites\fund\MDataUnhcr.mdb.MData
) as A
GROUP BY FNo, FName



and if you really do need to store this data in a table in dbtemp then this
should work:


SELECT FNo, FName, Damount
INTO Total
FROM
(
SELECT FNo, FName, Damount FROM d:\Sites\fund\db1.mdb.MData
UNION ALL
SELECT FNo, FName, Damount FROM d:\Sites\fund\MDataUnhcr.mdb.MData
) as A


You could use the

--
Dan


 
Reply With Quote
 
Mary
Guest
Posts: n/a
 
      06-18-2009
Thanks very much . It works !! You are great !!


<>
???????:b7c69333-7cac-4c51-9c16-...
>I have two databases with same column name, I want to combine all the
> data into a temp table in the fly, after display the result then drop
> the table, like below
>
> dbA
> FNo FName Amount
> 101 BBB 100
> 102 CCC 200
>
> dbB
> FNo FName Amount
> 101 BBB 400
> 102 CCC 500
>
> dbtemp
> FNo FName Amount
> 101 BBB 500
> 102 CCC 700
>
> I used the following clause but failed,
> SQL = "CREATE TABLE dbtemp (FNo varchar(10), FName varchar(100),
> DAmount INT)"
> SQL = " And SELECT FNo, FName, Amount INTO dbtemp IN 'dbA.mdb' FROM
> 'dbA'"
> SQL = " And SELECT FNo, FName, Amount INTO dbtemp IN 'dbB.mdb' FROM
> 'dbB'"
> Set rs = GetMdbRecordset( "dbtemp", SQL)
>
> Can anyone help me ? Thanks !



 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
select gridview row without using select button? =?Utf-8?B?RGFiYmxlcg==?= ASP .Net 5 02-27-2009 02:56 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
Selection from One SELECT changes selected option of another SELECT? J. Hall HTML 2 04-21-2004 05:36 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM



Advertisments
 



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