cyberwalker2000
asked on
problems with Query INSERT IN
Guys,
I need to take the data from a SQL Server 2000 Database to a Access Database. To accomplish this task, first I tryed to do from a Access database to another access database, so I wrote the code below:
Dim Conn As New OleDb.OleDbConnection(Stri ngConn3)
Conn.Open()
SQL = "INSERT INTO " + Ds.Tables("carregatabela") .Rows(j)(" TableName" ) + " ( " + VarCamposInsere + " ) IN '" + ArqDes + "'"
SQL += " SELECT " + VarSelect
SQL += " FROM " + Ds.Tables("carregatabela") .Rows(j)(" TableName" )
Dim Insert As New OleDb.OleDbCommand(SQL, Conn)
Try
'Executa a Query acima
Insert.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
'Exit Sub
End Try
Conn.Close()
where:
ArqDes: Is the destiny path of the access file
StringConn3: Is the origin OLEDB connection of the access file.
When I do this, everything works fine. But when i change the StringConn3 to a SQL Server Connection, using a SQLOLEDB, the system give me the follow error:
"Error in IN syntax. "
Does anyone knows where is it wrong?
Thanks in advance
Angelo
I need to take the data from a SQL Server 2000 Database to a Access Database. To accomplish this task, first I tryed to do from a Access database to another access database, so I wrote the code below:
Dim Conn As New OleDb.OleDbConnection(Stri
Conn.Open()
SQL = "INSERT INTO " + Ds.Tables("carregatabela")
SQL += " SELECT " + VarSelect
SQL += " FROM " + Ds.Tables("carregatabela")
Dim Insert As New OleDb.OleDbCommand(SQL, Conn)
Try
'Executa a Query acima
Insert.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
'Exit Sub
End Try
Conn.Close()
where:
ArqDes: Is the destiny path of the access file
StringConn3: Is the origin OLEDB connection of the access file.
When I do this, everything works fine. But when i change the StringConn3 to a SQL Server Connection, using a SQLOLEDB, the system give me the follow error:
"Error in IN syntax. "
Does anyone knows where is it wrong?
Thanks in advance
Angelo
Do a debug print of SQL right before it's executed and post the exact SQL statement that it's trying to execute.
please dump a sample of the actual SQL generated and paste it here
I belive that the syntax you are using is invalid for sql server ..
Jay
I belive that the syntax you are using is invalid for sql server ..
Jay
DANG is there an echo in here or what
Jay
Jay
ASKER
ok. That's is the Query:
"INSERT INTO Entrada ( [CodigoEnt],[Data],[NumNf] ,[CodForn] ,[Nome],[V rTotal] ) IN 'D:\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb' SELECT Entrada.[CodigoEnt],Entrad a.[Data],E ntrada.[Nu mNf],Entra da.[CodFor n],Entrada .[Nome],En trada.[VrT otal] FROM Entrada"
"INSERT INTO Entrada ( [CodigoEnt],[Data],[NumNf]
The Syntax you are looking for is
SELECT *
FROM [c:\path\DbName.mdb].table name
so your query would be ..
INSERT INTO Entrada ( [CodigoEnt],[Data],[NumNf] ,[CodForn] ,[Nome],[V rTotal] ) SELECT Entrada.[CodigoEnt],Entrad a.[Data],E ntrada.[Nu mNf],Entra da.[CodFor n],Entrada .[Nome],En trada.[VrT otal] FROM [D:\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb].En trada
JAY
SELECT *
FROM [c:\path\DbName.mdb].table
so your query would be ..
INSERT INTO Entrada ( [CodigoEnt],[Data],[NumNf]
JAY
ASKER
Now it's giving me the follow error Jay:
"Invalid object name 'D:\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb.Ent rada'."
"Invalid object name 'D:\Visual Studio Projects\BackBD\bin\Bdtemp
If your ultimate goal is to Transfer data from SQL To Access why don't you use a DTS package.
Open SQL Enterprise Manager
Right click on the table you want to export
Then Select All tasks >> Export Data
Then just follow the steps by choosing your Source which is SQL server
and destination which is your MS Access DB
Open SQL Enterprise Manager
Right click on the table you want to export
Then Select All tasks >> Export Data
Then just follow the steps by choosing your Source which is SQL server
and destination which is your MS Access DB
ASKER
yes, I could do that, but the problem is thar my manager want I write a program with that goal because he use these access files like a backup of his databases. So he would need to run the DTS several times for each database we have.
So the goal of this program is to make easier for him to backup his databases.
I already tryed to show him the backup tools that SQL Server have, but he prefer in that way....
So the goal of this program is to make easier for him to backup his databases.
I already tryed to show him the backup tools that SQL Server have, but he prefer in that way....
Are you sure that file exists?
it must be EXACTLY that path.
Jay
it must be EXACTLY that path.
Jay
Since i gather this is running on the server you will probably have to use a UNC name
like \\MYWORKSTATION\d$\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb
Jay
like \\MYWORKSTATION\d$\Visual Studio Projects\BackBD\bin\Bdtemp
Jay
ASKER
Not yet Jay... First i'm trying to run this tool in my local machine....
What I don't understand is that the query worked with the Access connection, but not with the SQL Server... Can be the connection am i using?
Provider=SQLOLEDB.1;Passwo rd=cpd;Per sist Security Info=False;User ID=sa;Initial Catalog=BDNatco_ADM;Data Source=172.19.193.71
What I don't understand is that the query worked with the Access connection, but not with the SQL Server... Can be the connection am i using?
Provider=SQLOLEDB.1;Passwo
The sql syntax is different,
PLUS the query is actually running on the SERVER with sql ... not thru jet on your local machine
Jay
PLUS the query is actually running on the SERVER with sql ... not thru jet on your local machine
Jay
You can also use a DSN connection to do this.
set up the accessDB as a linked server (only need to do this once)
then you can access it easily :
sp_addlinkedserver @server = 'MyAccessServer', @provider = 'Microsoft.Jet.OLEDB.4.0', @datasrc = 'D:\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb.Ent rada' , @srvproduct = 'MSAccess'
then you can access (pardon the pun!) it as a regular server -
SELECT * FROM MyAccessServer...MyTable
then you can access it easily :
sp_addlinkedserver @server = 'MyAccessServer', @provider = 'Microsoft.Jet.OLEDB.4.0',
then you can access (pardon the pun!) it as a regular server -
SELECT * FROM MyAccessServer...MyTable
your data source for this should only be
@datasrc = 'D:\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb'
this though
Jay
@datasrc = 'D:\Visual Studio Projects\BackBD\bin\Bdtemp
this though
Jay
sorry, yes the datasource is the database file, not the specific table, my mistake.
ASKER
Sorry guys, but i never did this before..
How can I set up the linked server?
and another question:
When I use this query: SELECT * FROM MyAccessServer...MyTable
I will be doing a Select in a Access file, won't I ? Actually, I want to do a SELECT in a SQL Server and Insert the data inside the Access file....
How can I set up the linked server?
and another question:
When I use this query: SELECT * FROM MyAccessServer...MyTable
I will be doing a Select in a Access file, won't I ? Actually, I want to do a SELECT in a SQL Server and Insert the data inside the Access file....
In Query Analyser, just run this code to set up your linked server (replace the details for your file name/path etc)
sp_addlinkedserver @server = 'MyAccessServer', @provider = 'Microsoft.Jet.OLEDB.4.0', @datasrc = 'D:\Visual Studio Projects\BackBD\bin\Bdtemp \BDNatco_A DM.mdb' , @srvproduct = 'MSAccess'
Then, if you want to insert data into the access file, do
INSERT INTO MyAccessServer...MyTable
SELECT * FROM LocalTable
sp_addlinkedserver @server = 'MyAccessServer', @provider = 'Microsoft.Jet.OLEDB.4.0',
Then, if you want to insert data into the access file, do
INSERT INTO MyAccessServer...MyTable
SELECT * FROM LocalTable
ASKER
Ok i'll put this code in my system, but are there any problems to execute this code everytime the user run my program? And How does this linked server affect my server?
ASKER
I searched with the Enterprise Manager into Stored Procedures folders, and didn't find any sp_addlinkedserver.
Shouldn't it be there?
Shouldn't it be there?
It should be in master database.
You only need to run this code once ever. You don't run it every time you want to link servers.
The only effect is that anybody who has permissions to log onto SQLServer will be able to view the Access DB. It won't have any performance issues. Nor can the AccessDB 'link' back to the SQL Server - it is a one-way link only.
You only need to run this code once ever. You don't run it every time you want to link servers.
The only effect is that anybody who has permissions to log onto SQLServer will be able to view the Access DB. It won't have any performance issues. Nor can the AccessDB 'link' back to the SQL Server - it is a one-way link only.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.