ColdFusion 2016 and stored proc throwing invalid character error - db2

I am trying existing code in a CF 2016 install... I get this error
"[Macromedia][DB2 JDBC Driver][DB2]ILLEGAL SYMBOL =; VALID SYMBOLS ARE ..."
the line identified is a param of a stored proc call that looks like this:
<cfstoredproc datasource="#application.dsn#" procedure="LIVE.STOPS">
<cfprocparam type="In" cfsqltype="CF_SQL_BIGINT" dbvarname="STOPID" value="#val( variables.procstopid )#" null="no">
<cfprocparam type="In" cfsqltype="CF_SQL_INTEGER" dbvarname="TRIPID" value="#val( url.tripId )#" null="no">
</cfstoredproc>
I cannot find any mention on line of a change in stored proc tag - maybe the DB2 driver? I'm looking for any input. Thanks.
Other info;
Windows10, Apache2.4, connectiong to DB2 v10.
#pendo, Here is the stored proc - it should be noted that I abbreviated some of the sql, but the SP works and has for a long time in the app running CF10.
CREATE OR REPLACE PROCEDURE LIVE.STOP(
IN stopId BIGINT DEFAULT 0,
IN tripId INTEGER DEFAULT 0
) LANGUAGE SQL
BEGIN
DECLARE updateTripId INTEGER DEFAULT 0;
DECLARE minStopId BIGINT DEFAULT 0;
DECLARE maxStopId BIGINT DEFAULT 0;
DECLARE TripSearch_cursor CURSOR FOR
SELECT s1.fkTripsId
FROM live.paymentsTripsStops s1
JOIN live.Trips t ON s1.fkTripsId = t.Id
WHERE s1.fkStopsId = stopId
FETCH FIRST 1 ROWS ONLY;
DECLARE minMaxStop_cursor CURSOR FOR
SELECT
COALESCE(
(
SELECT s.Id
FROM live.Stops s
JOIN live.Trips t ON s.fkTripsId = t.Id
ORDER BY s.Sequence
FETCH FIRST 1 ROWS ONLY
),
0
) AS firstStopId,
COALESCE(
(
SELECT s.Id
FROM live.Stops s
JOIN live.Trips t ON s.fkTripsId = t.Id
ORDER BY s.Sequence DESC
FETCH FIRST 1 ROWS ONLY
),
0
) AS lastStopId
FROM live.Trips t
WHERE t.Id = updateTripId
FETCH FIRST 1 ROWS ONLY;
IF TripId > 0
THEN SET updateTripId = TripId;
ELSE OPEN TripSearch_cursor;
FETCH FROM TripSearch_cursor INTO updateTripId;
CLOSE TripSearch_cursor;
END IF;
IF updateTripId > 0
THEN OPEN minMaxStop_cursor;
FETCH FROM minMaxStop_cursor INTO minStopId, maxStopId;
CLOSE minMaxStop_cursor;
UPDATE live.Trips
SET fkFirstStopId = minStopId,
fkLastStopId = maxStopId
WHERE intId = updateTripId;
END IF;
END

Related

Merge - when matched then do nothing

I need to write a MERGE statement to insert data WHEN NOT MATCHED condition, WHEN MATCHED I'd like the query to do nothing but I've got to include this condition because I badly need to catch the source data from both conditions into my output table.
Here's my code:
MERGE dm_data_bps.dbo.akcja AS target
USING (
SELECT *
FROM #CEIDG
WHERE isnull(sp_id, '') <> ''
) AS source
ON target.ak_id = source.ceidg_ak_id
WHEN NOT MATCHED
THEN
INSERT (
ak_akt_id
,ak_sp_id
,ak_kolejnosc
,ak_interwal
,ak_zakonczono
,ak_pr_id
,ak_publiczna
)
VALUES (
1246
,sp_id
,0
,0
,getdate()
,5
,1
)
WHEN MATCHED
THEN
UPDATE
<DO NOTHING>
OUTPUT inserted.ak_id
,source.Firma
,source.AdresPocztyElektronicznej
,source.AdresStronyInternetowej
,source.IdentyfikatorWpisu
,source.DataRozpoczeciaWykonywaniaDzialalnosciGospodarczej
,source.DataZawieszeniaWykonywaniaDzialalnosciGospodarczej
,source.DataWznowieniaWykonywaniaDzialalnosciGospodarczej
,source.DataZaprzestaniaWykonywaniaDzialalnosciGospodarczej
,source.DataWykresleniaWpisuZRejestru
,source.MalzenskaWspolnoscMajatkowa
,source.SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaNIP
,source.SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaREGON
,source.Zakazy
,source.InformacjeDotyczaceUpadlosciPostepowaniaNaprawczego
,source.Sukcesja
,source.AdresGlownegoMiejscaWykonywaniaDzialalnosci
,source.AdresyDodatkowychMiejscWykonywaniaDzialalnosci
,source.AdresyDodatkowychMiejscWykonywaniaDzialalnosci2
,source.AdresDoDoreczen
,source.STATUS
INTO #ceidg_ak_id;
How can I accomplish my goal?
I'm not sure I'd bother with all of the overhead that comes with a MERGE statement. See Use Caution with SQL Server's MERGE Statement.
You can get everything you need with an explicit transaction.
BEGIN TRANSACTION;
UPDATE
target
SET
ak_akt_id = 1246
,ak_sp_id = sp_id
,ak_kolejnosc = 0
,ak_interwal = 0
,ak_zakonczono = GETDATE()
,ak_pr_id = 5
,ak_publiczna = 1
FROM
dm_data_bps.dbo.akcja AS target
JOIN
(SELECT * FROM #CEIDG WHERE sp_id <> '') AS source
ON
target.ak_id = source.ceidg_ak_id;
SELECT
Firma
,AdresPocztyElektronicznej
,AdresStronyInternetowej
,IdentyfikatorWpisu
,DataRozpoczeciaWykonywaniaDzialalnosciGospodarczej
,DataZawieszeniaWykonywaniaDzialalnosciGospodarczej
,DataWznowieniaWykonywaniaDzialalnosciGospodarczej
,DataZaprzestaniaWykonywaniaDzialalnosciGospodarczej
,DataWykresleniaWpisuZRejestru
,MalzenskaWspolnoscMajatkowa
,SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaNIP
,SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaREGON
,Zakazy
,InformacjeDotyczaceUpadlosciPostepowaniaNaprawczego
,Sukcesja
,AdresGlownegoMiejscaWykonywaniaDzialalnosci
,AdresyDodatkowychMiejscWykonywaniaDzialalnosci
,AdresyDodatkowychMiejscWykonywaniaDzialalnosci2
,AdresDoDoreczen
,STATUS
INTO
#ceidg_ak_id
FROM
#CEIDG
WHERE
sp_id <> '';
COMMIT TRANSACTION;

Postgres function with dblink

Got a problem here with following function in postgresql function:
im getting following error :
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function savegamelog(text,text,integer,integer,bigint,bigint,bigint,integer,integer,integer,integer) line 15 at SQL statement
my function is as follows:
DECLARE
s text;
pDatatime integer;
pDataid bigint;
BEGIN
pDatatime = floor(extract(epoch from '["now()",]'::timestamp));
IF length(pTableName) > 0 THEN
UPDATE lastgamedata SET data= pData::bytea, gameid= pGameid, balance= pBalance ,bet= pBet, win= pWin, betline= pBetline, lines=pLines, datatime= pDatatime WHERE uid= pUserid;
END IF;
UPDATE gamestatistic SET totalin =totalin+pBet , totalout =totalout+pWin WHERE gameindex=pGameid;
SELECT dblink_connect('host=127.0.0.1
user=user
password=pass
dbname=dbname');
SELECT dblink_exec('UPDATE hall SET totalbetin = totalbetin+pBet , totalbetout =totalbetout+pWin WHERE id = (SELECT roomnumber FROM users WHERE uid = pUserid)');
INSERT INTO gamedata( sessionID, uid, gameID, key, balance, bet, win, betline, lines, datatime, type, denomination ) VALUES ( 0, pUserid, pGameid, 0, pBalance, pBet, pWin, pBetline, pLines, pDatatime, pType, pDenomination ) RETURNING dataid INTO pDataid;
INSERT INTO gamedata_storage ( dataid, data ) VALUES ( pDataid, pData::bytea );
return pDataid;
END;
i know the function is wrong, and i dont know how to fix it..
can anybody point me in the correct direction pls..
following the error message instructions, change
SELECT dblink_connect('host=127.0.0.1
user=user
password=pass
dbname=dbname');
to
PERFORM dblink_connect('host=127.0.0.1
user=user
password=pass
dbname=dbname');

DB2 Query : insert data in history table if not exists already

I have History table and transaction table.....and reference table...
If status in reference table is CLOSE then take those record verify in History table if not there insert from transaction table..... wiring query like this .... checking better one... please advice.. this query can be used for huge data ?
INSERT INTO LIB1.HIST_TBL
( SELECT R.ACCT, R.STATUS, R.DATE FROM
LIB2.HIST_TBL R JOIN LIB1.REF_TBL C
ON R.ACCT = C.ACCT WHERE C.STATUS = '5'
AND R.ACCT NOT IN
(SELECT ACTNO FROM LIB1.HIST_TBL)) ;
If you're on a current release of DB2 for i, take a look at the MERGE statement
MERGE INTO hist_tbl H
USING (SELECT * FROM ref_tbl R
WHERE r.status = 'S')
ON h.actno = r.actno
WHEN NOT MATCHED THEN
INSERT (actno,histcol2, histcol3) VALUES (r.actno,r.refcol2,r.refcol3)
--if needed
WHEN MATCHED
UPDATE SET (actno,histcol2, histcol3) = (r.actno,r.refcol2,r.refcol3)

Calling Stored Procedure on Column TSQL

Here is my situation. I know there must be a simple answer, but I am just not well versed in TSQL to know how. Below I have the main query of a stored procedure that selects the data I need. I have it working so far except that I need to call a seperate stored procedure called GetRecordMediaById where I feed it the Id from the PhotoId column, and it selects the BLOB data from the appropriate database which then needs to be its own column in the final query or replace the original PhotoId column.
I have no clue how to go about this. I've tried implementing temp tables, but I could never even get it to execute.
Here is my code:
ALTER PROCEDURE [dbo].[GetRollCallData]
#Ids VARCHAR(255),
#LexiconId INT,
#UUID UNIQUEIDENTIFIER,
#ReadOnly INT
AS
DECLARE #TableCode INT
SET #TableCode = 58
EXEC InsertInSelectionCache #Ids, #UUID, #TableCode, 0
WITH DOACTE AS(
SELECT ROW_NUMBER() OVER(PARTITION BY [File].Id ORDER BY CustomRecordsetId DESC) AS RowNumber, [File].*, FileType2Lexicon.Label as FileTypeLabel, [People].DefaultPhone, [People].InvertedName, CustomFieldValue.Value as DateofArrest
FROM FileType2Lexicon, SelectionCache, [People], [File]
INNER JOIN [CustomRecordSet]
ON [CustomRecordset].RecordId = [File].Id
INNER JOIN CustomFieldValue
ON [CustomRecordset].Id = CustomFieldValue.CustomRecordsetId
INNER JOIN [CustomField2Lexicon]
ON CustomField2Lexicon.CustomFieldId = CustomFieldValue.CustomFieldId
WHERE [File].Id = SelectionCache.RecordId
AND SelectionCache.UUID = #UUID
AND SelectionCache.TableCode = #TableCode -- this is the code for File table
AND [File].Id <> 0
AND [File].FileTypeId = FileType2Lexicon.FileTypeId
AND FileType2Lexicon.LexiconId = #LexiconId
AND [File].ClientIdString = [People].ClientIdString
AND CustomFieldValue.Value <> ''
AND CustomField2Lexicon.Label = 'Date of Arrest'),
PHOTOCTE AS(
SELECT [File].Id, CustomFieldValue.Value as PhotoId
FROM FileType2Lexicon, SelectionCache, [People], [File]
INNER JOIN [CustomRecordSet]
ON [CustomRecordset].RecordId = [File].Id
INNER JOIN CustomFieldValue
ON [CustomRecordset].Id = CustomFieldValue.CustomRecordsetId
INNER JOIN [CustomField2Lexicon]
ON CustomField2Lexicon.CustomFieldId = CustomFieldValue.CustomFieldId
WHERE [File].Id = SelectionCache.RecordId
AND SelectionCache.UUID = #UUID
AND SelectionCache.TableCode = #TableCode -- this is the code for File table
AND [File].Id <> 0
AND [File].FileTypeId = FileType2Lexicon.FileTypeId
AND FileType2Lexicon.LexiconId = #LexiconId
AND [File].ClientIdString = [People].ClientIdString
AND CustomFieldValue.Value <> ''
AND CustomField2Lexicon.Label = 'Booking Photo')
SELECT DOACTE.*, PHOTOCTE.PhotoId
FROM DOACTE
INNER JOIN
PHOTOCTE
ON DOACTE.Id = PHOTOCTE.Id
WHERE DOACTE.RowNumber = 1
EDIT:
Solution for me was to create a scalar function that resolves the Id in the BLOB database and returns the BLOB data.
SELECT DOACTE.*, dbo.GetImagebyId(PHOTOCTE.PhotoId) as Photo,
FROM DOACTE
INNER JOIN
PHOTOCTE
ON DOACTE.Id = PhotoCTE.Id
WHERE DOACTE.RowNumber = 1
You can declare a #table_variable and insert the results from "EXEC InsertInSelectionCache #Ids, #UUID, #TableCode, 0" into the table variable.
Then you can join to the #table_variable in the final query.
See here for examples: How to return temporary table from stored procedure

oracle sequence question

There are two inserts in my trigger which is fired by an update. My Vendor_Hist table has a field called thID which is the primary key in Task_History table. thID gets its' value from mySeq.nextval.
INSERT INTO TASK_HISTORY
( thID, phId, LABOR, VERSION )
( select mySeq.NEXTVAL, mySeq2.CurrVal, LABOR, tmpVersion
from tasks t
where t.project_id = :new.project_ID );
select mySeq.currval into tmpTHID from dual; -- problem here!
INSERT INTO VENDOR_HIST
( vhID, thID, Amount, Position, version )
( select mySeq3.NEXTVAL, tmpTHID,
Amount, Position, tmpVersion
from vendors v2, tasks t2
where v2.myID = t2.myID
and t2.project_id = :new.project_ID );
Now, my problem is the tmpTHID always the latest value of mySeq.nextVal. So, if thID in task_history is 1,2,3, I get three inserts into vendor_hist table with 3,3,3. It has to be 1,2,3. I also tried
INSERT INTO TASK_HISTORY
( thID, phId, LABOR, VERSION )
( select mySeq.NEXTVAL, mySe2.CurrVal, LABOR, tmpVersion
from tasks t
where t.project_id = :new.project_ID ) returning thID into :tmpTHID;
but then I get a "warning compiled with errors" message when I execute the trigger. How do I make sure that the thID in first insert is also the same in my second insert?
Hope it makes sense.
for i in (select * from tasks t
where t.project_id = :new.project_id)
loop
insert into task_history
( thID, phId, LABOR, VERSION )
values
(mySeq.NEXTVAL, mySeq2.CurrVal, i.LABOR, i.tmpVersion);
for each j in (select * from vendors v
where i.myId = v.myId)
loop
insert into vendor_history
( vhID, thID, Amount, Position, version )
values
(mySeq3.NEXTVAL, mySeq.CURRVAL, j.Amount, j.Position, j.tmpVersion)
end loop;
end loop;
I'm assuming the columns inserted in the second insert are from the VENDORS table; if not, the referencing cursor (i or j) should be used as appropriate.
Instead of the currVal, it works with the following subselect.
( select min(thID) from task_history t3
where t3.project_id = t2.project_id
and t3.myID = t2.myID
and t3.version = tmpVersion ),