Simple SQL query that works for Oracle and SQL Server - sql

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.

Related

Common ways to find sub string in oracle and sql server

Hi I have below sql query where I am using SUBSTR in oracle, I need to modify SUBSTR so that this query should work for both oracle and sql server
Note: in oracle we have SUBSTR and in sql server we have SUBSTRING, LEFT, RIGHT but I need common query that should work in both oracle an sql server data base.
SELECT INA.LOCATION_CODE,INA.ACCT_NUM,INA.STATE_ENTRY_DATE,FP.NUM_INA_DYS_BF_PUR FROM INACTIVE_ACCOUNT INA
LEFT JOIN FUNCTIONAL_AREA FA ON INA.LOCATION_CODE=FA.LOCATION_CODE AND SUBSTR(INA.RETURN_TO_STE_CODE,0,1)=FA.FUNC_STATE_ENTR_KY
JOIN FUNC_AREA_PARAM FP ON FA.GUID=FP.PARN_GUID;
There isn't such a function. Trying to write code that works across different databases with exactly the same syntax is a fool's errand. There are too many subtle differences between most databases that it is generally not possible.
That said, you can do something in this case:
ON INA.LOCATION_CODE = FA.LOCATION_CODE AND
INA.RETURN_TO_STE_CODE LIKE CONCAT(FA.FUNC_STATE_ENTR_KY, '%')
This should work in both databases. That is because you are lucky.

Standard SQL command to get current RDBMS

Is there any standard sql command that can show you the current RDBMS and version?
The reason for this question is that I am using a CMS remotely that use a SQL database, but I don't know which RDBMS is being used, so I thought maybe there is standard SQL command to print it, something similar to SQL server's ##version
This is not a direct answer to the question, but since there is no standard sql query or function to show the RDBMS, I will post the queries/functions/vars used to identify it.
-- SQL SERVER
SELECT ##version;
-- Postgres, mysql, mariadb
SELECT version();
-- Oracle
SELECT * FROM v$version;
-- sqlite
select sqlite_version();
Please pay attention that Mysql and Sqlite will show only the version and will not include engine name in contrast to other RDBMS.

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

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
...

Select into from one sql server into another?

I want to select data from one table (T1, in DB1) in one server (Data.Old.S1) into data in another table (T2, in DB2) in another server (Data.Latest.S2). How can I do this ?
Please note the way the servers are named. The query should take care of that too. That is,
SQL server should not be confused about fully qualified table names. For example - this could confuse SQL server - Data.Old.S1.DB1.dbo.T1.
I also want "mapping" . Eg Col1 of T1 should go to Col18 of T2 etc.
Use Sql Server Management Studio's Import feature.
right click on database in the object explorer and select import
select your source database
select your target database
choose the option to 'specify custom query' and just select your data from T1, in DB1
choose your destination table in the destination database i.e. T2
execute the import
create a linked server. then use an openquery sql statement.
select * into [newtable] from [linked_server].[databasename].dbo.[tablename]
If it is just one time linked server in appropriate.
But if it neede to move data frequently ,replication is better and easier.
I think you're overcomplicating it by insisting on SQL. In SSMS, right-click the server you want to export from, select "Tasks", "Export", and let the wizard walk you through the steps of selecting your target server and table, which includes mapping all of the columns exactly as you're trying to do with your SQL situation. All the functionality you seem to be looking for is already there.
There's no need for linked servers, SSIS, or anything else to accomplish this. It's already built into SQL Server Management Studio.
Based on the accepted answer.
Make sure the source server (server1) is linked to the destination server (server2):
SELECT *
INTO Server2.DB2.dbo.T2
FROM OPENQUERY (server1
, ' SELECT col_one, col_two, ...
FROM DB1.dbo.T1
WHERE ...
...
);
You could look into this:
Clicky!
Or you could use SSIS, which would probably be much simpler.