I have a query in Access which does some calculations which (I think) can't be done in SQL Server directly because of a vital local table in Access. I used to use a append query in Access to save this data. I'm now working on replacing the Access database with a SQL Server database.
Is there a way to get the Access query results saved in SQL Server?
Thanks in advance,
Access can directly execute INSERT INTO queries to SQL server tables.
The easiest way is to use a linked table, but if that's undesirable for whatever reason, you can use the connection string in the query. It has to be a valid string for DAO (e.g. ODBC string starting with ODBC;).
INSERT INTO [ODBC;Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Trusted_Connection=Yes;].[My Table] (Column1, Column2)
SELECT Column1, Column2
FROM SomeQuery
Related
I am trying to run SQL through excel and I have the following SQL query within the data connection:
WITH TEST_DB as (select * from TEST_ADM.KPI_SAMPLE_TEST)
SELECT DISTINCT
BASE.YEAR_MONTH AS YEAR_MONTH
FROM TEST_DB BASE
If I try running this with SQL Developer, it will run successfully, no problems but when I try to run it in Excel, I get the message:
The query did not run, or the database table could not be opened.
Check the database server or contact your database administrator. Make sure the external database is available and hasn't been moved or reorganised, then try the operation again.
Is there some sort of limitation within Excel that prevents me from using WITH statements?
If I remove the statement and select from the table directly, it works but I need to use the WITH data table 6 times in my code and its rather long.
Oh, the database is an oracle database.
How do you access another database from same server? Got server name using SELECT ##SERVERNAME, and then did servername.dbo.mydatabasename.mytablename but query analyzer says Invalid object name. Any ideas? Am doing
insert into Myservername.Mydatabasename.Mytablename
(Email,Username1)
Values
('h','h')
Using MS SQL Server 2008, on same server
Assuming you're using MS SQL Server, fully qualified references are in the form:
[servername].[databasename].[schema].[object]
On the same server you do not need the [servername] reference.
In your case, you reversed the databasename and schema. It should be:
servername.mydatabasename.dbo.mytablename
Your INSERT should look like:
insert into Mydatabasename.Schema.Mytablename
(Email,Username1)
Values ('h','h')
(probably your Schema here is dbo)
You would include the [servername] component when performing an operation across a linked server in which case [servername] would be the name of the linked server, which incidentally may not actually be the the same as the hostname/instance name of the remote server.
Is it not rather mydatabasename.DBO.mytablename ? And if your database is on the same server, you normally don't have to use the servername.
Should be
insert into Myservername.Mydatabasename.MySchema.Mytablename
(Email,Username1)
Values
('h','h')
Though as you're on the same server you don't need Myservername. In your example which was using three part notation it would have assumed that Mydatabasename was the schema; hence the error
I am working in Visual Studio and using the SQL manager built into the studio. Now I am connecting to several databases and I would very much like to be able to save and open my SQL queries and still have them access the correct database and table.
So:
Database servers:
db.company.com
databasenumber1
databasenumber2
databasenumber3
db2.company.com
databasenumber1
databasenumber2
databasenumber3
db3.company.com
databasenumber1
databasenumber2
databasenumber3
Now I wish to write an sql query that does something simple, lets say:
select * from users where userid = '12';
However I want to select this from database server db2 and from database databasenumber3.
How do I write that in a use statement? Or is there something other than "use"??
Working among several databases in once script file requires USE followed by GO statement.
USE db1;
GO
SQL statements ...
...
USE db2;
GO
SQL statements ...
...
Another option is to use server.dbname.tablename format but that strictly requires that all of your databases are hosted on same server.
SELECT * FROM server.db1.table1
SELECT * FROM server.db2.table2
...
How can I select data in the same query from two different databases that are on two different servers, one DB2 Server and the other a SQL Server?
On your sql server, set up a linked server to the db2 database.
Then write your query on sql server. I suggest that you use openquery for the db2 stuff. If you have to combine the data, populate a sql server temp table with the openquery results and work from there.
The reason I suggest this is performance. I have found that if you use this syntax
select somefields
from server.database.owner.table
where whatever
sql server will bring back the entire table from the linked server and apply the where clause afterwards.
You can set up a linked server http://support.microsoft.com/kb/222937
How to create a linked server
I have written SQL statements (stored in a text document) that load data into a SQL Server database. These statements need to be repeated daily. Some of the statements use the NewId() function to populate a keyed field in the database, and this works fine.
While I'm in the process of writing an application to replicate these statements, I want to use Access queries and macros instead of copying and pasting queries into SQL Server, thus saving me time on a daily basis. All is working fine but I can't find any function that will replace the SQL Server NewId() function. Does one exist or is there a work around?
I'm using SQL Server 2005 and Access 2007.
On top of matt's answer, you could simply use a pass-through query and just use your existing, working queries from MS Access.
A solution would be to insert the stguidgen() function in your code, as you can find it here: http://trigeminal.fmsinc.com/code/guids.bas https://web.archive.org/web/20190129105748/http://trigeminal.fmsinc.com/code/guids.bas
The only workaround I can think of would be to define the column in your access database of type "Replication ID" and make it an autonumber field. That will automatically generate a unique GUID for each row and you won't need to use newid() at all. In SQL server, you would just make the default value for the column "newid()".
Again, there seems to be confusion here.
If I'm understanding correctly:
You have an Access front end.
You have a SQL Server 2005 back end.
What you need is the ability to generate the GUID in the SQL Server table. So, answers taht suggest adding an AutoNumber field of type ReplicationID in Access aren't going to help, as the table isn't a Jet table, but a SQL Server table.
The SQL can certainly be executed as a passthrough query, which will hand off everything to the SQL Server for processing, but I wonder why there isn't a default value for this field in SQL Server? Can SQL Server 2005 tables not have NewId() as the default value? Or is there some other method for having a field populate with a new GUID? I seem to recall something about using GUIDs and marking them "not for replication" (I don't have access to a SQL Server right at the moment to look this up).
Seems to me it's better to let the database engine do this kind of thing, rather than executing a function in your SQL to do it, but perhaps someone can enlighten me on why I'm wrong on that.