COALESCE: SQL Server vs Oracle - sql-server

I have following script:
SELECT 1
FROM Table t
WHERE COALESCE(NULL, t.ID) = NULL;
t is empty. The query returns 1 for Oracle and it returns nothing for SQL Server.
What is an output of COALESCE operation for SQL Server? Can we fix this code to behave for both DB in the same way?

What's the point of having colaesce here as your first argument is NULL.
Just do this:
SELECT 1
FROM Table t
where t.ID IS NULL;

The problem is not the Coalesce function. If the t table is empty then no rows will be found and returned by SQL Server.

Related

How to Use OpenQuery to do Create Alias (IBM DB2) in SQL Server

I use linked server to connect AS400 DB2.
For example: select query can work
select *
from openquery([DB2], 'select t1.* from lib.table01 t1
fetch first 1 rows only')
But I want to use query
Create Alias Library.T_temp For Library.T1 (MemberName)
in SQL Server.
It returned an error because it have no return rows.
As following (it will return error):
Select * from OpenQuery([DB2],' Create Alias...')
Update OpenQuery([DB2],' Create Alias...')
Is there any method to do that?
Thanks
Don't try..
Your openquery() is the preferred solution.
By using openquery(), the SQL statement is passed to Db2 and run there. Since you've included a fetch first 1 rows only only 1 row is returned.
the query form
select TOP 1 t1.*
from db2.myibmi.lib.table01 t1
offset 0 rows
first first 1 row only
Will actually pull back all rows to SQL Server, then filter them on the SQL Server.
(At least I know that's how it used when a WHERE clause was included. I assume TOP isn't any better)

Convert Oracle SQL query to Azure SQL query

I have a pretty long Oracle SQL query that needs to be compatible for Azure SQL. I am new to both database types. Here is query:
MERGE INTO studies
USING dual
ON (study_id = :study_id)
WHEN NOT MATCHED THEN
INSERT (study_id, study_date)
VALUES (:study_id, :study_date)
I am not sure USING dual would work. I read some solution saying that USING dual is not necessary in SQL Server.
I really appreciate if you can explain what this query means and how I can translate this for Azure SQL query.
This Oracle merge query has just a WHEN NOT MATCHED clause and no WHEN MATCHED, so basically that's insert and not exists:
insert into studies(study_id, study_date)
select x.*
from (values(#study_id, #study_date)) as x(study_id, study_date)
where not exists (select 1 from studies s1 where s1.study_id = x.study_id)
This is logically equivalent to the original Oracle query.
As for your original question: SQL Server does supports its own flavor or merge statement, whose syntax is different than Oracle. You would rewrite the Oracle merge as:
merge studies as s
using (values(#study_id, #study_date)) as x(study_id, study_date)
on (s.study_id = x.study_id)
when not matched
then insert (study_id, study_date) values(x.study_id, x.study_date)

IF condition in where clause in SQL Server

I need to write the if condition in the where clause of SQL where I wanted to define that if one column is null then search in another column in a row. I have used union but it's making the query slow to execute, so help me write this statement in the proper way.
This is the code I have right now:
SELECT *
FROM ACCOUNT
WHERE (IF ACCOUNTID IS NULL THEN REF_ACC_ID = 12 ELSE ACCOUNTID = 12)
you can use isnull built in function in sql server
like below link
w3schools
you can write this code for solve your problem
SELECT *
FROM ACCOUNT
WHERE isnull(ACCOUNTID ,REF_ACC_ID ) = 12

SQL Server 2008 R2: Query MS Access from SQL Server

Want to retrieve records from Access through linked server in SQL Server and need to convert/cast the column with VARCHAR for some constraint.
My attempt:
SELECT Cola, Colb
FROM MSAccess_LinkedServer...TableName
WHERE CAST([Cola] AS VARCHAR) = 'A123'
Unable to get the result from above query.
But when I remove CAST then I will get the result, but I want to know how to put the cast/convert.
No casting should be needed for text. How about simply:
WHERE [Cola] = 'A123'

Query fails column_name is null when column is a char

I'm using hibernate with sql_server, but am finding problems with the following:
select col_code
from table
where col_code is null
Query fails: sql error 0, SQLState S1093 The column col_code is not valid
col_code is a char(5), and reads okay from other database browsers. Its just from hibernate - Any Ideas?
I got the same error when I was doing an native SQL query from Java EE/Hibernate. The SQL statement that I passed to persistence was "SELECT Count (1) From Table" and I made the mistake of including a mapping class as a parameter. COUNT(1) returns an integer, so it doesn't need to be mapped. Hope that helps.
I would check that you're mapping is right.

Resources