LEFT JOIN Condition Error - database

I created this stored procedure in DB2 using Toad For DB2.
CREATE OR REPLACE PROCEDURE TEST_PROC
BEGIN
DECLARE GLOBAL TEMPORARY TABLE TEMP_TABLE_1(ID INT GENERATED ALWAYS AS IDENTITY,col001 CHAR(36)) NOT LOGGED WITH REPLACE;
DECLARE GLOBAL TEMPORARY TABLE TEMP_TABLE_2(ID INT GENERATED ALWAYS AS IDENTITY,col001 CHAR(36)) NOT LOGGED WITH REPLACE;
DECLARE GLOBAL TEMPORARY TABLE TEMP_TABLE_3(ID INT GENERATED ALWAYS AS IDENTITY,col001 CHAR(36)) NOT LOGGED WITH REPLACE;
INSERT INTO SESSION.TEMP_TABLE_1(col001) VALUES ('TABLE_1_ROW_1');
INSERT INTO SESSION.TEMP_TABLE_1(col001) VALUES ('TABLE_1_ROW_2');
INSERT INTO SESSION.TEMP_TABLE_1(col001) VALUES ('TABLE_1_ROW_3');
INSERT INTO SESSION.TEMP_TABLE_1(col001) VALUES ('TABLE_1_ROW_4');
INSERT INTO SESSION.TEMP_TABLE_2(col001) VALUES ('TABLE_2_ROW_1');
INSERT INTO SESSION.TEMP_TABLE_2(col001) VALUES ('TABLE_2_ROW_2');
INSERT INTO SESSION.TEMP_TABLE_2(col001) VALUES ('TABLE_2_ROW_3');
INSERT INTO SESSION.TEMP_TABLE_3(col001) VALUES ('TABLE_3_ROW_1');
BEGIN
DECLARE temp_cursor CURSOR WITH RETURN TO CLIENT FOR
SELECT tmp.* FROM(
SELECT id, col001 from SESSION.TEMP_TABLE_1
)tmp
Left join SESSION.TEMP_TABLE_2 tmp2 ON tmp2.id = tmp.id
AND EXISTS (SELECT 1 FROM SESSION.TEMP_TABLE_3 t3 WHERE t3.ID = 2);
OPEN temp_cursor;
END;
END;
But executing this stored procedure Call TEST_PROC() generates error.
DB2 Database Error: ERROR [56098] [IBM][DB2/NT64] SQL0727N An error occurred during implicit system action type "5". Information returned for the error includes SQLCODE "-338", SQLSTATE "42972" and message tokens "". SQLSTATE=56098
It does not give any error, if I
Change Left Join to INNER JOIN
Remove this condition AND EXISTS (SELECT 1 FROM SESSION.TEMP_TABLE_3 t3 WHERE t3.ID = 2)
but these two are required.
Any idea what am i doing wrong?

Related

Invalid Object Name for Temp Table in Stored Procedure

I'm trying to use a temp table to update a database table in a stored procedure.
My client was throwing an error about an illegal null value, so I tried testing within SSMS and it told me that the name of the temp table was invalid (regardless of what I named it).
If I run the beginning and change the INSERT INTO SERVICE SELECT S.* to simply SELECT S.* and run the code after Declaring and Defining #OldServicesString (so I can leave out the ALTER/CREATE PROCEDURE line) it runs exactly as expected.
Here's the beginning of the SP:
ALTER PROCEDURE [dbo].[app_CreateNewServicesOldFSD] (#OldServicesStr nvarchar(max)) AS
SET NOCOUNT ON
DECLARE #User char(10) = (SELECT TOP 1 CREATED_BY_USER FROM BATCHLOG WHERE BPROCESS_ID = 3 ORDER BY ID DESC);
DECLARE #LastOldService int = (SELECT MAX(ID) FROM SERVICE);
SELECT TOP 0 * INTO #Service FROM SERVICE;
ALTER TABLE #Service
DROP COLUMN RECNUM;
INSERT INTO #Service exec dbo.app_NewServicesOldFSD
;WITH cteOldFSDsToCreate AS (
SELECT JobID.Item JobID, CONVERT(date,ServDate.Item,103) ServDate
FROM dbo.SplitStringToTable(#OldServicesStr,',',2) JobID
INNER JOIN dbo.SplitStringToTable(#OldServicesStr,',',2) ServDate ON JobID.Rw = ServDate.Rw AND JobID.Cl = 0 AND ServDate.Cl = 1
)
INSERT INTO SERVICE SELECT S.*
FROM #Service S
INNER JOIN cteOldFSDsToCreate N ON N.JobID = S.JOB_ID AND N.ServDate = S.DATE
DROP TABLE #Service
A useable and likely #OldServicesStr could be '11428,23/07/2019,11429,23/07/2019,15186,5/10/2019'
To test it in SSMS I opened a new Query and typed in
exec app_CreateNewServicesOldFSD '11428,23/07/2019,11429,23/07/2019,15186,5/10/2019'
And got the following error:
Warning: Null value is eliminated by an aggregate or other SET operation.
Msg 208, Level 16, State 0, Procedure app_CreateNewServicesOldFSD, Line 65 [Batch Start Line 7]
Invalid object name '#Service'.
Completion time: 2020-11-20T20:36:57.1356921+11:00
I know that is not Your case but in the sake of not forget, there is also a limitation using Biztalk WCF-Custom adapter with SQL Bindings and temp tables.
According to this site :
http://thoughtsofmarcus.blogspot.com/2010/11/calling-stored-procedures-from-biztalk.html
You need to SET FMTONLY OFF before creating temp tables and inserting into them and
SET FMTONLY ON before selecting from them as in example from below.
ALTER PROCEDURE [dbo].[FetchTestData]
(#a4 varchar(4))
AS
DECLARE #FmtOnlyIsSet bit = 0
IF (1=0) BEGIN SET #FmtOnlyIsSet = 1 END
IF #FmtOnlyIsSet = 1
SET FMTONLY OFF
SELECT t1, t2, t3 INTO #temp01 FROM Table_1
IF #FmtOnlyIsSet IS NULL
SET FMTONLY ON
SELECT t1, t2, t3 FROM #temp01
RETURN

Issue with SQL INSERT Trigger

I'm reaching out for some help on this trigger I'm trying to get working.
Basically this is what I'm trying to do.
We have DMS software that writes to a Database and on a particular INSERT value I want the trigger to fire.
This is an example of an INSERT statement that will get processed.
INSERT INTO DOCSADM.ACTIVITYLOG (CR_IN_USE,ACTIVITY_DESC,BILLED_ON,BILLABLE,PAGES,KEYSTROKES,
TYPE_TIME,ELAPSED_TIME,TYPIST,AUTHOR,START_DATE,ACTIVITY_TYPE,REF_DOCUMENT,REF_LIBRARY,APPLICATION,VERSION_LABEL,DOCNUMBER,SYSTEM_ID)
VALUES ('','DOCSFusion','1753-01-01','',0,0,0,0,1920,1920,'2020-08-26T10:17:56',**115**,0,-1,1173,'',75,3252)
but I only want the trigger to fire when we see a value of 115 for the bold section in the INSERT statement (the Activity_type value).
For all other values that re not 115 I don't want to do anything.
This is what I have so far:
CREATE TRIGGER BW_TRIGGER
ON DOCSADM.ACTIVITYLOG
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--Declare some variable and set it as a value of 115.
--Example:
DECLARE #AlogType int = (SELECT I.ACTIVITY_TYPE FROM DOCSADM.ACTIVITYLOG A, INSERTED I) --This is the value you are looking for regarding the DM client/Matter actitivty type.
DECLARE #AlogDesc varchar(32) = (Select i.ACTIVITY_DESC from docsadm.ACTIVITYLOG A, INSERTED I)
--Next, you should have a fork or path in your trigger to determine how it proceeds.
--Path 1: The #AlogType value matches the inserted value so you want to process the rest of the trigger. Example path – “ProcessTrigger:”
--Path 2: The #AlogType value does NOT match the inserted value, you want to exit the trigger. Example Path – “ExitTrigger:”
IF #AlogType <> 115
GOTO TriggerExit;
ELSE
Begin
/*Create first temp table to collect insert values*/ --This table will have the SysID Value and the corresponding docnumber for the items you want.
--You can add whatever other values you think you need.
CREATE TABLE #TempSet1
(
AlogsysID INT,
Docnum INT,
AlogDate Varchar(64),
AlogTypist INT,
AlogAuthor INT,
AlogDesc varchar(32),
ALOGVER varchar(10),
ALOG_MATTER INT
)
INSERT INTO #TempSet1 (AlogsysID,Docnum,AlogDate,AlogTypist,AlogAuthor, ALOG_MATTER)
--SELECT  You SELECT STATEMENT WILL GO HERE MODIFIED TO POPULATE THE TABLE WITH THE DOCNUMBERS YOU WANT!!
select top 1 System_id, docnumber, LAST_ACCESS_DATE, TYPIST, AUTHOR, MATTER from docsadm.PROFILE where EXISTS (SELECT CLIENT.SYSTEM_ID FROM DOCSADM.CLIENT INNER JOIN DOCSADM.MATTER ON MATTER.CLIENT_ID = CLIENT.SYSTEM_ID
WHERE MATTER.SYSTEM_ID =#AlogDesc OR INH_LUP_SEC_FROM IS NULL OR INH_LUP_SEC_FROM = 0) AND MATTER=#AlogDesc
/*Set variable #SysID as the LASTKEY value -1. This will be used to set the SysID column on the #TempSet table*/
--DECLARE #SysID INT = (SELECT LASTKEY FROM DOCSADM.SEQ_SYSTEMKEY) -1;
/*Set the SysID value for every row on the #TempSet1 table as the #SysID variable +1*/
--UPDATE #TempSet1
--SET #SysID = AlogsysID = #SysID + 1
--Your #TempSet should now be set with ALL of the System_IDs and Docnumbers necessary for your insert!!!!—
--Verify this by doing a select against the #TempSet1 Table
SELECT * FROM #TempSet1;
--Next you need to set the SystemID to the correct value for future processing. To do this, we need to get a total count from the #TempSet table.
/*Set a variable to update the NEXTKEY value on the DOCSADM.SEQ_SYSTEMKEY table. The NEXTKEY value is used for the SYSTEM_ID field*/
--DECLARE #SeqUpdateCount INT = (SELECT COUNT(*) FROM #TempSet1);
/*Update the LASTKEY Value on the SEQ_SYSTEMKEY table to the next available value for DM.*/
--UPDATE DOCSADM.SEQ_SYSTEMKEY SET LASTKEY = LASTKEY+#SeqUpdateCount
--If you have all the values you need in your temp table, you can now insert them into the ACTIVITYLOG table.
--INSERT INTO DOCSADM.ACTIVITY
--(SYSTEM_ID, DOCNUMBER, START_DATE, version, EXT,)
--SELECT
--AlogSysID,Docnum,GETUTCDATE(),BLAH, BLAH
--FROM #TableSet1
INSERT INTO DOCSADM.ACTIVITYLOG
(SYSTEM_ID,
DOCNUMBER,
START_DATE,
TYPIST,
AUTHOR,
ACTIVITY_DESC,
VERSION_LABEL,
ACTIVITY_TYPE)
SELECT
AlogsysID, Docnum,AlogDate,AlogTypist, AlogAuthor, ALOG_MATTER, '',115
FROM #TempSet1;
--Now you need to Drop the Temp Table
DROP TABLE #TempSet1
--Go to the other half of your path above to exit the trigger.
END
TriggerExit:
END
Go
but when I try to run any INSERT statement on this table I get this error message. It doesn't matter if the activity_type has a value of 115 or not
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I know the issue is with this section of the trigger:
INSERT INTO #TempSet1 (AlogsysID,Docnum,AlogDate,AlogTypist,AlogAuthor, ALOG_MATTER)
--SELECT  You SELECT STATEMENT WILL GO HERE MODIFIED TO POPULATE THE TABLE WITH THE DOCNUMBERS YOU WANT!!
SELECT TOP 1
System_id
, docnumber
, LAST_ACCESS_DATE
, TYPIST
, AUTHOR
, MATTER
FROM docsadm.PROFILE
WHERE EXISTS (SELECT CLIENT.SYSTEM_ID
FROM DOCSADM.CLIENT
INNER JOIN DOCSADM.MATTER
ON MATTER.CLIENT_ID = CLIENT.SYSTEM_ID
WHERE MATTER.SYSTEM_ID =#AlogDesc
OR INH_LUP_SEC_FROM IS NULL
OR INH_LUP_SEC_FROM = 0)
AND MATTER=#AlogDesc
It's the SELECT statement that is causing it to fail.
I know that this statement will bring back multiple rows but I only need the value from one of them so I can use this value for my INSERT. I though having the "select top 1" would do this for me but it's not working like I think it should. What am I missing?
If I had to guess I would say your problem is here:
DECLARE #AlogType int = (SELECT I.ACTIVITY_TYPE FROM DOCSADM.ACTIVITYLOG A, INSERTED I) --This is the value you are looking for regarding the DM client/Matter actitivty type.
DECLARE #AlogDesc varchar(32) = (Select i.ACTIVITY_DESC from docsadm.ACTIVITYLOG A, INSERTED I)
How are ACTIVITYLOG and INSERTED joined in the above ? without a where it would be a CROSS JOIN. Why do you even drag ACTIVITYLOG into it, you can simply use INSERTED. Also please try to stop using implicit joins ( I can see that later down the script you use the proper, more verbose join syntax)
TRY:
DECLARE #AlogType int = (SELECT I.ACTIVITY_TYPE FROM INSERTED I) --This is the value you are looking for regarding the DM client/Matter actitivty type.
DECLARE #AlogDesc varchar(32) = (Select i.ACTIVITY_DESC from INSERTED I)
Be careful that this will work with single inserts only. When you do batched inserts the INSERTED is a table containing multiple rows and you will run into issues again.

Invalid object name error for temporary table defined in a stored procedure

I have written a stored procedure which executes a stored procedure and the result it gets has to be stored in a local temporary table. The Stored procedure gets created without giving any errors. But when I try to execute stored procedure, it returns the error that the temporary table is invalid object name.
CREATE PROCEDURE .dbo.CalulateETFWeights
-- Add the parameters for the stored procedure here
#CURR_DATE varchar(255),
#ETF_DATE datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--Select max(ETF_DATE) into #ETF_DATE from .dbo.ETF_LIST_V --where ETF_LOAD_DATE = #CURR_DATE
-- Insert statements for procedure here
SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable
from .map.v_get_tqa_security where cusip in (select distinct ETF_CUSIP from .dbo.ETF_LIST_V where ETF_LOAD_DATE = 'Mon Jun 17 14:15:09 BST 2019')
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd
#sourceTable = '#tempetftable',
#startDate = '20181219',
#endDate = '20181219',
#frequency = 'D'
Insert into .dbo.ETFComponentWeights
Select
C.ETF_CUSIP as W_CAL_CUSIP,
C.STK_IDX as W_CAL_COMP,
C.STK_QUANT as W_CAL_SHARES,
CP.VALUE as W_CAL_PRICE,
(C.STK_QUANT * CP.VALUE_) as W_CAL_MVAL,
(C.STK_QUANT * CP.VALUE_)/SUM(C.STK_QUANT * CP.VALUE) over (partition by C.ETF_CUSIP) as W_CAL_WEIGHT,
#ETF_DATE as W_CAL_DATE
from .dbo.ETF_COMP_V C
inner join (Select E.CUSIP, P.Value_ from #tempPriceTable P inner join #tempetftable E on P.TqaSecCode = E.TqaSecCode) CP
on C.ETF_CUSIP = CP.CUSIP
So the error I get is
Invalid object name '#tempPriceTable'.
I don't understand why is this not working? Can anyone suggest what am I doing wrong here? and why the #tempetftable works fine. But #tempPriceTable here is not working in this scenario?
Syntax:
SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable
creates a new temp table and then inserts data into this new table.
Syntax:
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd
is a regular "insert into" statement, which adds rows to existing table.
To use this syntax you need to create an empty temp table with correct schema beforehand.
So you need to do something like:
CREATE TABLE #tempPriceTable (your schema)
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd

SQL Server trigger on a procedure failing

I am trying to trigger a stored procedure to take the inserted values into my stored procedure as parameters and it is not letting me.
My table flow goes like this: a patient's history information will be inserted (HISTORY_APPOINTMENTS) and if at the time the patient has a column value of HasSuicidalThoughts = 'Y' I want the trigger to send the inserted patients information into a table I created called SuicideWatchLog.
First I created the table:
/* Table Creation for SuicideWatch Log*/
CREATE TABLE SuicideWatchLog
(
logNum integer IDENTITY(1,1) NOT NULL PRIMARY KEY,
PatientStudy# integer FOREIGN KEY References Patients(PatientStudy#),
PatientName varchar(20),
[Date] date,
Dr# integer FOREIGN KEY References DOCTORS(Dr#),
DaysinStudy integer
)
Next I created the procedure:
CREATE PROCEDURE AddSuicideWatch
#PatientStudy# integer,
#PatientName varchar(20),
#Date date,
#Dr# integer,
#DaysinStudy integer
AS
BEGIN
INSERT INTO SuicideWatchLog(PatientStudy#, Date, Dr#)
(SELECT PatientStudy#, ApptDate, Dr#
FROM APPOINTMENTS
WHERE #PatientStudy# = PatientStudy#
AND #Date = ApptDate
AND #Dr# = Dr#)
INSERT INTO SuicideWatchLog(PatientName, DaysinStudy)
(SELECT PatientFName, datediff(day,StudyStartDate,getdate())
FROM PATIENTS
WHERE #PatientName = PatientFName
AND #DaysinStudy = datediff(day,StudyStartDate,getdate()))
END
Finally I created the trigger:
CREATE TRIGGER SuicidalPatient
ON HISTORY_APPOINTMENT
AFTER INSERT
AS
EXEC AddSuicideWatch(
SELECT (I.PatientStudy#, P.PatientFName, A.ApptDate,
FROM INSERTED I
JOIN APPOINTMENTS A ON I.Appt# = A.Appt#
JOIN PATIENTS P ON I.PatientStudy# = P.PatientStudy#)
I expected this to allow me to send the inserted values into the stored procedure to trigger the creation of the log, but instead I am getting an error that is telling me my parameters aren't being found.
Is this an issue with the select statement, or is it a problem with the procedure itself?
Is this an issue with the select statement, or is it a problem with the procedure itself?
Your stored procedure accepts scalar parameters. You can't pass a whole resultset to it. You can:
1) Integrate the INSERTs directly into the trigger body, eliminating the stored procedure.
2) Open a cursor over the query in the trigger, and loop through the rows, calling the stored procedure fore each one.
3) Declare a User-Defined Table Type matching the query result rows, declare and load an instance of the table type in the trigger body, and change the stored procedure to accept a Table-Valued Parameter.
you cant pass table to sp , but i know 2 ways for that :
1- use user defined type like that :
create type NewTable AS table (PatientStudy# int, PatientFName nvarchar(max), ApptDate date)
and the insert into NewTable Then call sp
declare #TempTable NewTable
insert into #TempTable(PatientStudy# , PatientFName , ApptDate)
select I.PatientStudy#, P.PatientFName, A.ApptDate,
FROM INSERTED I
JOIN APPOINTMENTS A ON I.Appt# = A.Appt#
JOIN PATIENTS P ON I.PatientStudy# = P.PatientStudy#
EXEC AddSuicideWatch( #TempTable)
and of course you should edit your SP :
CREATE PROCEDURE AddSuicideWatch
#Table NewTable
AS
BEGIN
INSERT INTO SuicideWatchLog(PatientStudy#, Date, Dr#)
SELECT PatientStudy#, ApptDate, Dr#
FROM APPOINTMENTS A join #Table T ON
A.PatientStudy# = T.PatientStudy#
A.Date = T.ApptDate
A.Dr# = D.Dr#
INSERT INTO SuicideWatchLog(PatientName, DaysinStudy)
(SELECT PatientFName, datediff(day,StudyStartDate,getdate())
FROM PATIENTS P join #Table T ON
T.PatientName = P.PatientFName
AND A.DaysinStudy = datediff(day,StudyStartDate,getdate()))
END
And the seccond way : just pass the primary key to sp and handle other things in sp

How to avoid duplicate records using stored procedure [duplicate]

This question already has answers here:
SQL Server Insert if not exists
(13 answers)
Closed 3 years ago.
I want to write a stored procedure to insert data into a table and also to check whether same data already exist?
If yes, then exception throw as already exist. But I don't know where should I add an exception. Please help.
ALTER PROCEDURE [dbo].[datakutipantest1]
AS
BEGIN
DECLARE #ModifiedDate datetime = GETDATE()
INSERT INTO spk_DataKutipan ([NO_BIL], [NO_AKAUN], [TKH_BAYAR],
[STESYEN], [AMAUN_BAYAR], [JENIS_BAYAR], [NO_RESIT], [STATUS], [NO_VOT], [TKH_MODIFIKASI])
SELECT
D.BillNo,
D.AccountNo,
D.TxDate,
D.ReferenceCode,
D.Amount,
PaymentTypeId,
D.ReferenceNo,
D.Status,
D.RevenueCode,
#ModifiedDate
FROM
(SELECT
B.ComponentId,
B.AccountNo,
B.BillNo,
B.RevenueCode,
B.Amount,
B.TxId,
ReferenceNo,
B.ReferenceCode,
status,
TxDate
FROM
(SELECT
A.ComponentId,
A.TxId,
AccountNo,
BillNo,
RevenueCode,
Amount,
C.ReferenceCode
FROM
rcs_TxBillItem A
INNER JOIN
(SELECT
ComponentId,
ComponentName,
ReferenceCode
FROM
rcs_Component
WHERE
IsDeleted = 0) C ON C.ComponentId = A.ComponentId) B
INNER JOIN
rcs_TxBill P ON P.TxId = B.TxId) D
INNER JOIN
rcs_TxBillPayment E ON E.TxId = D.TxId
END
GO
CREATE PROCEDURE [dbo].[Useradd]
#username varchar(20),
#pword nvarchar(20),
#empname varchar(20),
#email nvarchar(50),
#designation varchar(20),
#reportto varchar(20) AS
IF (SELECT COUNT(*) FROM Users WHERE username= #username) < 1
BEGIN
INSERT INTO Users(username,pword,empname,email,designation,reportto)
VALUES(#username,#pword,#empname,#email,#designation,#reportto)
END
Consider to use MERGE
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition> --For example ColumnSource = ColumnTable
WHEN MATCHED --Rows already exist
THEN UPDATE target_table SET ... --Do Update for example
WHEN NOT MATCHED BY TARGET --Rows not exist on Target Table
THEN INSERT(...) VALUES(...) --Do Insert for example
WHEN NOT MATCHED BY SOURCE --Rows not exist on Source table but exists on Target Table
THEN DELETE ... --Do Delete for example
The MERGE statement basically merges data from a source result set to a target table based on a condition that you specify and if the data from the source already exists in the target or not. The new SQL command combines the sequence of conditional INSERT, UPDATE and DELETE commands in a single atomic statement, depending on the existence of a record.

Resources