Can I create a view that will query a table from another sql server on another server but same domain - sql

I need to query a table from another SQL Server on a different server but same domain, but I am not sure how I will be able to do it.
I tried solution given in this answer Can I create view in my database server from another database server but it doesn't work for me as I got SQL Server 2000 (please don't hate :-) ).
When I try solution given then i get this error,
Line 23: Incorrect syntax near '-'.
which is because command is not compatible with SQL Server 2000.
Edit
SELECT * FROM AnotherServer.AnotherServerDatabase.Server.Table1

you can link the servers and run cross server queries as long as you put the server name before the DB your running the query on.
For example
SELECT * FROM "linkedserver".dbo.aTable
(without "" marks )
bear in mind different server versions though. I run cross server queries from 2008 to 2000 servers and its a pain adapting :)

Related

For a Front end access user interface connected to a backend SQL server, do I create new queries for tables in the SQL server or Access frontend?

I am a new database intern working with a access front end and SQL server backend database. The database was custom made for the company. One of my assignments is to take scripts and apply them to make four new tables. I am aware that I need to make a new query for each new table but I don't know if I should make the query in SQL server management studio or the frontend access program. I have tried copying and pasting the given scripts into a new query in access but I get an error message "invalid SQL statement expected 'DELETE', 'INSERT'...". I decided to try to break done the program a little bit and tested the first line
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id =OBJECT_ID(N'[dbo] .[FK_tblInstrumentInterfaceLog_tlkpInstrument]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblInstrumentInterfaceLog]'))
but the same error message keeps popping up. I even tried just SELECT * FROM sys.foreign_keys, and I got the error message "could not find file...". I am very much a beginner and any guidance would be appreciated.Basically am I supposed to be applying these scripts the server SQL database or on the front end access program?
Are you using a pass-through query? i.e. not just a select query. Access needs to know where to send the query and since you are using TSQL not Access SQL this needs to be executed on the server.
Normally when you query a linked table the information of how to get the data (the connection string) is tied to the table. But for this kind of query you'll probably need to tell Access explicitly. Unless you are using an ADP/ADE, then the connection info travels with the program not the table.
As a general rule, you use SQL management studio (SSMS) to create and run those scripts. So the general accepted approach here is such scripts will not be placed in the front end. As noted such scripts if for some reason must be placed in the front end, then you have to create them as pass-though, but EVEN in this case you want to use the SSMS to create such quires.
So the answer here is you create the new scripts and make table queries in the back end, or in this case using the SQL server management studio.
The syntax checking, query editor etc. in recent versions of SSMS now has auto-complete etc. and you can test/write/update those scripts in SQL server. Once you have such a query or even several of them, then the resulting “several” statements can be pasted into a front end query that been created as pass-though. If you do not use a pass-though query, then you are creating and using and assuming client side SQL (JET (now called ACE)).
The client side has it own version of SQL syntax, and it is NOT 100% compatible with the SERVER SIDE. If you writing SQL in the client that is NOT pass though, then you using a linked table to SQL server. These linked tables thus will use local (JET/ACE) based SQL queries. The ODBC driver thus translates this SQL into server side compatible syntax. However the JET/ACE sql syntax is very limited when compared to SQL server and no server side commands exist in this SQL syntax for the client data engine (JET/ACE)
So for many quires, you will and can simply build such queries using the Access query builder.
However for SQL that needs to run 100% server side then such quires has to be setup as pass-though and are in most cased built + tested using SSMS.

SQL Server: Query not linked server providing credencials. Is possible?

I tried to query another specific server many times, but i failed... I searched and i found out that the server must be linked with the other server in order to achieve what i want. Unfortunately is not in my hands to change that so my question is, if it is possible to query the other server by providing credentials.
The server i want to query has SQL Server 2005 Service Pack 4 (9.00.5000) and the server i'm working on has SQL Server 2005 Service Pack 3(9.00.4035).
Thanks in advance.
Use OPENROWSET or OPENDATASOURCE instead. Both allow you to pass credentials. This is your best option if you are unable to use a linked server.
This example works if you are using SQL Server logins.
SELECT a.*
FROM OPENROWSET('SQLNCLI', 'SERVER=<servername>;UID=<username>;PWD=<password>',
'SELECT FOO FROM FOO.BAR') AS a

How to select a table from one machine to another using sql server 2008 r2?

I have explained the scenario below:
I am having two servers:
server 1
server 2
In both the servers I have Sql Server 2008 r2.
In server 1, I have a database named "DB_Server1" and in server 2, a database named "DB_Server2".
In DB_server1 database, I have a table named "TB_Server1" and in DB_Server2, a table named "TB_Server2".
My requirement is, in DB_Server1, I'm going to write a stored procedure which selects the table "TB_Server2" from DB_Server2 which is located in the server 2.
How can I achieve the above requirement?
you can use linkserver
at first select Server1
step 1: exec sp_addlinkedserver 'Server2'
step 2: select * from [Server2].[DB_Server2].TB_Server2
You can create linked server (http://msdn.microsoft.com/library/ff772782.aspx)
Use Linked Server:
Allows you to query databases and tables on remote servers as though they are part of the local database. Very easy to setup (just call exec sp_addlinkedserver) and once defined uses nothing but plain old SQL.
Here is a simple tutorial about how to create a linked server. After creating linked server, we can query it as follows:
select * from LinkedServerName.DatabaseName.dbo.TableName
Click here for another tutorial.
Read more about Linked Servers here.

Static Reference (function?) to a db in SQL 2005 & 2008

We are in desperate need to having some way to reference a static table in SQL. I'm assuming this maybe possible using a function but i'm not sure how to accomplish this.
For example I want to ...
-on SQL Server A (2005)
SELECT * FROM staticDB.someTbl
-on SQL Server B (2008)
SELECT * FROM staticDB.someTbl
then on each server have the real "staticDB" reference someplace
Hopefully this makes sense!
Thanks
EDIT
So when I have a stored procedure on our dev server it points to the dev db (via the function/var). Then on the prd server it points to prd db. Instead of having to remember to replace them every time. We can have one reference to this function/var and it would just work across both servers.
Something like a connection string equivalent, but for SQL.

How to retrieve records from server database into local database

I have a little problem. I want to create a query in my local database (tijdsregistratie.mdf) to retrieve rows from my server database (IT Solutions Develop.dbo) on server itshou-dev03\sql2008.
But I don't know how to connect to the server database. I tried it like this :
select TOP 10 * from [IT Solutions Develop].dbo.[IT Solutions BVBA$Planning]
.. but it gives me this error :
Invalid object name 'IT Solutions Develop.dbo.IT Solutions
BVBA$Planning'.
One way is to link the servers:
http://msdn.microsoft.com/en-us/library/ms188279.aspx?ppud=4
You can also define linked servers by using SQL Server Management
Studio. In the Object Explorer, right-click Server Objects, select
New, and select Linked Server. You can delete a linked server
definition by right-clicking the linked server name and selecting
Delete.
This is the process by which you tell SQL Server where another server is and how to connect to it. You can do this in SQL Server Management Studio or in T-SQL. You can then refer to the linked server by a four part name (similar to what is in your question):
[LinkedServerName].[Database].[Schema].[Object]