If-Else condition in H2 database query - sql-server

I have a connector application which reads from third-party database using apache camel. I am trying to mock the same database and query operations using H2 database, so that connectivity to the thirdparty database is not required for testing.
The database is in MS SQL Server. I am using the Mode as SQLServer in my H2 database. However, I have a mssql query with if-else condition. When I try to execute it, I am getting the following error.
MSSQL query is
if((SELECT count(*) from Employee where DateCreated < '2016-02-02 00:05:00')>1)
SELECT TOP 10 * from Employee where DateCreated < '2016-02-02 00:05:00'
else
SELECT TOP 1 * from Employee where DateCreated < '2016-02-02 00:05:00'
Error Message :
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement " IF[*]((SELECT COUNT(*) FROM EMPLOYEE)>1)
SELECT TOP 10 * FROM EMPLOYEE
ELSE
SELECT TOP 1 * FROM EMPLOYEE "; expected "INSERT, {"; SQL statement:
if((SELECT count(*) from Employee)>1)
SELECT TOP 10 * from Employee
else
SELECT TOP 1 * from Employee [42001-185]
I am assuming that the conditional query is not supported in the H2 database. IS there anyway, I can execute this same query in H2?
Worst Case, is there any otherway, in which I can change the mssql query which is compatible with H2 ?

I don't think there is. You can use the case command in H2 for the if/else effect, and in your application could factor the code that goes into the conditions and/or statements to execute so that you write it and use it in both your ms sql and h2 requests.

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)

How to monitor and alert a table row value in SQL Server for a maximum value

I have two rows in one table. One has the start value of an ID#1 with 100000 and the other with an ID#2 1000000
Is there any possibility to monitor and trigger an alert on Microsoft SQL Server if the values were reaching for ID#1 999999 or for ID#2 9999999?
The ID get updated by an external code. Here I need to monitor the records and notify once condition occurred. I wonder if it is possible to achieve the task from database server side?
I already searched in MS SQL MMS and on the MS SQL Documentation but couldn't find a solution.
Set up a recurring SQL job in the SQL Job Agent that checks for the existence of a record and sends you an e-mail if it's there.
IF EXISTS (SELECT 1
FROM tablea
WHERE id >= 999999)
EXEC Sp_send_dbmail; -- this needs to be expanded of course
IF EXISTS (SELECT 1
FROM tableb
WHERE id >= 999999)
EXEC Sp_send_dbmail;

IBM i (AS400) to SQL Server table join syntax

I am using Excel Connection to query customer contracts from DB2 for IBM i (AS400) through SQL Server connection and trying to join a SQL Server table to determine contract expiration date and sales team responsibility.
The AS400 query operates but I continue to receive an error on joining the SQL Server table ACCOUNT.dbo.CUSTOMER but can't find reference to alternate syntax on the join.
[select *
from openquery(
bpcsrpt_new,'
select s.SCID, s.SVER, s.CONTEXP, a.ACCTNAME, a.SALESTEAM
from AS400table1.contract c, AS400table1.subcontract s, ACCOUNT.dbo.CUSTOMER a
where c.cid=''Active''
and c.cid=s.scid
and c.cver=s.sver
and c.cid=a.acid')]
That's not going to work. When you use openquery, the statement gets sent to the remote machine. Obviously, ACCOUNT.dbo.CUSTOMER is not on the remote IBM i (aka AS400) machine.
You could use 4 part naming in the query directly
select s.SCID, s.SVER, s.CONTEXP, a.ACCTNAME, a.SALESTEAM
from IBMILNKNAM.IBMIDBNAM.IBMILIBNAM.contract c
, IBMILNKNAM.IBMIDBNAM.IBMILIBNAM.subcontract s
, ACCOUNT.dbo.CUSTOMER a
where c.cid='Active'
and c.cid=s.scid
and c.cver=s.sver
and c.cid=a.acid
Note however, the SQL Server will pull back the complete contract and subcontract tables to do the join locally.
Openquery is a better option if you're only interested in a few rows of a large table on the IBM i. If I recall correctly, something like so: (not tested)
select *
from (select * from Openquery(IBMIKNKNAM, 'select s.SCID, s.SVER, s.CONTEXP
from contract c
join subcontract s
on c.cid=s.scid
and c.cver=s.sver
where c.cid=''Active'')) as rmt
join ACCOUNT.dbo.CUSTOMER a on a.acid = rmt.cid

Why are these DMVs returning rows for all databases except one?

I am trying to query the DMVs in SQL Server 2008 R2.
On this server are two user databases called histrx and OpenLink. To prove I have their names correct:
select db_id('histrx') -- Returns 5
select db_id('OpenLink') -- Returns 7
If I run the following query, picking out entries for the histrx database, I get 25 rows in the result set:
select top 25
total_worker_time/execution_count as avg_worker_time,
total_logical_reads/execution_count as avg_logical_reads,
db_name(s.dbid) as [db_name],
object_name(s.objectid, s.dbid) as [object_name],
execution_count,
plan_generation_num,
last_execution_time,
creation_time,
[text],
p.query_plan
from
sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) s
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) p
where
db_name(s.dbid) = 'histrx'
order by
avg_logical_reads desc
If I then change the where clause to the following, no rows are returned:
where
db_name(s.dbid) = 'OpenLink'
I know that there is a significant amount of activity on the OpenLink database. If I look at Recent Expensive Queries in the Activity Monitor, I can see entries for OpenLink, and I'm pretty sure this is using the DMVs underneath.
I'm running the Activity Monitor and the DMV query under the same
login
That login is the owner of the OpenLink database
If I run select * from fn_my_permissions (NULL, 'server'); then I can see I have VIEW SERVER STATE permissions
If I remove the where clause, I see entries for other databases such as msdb and distribution
Here is a screenshot of the mappings for my login. I'm pretty sure I shouldn't be the owner, but that's a different question.
Can anyone tell me why my DMV query is returning zero rows for this database?
Quote from Books Online 2008 R2 > sys.dm_exec_sql_text:
dbid smallint ID of database. Is NULL for ad hoc and prepared SQL
statements.
1) So, for this type of SQL statements, where db_name(s.dbid) = 'OpenLink' means where NULL = 'OpenLink' and this predicate is evaluated to UNKNOWN.
2) dbid column is NOT NULL for "non-adhoc and non-prepared SQL" statements (ex. for stored procedures).
3) For SQL 2008R2 you might get the dbid for "ad hoc and prepared SQL statements" from sys.dm_exec_plan_attributes ( plan_handle ) function (link) using this query:
SELECT
...
ISNULL(src.dbid,CONVERT(SMALLINT,att.value)) AS my_dbid,
DB_NAME(ISNULL(src.dbid,CONVERT(SMALLINT,att.value))) my_dbname,
...
FROM
sys.dm_exec_query_stats qs
...
CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) att
WHERE att.attribute='dbid'

Update statement to Oracle failing in SSIS Execute SQL Task

I have to execute some Update statements from SSIS to Oracle which I cannot put into a stored Proc. This statement runs fine in Oracle, but I get error when executing from SSIS. I am using an Execute SQL Task with properties SQL Source Type = Direct Input, BypassPrepare = True. On executing the task, it just hangs for 20 minutes or so. Then I clicked on stop debugging.
UPDATE Table1 R
SET R.Column1 =
(SELECT SUM (Column2)
FROM Table2 M
WHERE
M.Column3 IS NULL AND M.Column4 = R.Column4)
WHERE EXISTS ( SELECT Column4 AS Column4
FROM Table2 M
WHERE
M.Column3 IS NULL AND M.Column4 = R.Column4
GROUP BY Column4) `
When this happened to me, it was because I had left a transaction open. I had been using SQL Developer to reset column values in the target table during testing. (SQL Developer doesn't use implicit transactions by default.)
Here's the detail:
SSIS PL/SQL task hangs with message “Multiple-step OLE DB operation generated errors.”

Resources