Is there an advantage to using OPENQUERY when syntax doesn't require it? - sql-server-2005

Assume a query that contains something like
set #x = max([column]) from [table]
insert into [table]
select * from [linkedserver].[database].dbo.[table] where [column]>#x
Is there an advantage to using OPENQUERY here instead of the 'direct' approach?
I'm running this on SQL Server 2016, data is being migrated from the linked server which is an old SQL Server 2005.
It works, I'm just wondering if it would be better to use OPENQUERY or not.

Related

Save results of a Access query to SQL Server

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

Simple SQL query that works for Oracle and SQL Server

I am looking for a small and simple query that works on Oracle and SQL Server.
It is used as a test query to check the connection.
For SQL Server we used SELECT 1, but in Oracle it would have to be SELECT 1 FROM DUAL.
What we plan to use now is SELECT COUNT(*) FROM (sometable) but any ideas for an even simpler query are appreciated.
One simple option is to add a view to SQL Server called DUAL that just returns 1, that way you can have a simple query that works the same in both environments:
SELECT 1 FROM DUAL
If returning data from relations isn't important then:
SELECT 'Hello world';
will rely on a connection as much as anything else. Your RDBMS should return 'Hello world'. Tested on SQL server and PostgreSQL (don't have access to Oracle).
As long as you write query in ANSI standard. It can be executed in all the RDMS.
May be you can try this query....
select ColumnName from TableName where 1=2
BTW, the DBProvider shld have come property to state of DB connectivity... which DB provider does ur application uses?
Does it need to be a query? You could create a simple stored procedure with the same name in both databases, that just returns a constant, and execute it from the application server.

Use a specific database and table in MSSQL (Visual Studio)

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 servers and databases from SQL Server Management Studio?

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

Determine SQL Server version of linked server

Does anyone here know how i can determine the version of SQL running on my linked server through use of TSQL statements?
I am running SQL2005 my linked servers are running a mix of sql2000, 2005 and 2008.
select * from openquery(MyLinkedServer,'SELECT SERVERPROPERTY(''productversion'')')
Works
One minor nitpick about OPENQUERY is that one cannot use anything other than string literals for both the server and the query.
With EXEC AT you can at least use varchar variables for the query (although it can be a pain to quote the stuff correctly) although not for the server-name:
declare #sql AS varchar(max) = 'SELECT SERVERPROPERTY(''productversion'')'
EXEC(#sql) AT MyLinkedServer
I assume this is just a parser limitation rather than some deliberate limitation in the design.
You can access ##version through a linked server using OPENQUERY
SET #sql = 'SELECT * FROM OPENQUERY(['+#servername+'],''select ##VERSION'')'
SELECT ##VERSION
Returns a string detailing the version of the server.
Also you can try:
exec master..xp_msver