Copy data from one table to another in SQL using .VBS

How do I copy data from one table in sql to another. I don't need all fields from one table, but I'd like to select specific fields and add them to another table.

example

SQL table 1

Employees       Contract      E-mail      Owner


Then select only two of the fields



SQL table 2


Contract       Owner

These arent' the exact names of the columns I need, but something similar
Darkejo1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
what part of the problem in unclear?
I presume the SQL part:
INSERT INTO table2 ( contract, email)
 select contract, email FROM table1 

Open in new window

0
RiteshShahCommented:
this should work. do keep your own connection string.




Set objConnection = CreateObject("ADODB.Connection")
 
objConnection.Open _
    "Provider=SQLOLEDB;Data Source=MSSQLSERVER;" & _
        "Trusted_Connection=Yes;Initial Catalog=databaseName" 
 
 
sql = "INSERT INTO SQLtable2 ( contract, email) select contract, email FROM SQLtable1"
 
objConnection.Execute(sql)

Open in new window

0
Darkejo1Author Commented:
I get that, but I'm using a .vbs scirpt to do the work. I don't know how I would do something like the following: I might not have explained it correctly in the initial post. SOmething like the following is what i'm looking for.


const adopenstatic=3
const adlockoptimistic =3
 
 
set objconection = createobject("ADODB.CONNECTION")
set objrecordset = createobject("ADODB.recordset")
 
objeconnection.open _
    "driver={SQL Server}; " & _
       "server=usfmtl0000;database=input_database;uid=intranet; pwd=password123;"
 
objeconnection1.open _
    "driver={SQL Server}; " & _
       "server=usfmtl0000;database=output_database;uid=intranet; pwd=password123;"
 
objrecordset.open "select * from dbo.table_in_input_database", _
     objconnection, adopenstatic, adlockoptimistic
row = 1
do until objrecordset(row) = ""
objrecordset.addnew
objrecordset1row = objrecordset(row)
loop
row = row + 1

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Guy Hengel [angelIII / a3]Billing EngineerCommented:
outch... that is (more or less) the worst method to do, database speaking..
const adopenstatic=3
const adlockoptimistic =3
  
set objconection = createobject("ADODB.CONNECTION")
 
objeconnection.open _
    "driver={SQL Server}; " & _
       "server=usfmtl0000;database=input_database;uid=intranet; pwd=password123;"
 
objeconnection.execute( "INSERT INTO table2 ( contract, email) select contract, email FROM table1  ")
objeconnection.close

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RiteshShahCommented:
try using following script, you have to give database name along with table name as you are referring two different databases.



  
set objconection = createobject("ADODB.CONNECTION")
 
objeconnection.open _
    "driver={SQL Server}; " & _
       "server=usfmtl0000;database=input_database;uid=intranet; pwd=password123;"
 
objeconnection.execute( "INSERT INTO input_database..SQLtable2 ( contract, email) select contract, email FROM output_database..SQLtable1  ")
objeconnection.close

Open in new window

0
RiteshShahCommented:
>>"INSERT INTO input_database..SQLtable2 ( contract, email) select contract, email FROM output_database..SQLtable1  "<<


copy data of output_database..SQLtable1 to input_database..SQLtable2 . if you want to do reverse than change the place of tablename with each other.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.