DB2 SQL How to get the last executed SQL-Statement with GET DIAGNOSTICS? - sql

I want to call a procedure in RPG on IBM i with SQLSTATE and with a variable text.
getSQLMessage(SQLSTT: text)
The variable text should be the last executed sql statement before the procedure call.
Is there a opportunity to get it like this:
EXEC SQL GET DIAGNOSTICS CONDITION 1 :text = last executed sql statement
Or maybe someone knwos another solution for my problem?
Thanks a lot!

You can't use GET DIAGNOSTICS, but you can first get the JobLog
DSPJOBLOG OUTPUT(*OUTFILE) OUTFILE(QTEMP/ERR_LOG)
then get the last SQL Error:
Select Qmhmf,
Qmhmid,
Qmhmdt
From Qtemp.Err_Log
Where Qmhsev >= 20
And Substr(Qmhmid, 1, 3) In ('CPA' , 'CPD' , 'CPF' , 'SQL')
Order By Rrn(Err_log) Desc
Fetch First 1 Rows Only

Related

Openfire ORA-00907: Missing right parenthesis

I have the following problem retrieving messages from the OpenFire Monitoring Service plugin. I discovered that the error is due to an incorrect query in the database but I still cannot detect the error for which the query is not working correctly.
SELECT
fromjid,
fromjidresource,
tojid,
tojidresource,
sentdate,
body,
stanza,
messageid,
barejid
FROM
(
SELECT
DISTINCT ofmessagearchive.fromjid,
ofmessagearchive.fromjidresource,
ofmessagearchive.tojid,
ofmessagearchive.tojidresource,
ofmessagearchive.sentdate,
ofmessagearchive.body,
ofmessagearchive.stanza,
ofmessagearchive.messageid,
ofconparticipant.barejid
FROM
ofmessagearchive
INNER JOIN ofconparticipant ON ofmessagearchive.conversationid =
ofconparticipant.conversationid
WHERE
(
ofmessagearchive.stanza IS NOT NULL
OR ofmessagearchive.body IS NOT NULL
)
AND ofmessagearchive.messageid IS NOT NULL
AND ofmessagearchive.sentdate >= 0
AND ofmessagearchive.sentdate <= 1602748770287
AND ofconparticipant.barejid = 'usuario3#192.168.0.79'
AND (
ofmessagearchive.tojid = 'usuario4#192.168.0.79'
OR ofmessagearchive.fromjid = 'usuario3#192.168.0.79'
)
ORDER BY
ofmessagearchive.sentdate DESC
LIMIT
100
) AS part
ORDER BY
sentdate
I get an error when doing the following query
ORA-00907: missing right parenthesis
Command line error:32 Column: 9
There is no LIMIT keyword available in Oracle and if you are using Oracle 12c you can use FETCH FIRST 100 ROWS ONLY instead of it.
You cannot use AS to give alias to the sub query and it is not recognised by Oracle. So either you can remove the alias completely as you are not using it anywhere or just remove the AS and keep the alias name part only which should be fine.
Here is a good SO link about the Oracle limiting result set and you can always look into other sites available such as Oracle base or the official document as well. For 11g solution you have to use row_number

SQL Sub-query parameters from Excel

I have SQL query with a sub-query where I want to assign sub query parameters from a cell in an Excel sheet, as this query cant be represented graphically Excel keep throwing error on this:
[Microsoft][ODBC SQL Server Driver] Invalid paramter number and
[Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index
I have already tried solution as mentioned here to trick excel without success Using Parameters in SQL query with sub-query
;WITH dataset_bl as (
SELECT
--rank() over (partition by date_of_AC, ac_room order by into_theatre) as OrderNumber
--,datetime_of_AC as booking_datetime
date_of_AC
,AC_room
,specialty
,OAMRN
,substring(Subject_NAME,1,CHARINDEX(' ',Subject_NAME,1)) as 'surname'
,substring(Subject_NAME,CHARINDEX(' ',Subject_NAME,1)+2,500) as 'forename'
,convert(date,[dttm_of_birth],103) as DOB
,PRIN_SO as 'surg'
,'"'+ASST_SO_1+' '+ASST_SO_2+'"' as 'assistant_SO'
,substring(CONSULT_NAME,1,abs(CHARINDEX(',',CONSULT_NAME,1)-1)) as 'consult_surname'
,substring(CONSULT_NAME,CHARINDEX(' ',CONSULT_NAME,1)+1,500) as 'consult_first_name'
,'"'+actual_AC+'"' as AC_details
,'"'+PLANNED_PROD+'"' as booked_details
,'Carried Out' as 'TT_outcome'
, 'NULL' as 'unP_return_flag'
,OP_type
--,LOS
,case when CL_PRIORITY ='' then 'Not listed' else 'isted' end as islisted
,CLI_PRIORITY as listed_priority
,case when EM_PRIORITY like '<=%' then 'Submitted' else 'No Greensheet' end as isGreensheet
,em_priority as a, case when EM_PRIORITY like '<=%' and em_priority <> '<=72hrsCannot charge before prod'then LEFT(EM_PRIORITY, abs(charindex('-',EM_PRIORITY)-1))
when em_priority = '<=72hrsCannot diso before prod' then '<=72hrs' else 'NULL' end as em_priority
FROM sample.dbo.tb_Fnl_Sur_th4 WITH (NOLOCK)
**WHERE main_ident = 'A224' and convert(date,into_Start, 103) >= '2019-07-01' and convert(date,into_Start, 103) <= '2019-07-31'** --i am trying to get these parameters from excel cell value
)
SELECT * FROM dataset_bl
WHERE specialty like 'abc%'
or (consult_surname like '%abc%' and consult_first_name like '%def%' )
or surg in ('cde,fghi',
'jkl,'mnop,
'qrs,Tuv')
order by convert(date,date_of_procedure,103), operating_room--, into_theatre
Here's some suggestion.
First is to check the formatting of your date columns.
and build your parameter to be like
Cast(into_Start as smalldatetime) between ? and ?
I've been on that same issue and seem to find the solution for me. In fact the parameter [?] use in MSQUERY in Excel will normally work when you have a direct Query
Ex:
Select [YourTable].[Column]
From [YourTable]
Where [YourTable].[Column]= ?
The Excel parameter will prompt and trigger where the windows that ask you which cell or Data you want that parameter have to be filled with.
In the other hand if you use the a Subquery and you add a Where condition with the Parameter ? will be stuck with the parameter [Microsoft][ODBC SQL Server Driver] Invalid paramter number and [Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index
If you download the ODBC Driver for SQL Server here -->
https://www.microsoft.com/fr-fr/download/details.aspx?id=56567
Install it and make sure that the ODBC use for your MSQUERY in Excel use that connection.
ODBC Data Source to use
After this I was able to use a MSQuery in Excel using Parameter like this one
Select [RenamedTable].*
From (Select [YourTable].[Column1],
[YourTable].[Column2],
[YourTable].[Column3]
From [YourTable]) as RenamedTable
Where [RenamedTable].[Column1] between ? and ?
Hope this will work for you as for me because I had similar problem with between date Parameter

Invalid object name 'sys.dm_exec_procedure_stats' error

I am trying to obtain the maximum time consumed by a stored procedure in my DB. I obtained a sample query from here to obtain the same using sys.dm_exec_procedure_stats. The same is posted below. Whenever I try to execute this query I get the error as
Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.dm_exec_procedure_stats'.
Can you please let me know where I might probably be going wrong?
Below is the query used. No changes made.
SELECT TOP 10 d.object_id, d.database_id, OBJECT_NAME(object_id, database_id) 'proc name',
d.cached_time, d.last_execution_time, d.total_elapsed_time,
d.total_elapsed_time/d.execution_count AS [avg_elapsed_time],
d.last_elapsed_time, d.execution_count
FROM sys.dm_exec_procedure_stats AS d
ORDER BY [total_worker_time] DESC;
EDIT: Sorry for the blunder. Server is 2005.

Firebird " Column does not belong to referenced table "

I am trying to create my first procedure on firebird 2.5 by using ibexpert gui.
The procedure will return 'PROCESS_DATE' which belongs to a specific 'PROCESS_ID'. I prepared following code:
begin
OUTPUT_DATE = (select PROCESS_DATE from PROCESSES
where PROCESS_ID = INPUT_ID);
suspend;
end
input parameter : 'INPUT_ID' --> type 'INTEGER'
output parameter : 'OUTPUT_DATE' --> type 'DATE'
But when I tried to compile it returns this error:
Column does not belong to referenced table.
Dynamic SQL Error.
SQL error code = -206.
Column unknown.
INPUT_ID.
At line 9, column 48.
I do not know how to deal with this error.
I tried to find solutions on other questions also the internet but i couldn't find a basic, understandable answer for beginners. thanks for helps.
Try this:
CREATE PROCEDURE MyP (INPUT_ID INTEGER)
RETURNS (OUTPUT_DATE DATE)
AS
BEGIN
FOR
SELECT PROCESS_DATE FROM PROCESSES
WHERE PROCESS_ID = :INPUT_ID
INTO :OUTPUT_DATE
DO
SUSPEND;
END
Always prepend parameter names with ":". The only place where ":" is not allowed is at the left side of "=" operator.

Logical operator sql query going weirdly wrong

I am trying to fetch results from my sqlite database by providing a date range.
I have been able to fetch results by providing 3 filters
1. Name (textfield1)
2. From (date)(textfield2)
3. To (date)(textfield3)
I am inserting these field values taken from form into a table temp using following code
Statement statement6 = db.createStatement("INSERT INTO Temp(date,amount_bill,narration) select date,amount,narration from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) <= substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) AND substr(date,7)||substr(date,4,2)||substr(date,1,2) >= substr (\'"+TextField2.getText()+"\',7)||substr (\'"+TextField2.getText()+"\',4,2)||substr (\'"+TextField2.getText()+"\',1,2) ");
statement6.prepare();
statement6.execute();
statement6.close();
Now if i enter the following input in my form for the above filters
1.Ricky
2.01/02/2012
3.28/02/2012
It fetches date between these date ranges perfectly.
But now i want to insert values that are below and above these 2 date ranges provided.
I have tried using this code.But it doesnt show up any result.I simply cant figure where the error is
The below code is to find entries having date lesser than 01/02/2012 and greater than 28/02/2012.
Statement statementVII = db.createStatement("INSERT INTO Temp5(date,amount_rec,narration) select date,amount,narration from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField2.getText()+"\',7)||substr (\'"+TextField2.getText()+"\',4,2)||substr (\'"+TextField2.getText()+"\',1,2) AND substr(date,7)||substr(date,4,2)||substr(date,1,2) > substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2)");
statementVII.prepare();
statementVII.execute();
statementVII.close();
Anyone sound on this,please guide.Thanks.
you need to use an Or clause together with brackets:
WHERE name='....' AND (yourDateField<yourLowerDate OR yourDateField>yourHigherDate)