Microsoft SQL Server and Oracle SQL Developer - sql

I linked an Oracle Database to my SQL Server in Microsoft SQL Server Management Studio 18. Server Objects -> Linked Servers.
I have a SQL Statement that when I run on the Oracle Developer Tool/Platform it returns the information as expected. But when I run the exact same query on the SQL Server it returns the incorrect results (The actual values in the rows and columns do not match).
What I know.
The table I am query in lives in the Oracle Database.
I can get the same/matching results on the Oracle Developer and SQL Server if I exclude in my WHERE statement anything involving a DATE.
Any thoughts?
The example of the query below.
Works on Oracle Developer but not on MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE('01/03/2020', 'DD/MM/YYYY') AND TO_DATE('10/12/2020','DD/MM/YYYY');
The example of the query below.
Works on both Oracle Developer and MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and BATCHID = 'ThisBAtchID';

You cannot use ORACLE specific functions like TO_DATE in SQL Server calls. You have to execute them remotely using OPENQUERY. OPENQUERY in MSDN
SELECT * FROM OPENQUERY (OracleSvr, 'SELECT * FROM TABLE1
WHERE status = ''Deviation'' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE(''01/03/2020'', ''DD/MM/YYYY'') AND TO_DATE(''10/12/2020'',''DD/MM/YYYY'');');

Related

Case sensitivity issue on SQL server

I have a Dblink in place between my Oracle database and Microsoft SQL Server, fetching data from SQL server while on Oracle. Connectivity is fine but I am getting invalid column names whenever I try to include any column name in my select query.
On Oracle with Dblink:
select COLUMN1 from table1#dblink
Fails with:
ORA-00904: COLUMN1: invalid identifier
On Oracle with Dblink, this works fine.
select "COLUMN1" from table1#dblink
On SQL server this works fine:
select column1,COLUMN1 from table1
Seems case sensitivity is not enforced when on SQLserver, however, while trying from oracle it is enforced, any idea how I can bypass this,
The collation on my SQL server is SQL_Latin1_General_CP1_CI_AS.

Understanding of difference between MariaDB and SQL Server for a query

I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.
Please try this query
Select distinct col from (
Select col from table order by col
)

Column name in SQL Server fully qualified name

I read in SQL Server documentation that I can specify column names in FROM clause of a simple SELECT query.
But when I try to run this query:
select * from my_db.dbo.test_table.test;
I get the following error:
Msg 7202, Level 11, State 2, Line 1 Could not find server 'my_db' in
sys.servers. Verify that the correct server name was specified. If
necessary, execute the stored procedure sp_addlinkedserver to add the
server to sys.servers.
I know this is a T-SQL page, and I'm trying to run .
Why does this happen?
I'm using SQL Server version 2017 via Microsoft SQL Server Management Studio version 2017 as well.
try rewriting from this
select * from my_db.dbo.test_table.test;
to this
select test_table.test,* from my_db.dbo.test_table;
the way it was written with that many Periods it assume you are trying to fully qualify the table so in what you had tried to the server is treating it as follows
my db = linked server (a server other than the Server you are working on)
dbo = Schema (which is correct)
test_table = Table (Also correct)
test = just plain erroror
the fields you want to show should directly follow the Keyword Select so if you only wanted 2 fields you could write
select test,test2 from my_db.dbo.test_table;
or simpler if you only have the one server
select test,test2 from dbo.test_table;
or if you only have the defailt Schema dbo (Database Base Owner)
select test,test2 from test_table;
I hope thaelps
You cannot specify columns in the FROM clause, but you can specify columns in the SELECT clause.
select test from my_db.dbo.test_table
Please run sp_addlinkedserver and sp_addlinkedsrvlogin SPs:
EXEC sp_addlinkedserver #server='MyLinkedServer'
EXEC sp_addlinkedsrvlogin 'MyLinkedServer', 'false', NULL, 'MyUserName', 'MyPassword'
After that, you can try to run: select * from my_db.dbo.test_table
Ref: https://blog.sqlauthority.com/2014/08/26/sql-server-fix-error-7202-could-not-find-server-in-sys-servers-verify-that-the-correct-server-name-was-specified/
Also, even though it is SQL Server 2014 related maybe helps, please see: Could not find server 'server name' in sys.servers. SQL Server 2014

How to use GROUP BY for multiple columns in SQL server

I am working on existing project which is developed on Laravel + Mysql. Now, I have migrated my database from Mysql to Sql server. So all the queries written in project using Mysql syntax.
Now, I have an query given below:
SELECT table_1.*, table_3.date as sem1date
FROM table_1, table_2, table_3
WHERE table_3.ID=table_2.DID AND block_datelu.BID=table_1.final_exam_date AND table_1.centreid=1234 AND table_1.course=1
GROUP BY table_1.start_date, table_1.end_date
When I am converting above query into sql server syntax, then sql server giving me error msg "Column 'table_1.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Can anyone tell me how can I use group by for multiple columns having joins?

CONTAINSTABLE with wildcard works different in SQL Server 2005 and SQL Server 2008?

I have two same databases one on SQL Server 2005 and one on SQL Server 2008, it have same SQL_Latin1_General_CP1_CI_AS Collation, and full text search catalogs have the same settings.
These two databases contains table with same data, NTEXT string:
"...kræve en forklaring fra miljøminister Connie Hedegaard.."
My problem is:
CONTAINSTABLE on SQL Server 2008 finds nothing if query is:
select * from ContainsTable(SearchIndex_7, Content, N'"miljø*"') ct
but SQL Server 2005 works perfectly and finds necessary record.
SQL Server 2008 finds necessary record if query is:
select * from ContainsTable(SearchIndex_7, Content, N'"milj*"') ct
or
select * from ContainsTable(SearchIndex_7, Content, N'"miljøminister"')
What can be reason for so strange behavior?
Check the default language in fulltext index column