Converting the sybase stuff function in to oracle - sql

Sybase query :
UPDATE #horizCallSign SET
effDaysZ = STUFF( effDaysZ, csd.day+shift1, 1, '1')
FROM #callSignTbl csd
WHERE csd.legId1 = #horizCallSign.legId1
AND csd.legId2 = #horizCallSign.legId2
AND day = 28
Oracle Query :
UPDATE TEMP_HORIZ_CALL_SIGN
SET eff_Days_Z = REPLACE( eff_days_Z,csd.day+shift1, '1')
FROM temp_call_sign_table1 csd
WHERE csd.leg_Id1 = temp_horiz_Call_Sign.leg_Id1
AND csd.leg_Id2 = horiz_Call_Sign.leg_Id2
AND day = 28
In Oracle, we have changed the column names with having "_" in them.
temp_call_sign_table1 and TEMP_HORIZ_CALL_SIGN are global temporary tables
created for Oracle in place of sybase temp tables with on commit preserve
rows.
We are converting sybase DB to Oracle DB. When I execute the above query in oracle it says QL Error: ORA-00933: SQL command not properly ended.
Please help.

There was an issue with your table name on line 5, this is easily avoided by using aliases.
Also, you can't use FROM in Oracle without a SELECT. Thus a sub-query must be used in this instance. For example...
UPDATE TEMP_HORIZ_CALL_SIGN hcs
SET eff_Days_Z =
(SELECT
REPLACE(eff_days_Z,csd.day+shift1, '1')
FROM temp_call_sign_table1 csd
WHERE csd.leg_Id1 = hcs.leg_Id1
AND csd.leg_Id2 = hcs.leg_Id2
AND day = 28)
(Not tested)

Your are off on your aliasing. Also, consider using INNER JOINS instead.
UPDATE THCS
SET eff_Days_Z = REPLACE( eff_days_Z,csd.day+shift1, '1')
FROM temp_call_sign_table1 csd , TEMP_HORIZ_CALL_SIGN THCS, horiz_Call_Sign HCS
WHERE csd.leg_Id1 = THCS.leg_Id1
AND csd.leg_Id2 = HCS.leg_Id2
AND day = 28

In Oracle, You can use MERGE INTO.
MERGE INTO temp_horiz_call_sign m
using (SELECT leg_id1,
leg_id2,
eff_days_z,
day + shift1 plushift1
FROM temp_call_sign_table1
WHERE day = 28) r
ON ( r.leg_id1 = m.leg_id1
AND r.leg_id2 = m.leg_id2 )
WHEN matched THEN
UPDATE SET m.eff_days_z = Replace(m.eff_days_z, r.plushift1, '1');

Related

GETTING ERROR-- ORA-00936:MISSING EXPRESSION for below query please help on this

SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;

Oracle SQL merge does not insert when the destination table is empty

I want to update/insert records in main schema from working schema using a merge statement. The piece of code that does the merge is as below.
MERGE INTO ods_workday.ORGANIZATION x
USING (SELECT organization_id, organization_code, organization_type, organization_name, organization_description,
inactive, manager, leadership_role_assignee, organization_owner, supervisory_organization,
primary_site_reference, inactive_date, organization_subtype
FROM ods_workday_w.ORGANIZATION) y
ON (x.ORGANIZATION_ID = y.ORGANIZATION_ID)
WHEN MATCHED THEN
UPDATE SET x.organization_code = y.organization_code,
x.organization_type = y.organization_type,
x.organization_name = y.organization_name,
x.organization_description = y.organization_description,
x.inactive = y.inactive,
x.manager = y.manager,
x.leadership_role_assignee = y.leadership_role_assignee,
x.organization_owner = y.organization_owner,
x.supervisory_organization = y.supervisory_organization,
x.primary_site_reference = y.primary_site_reference,
x.inactive_date = y.inactive_date,
x.organization_subtype = y.organization_subtype,
x.UPDATE_TIME_STAMP = CURRENT_TIMESTAMP
WHERE x.organization_code <> y.organization_code OR
x.organization_type <> y.organization_type OR
x.organization_name <> y.organization_name OR
x.organization_description <> y.organization_description OR
x.inactive <> y.inactive OR
x.manager <> y.manager OR
x.leadership_role_assignee <> y.leadership_role_assignee OR
x.organization_owner <> y.organization_owner OR
x.supervisory_organization <> y.supervisory_organization OR
x.primary_site_reference <> y.primary_site_reference OR
x.inactive_date <> y.inactive_date OR
x.organization_subtype <> y.organization_subtype
WHEN NOT MATCHED THEN
INSERT(x.organization_id, x.organization_code, x.organization_type, x.organization_name,
x.organization_description, x.inactive, x.manager, x.leadership_role_assignee,
x.organization_owner, x.supervisory_organization, x.primary_site_reference, x.inactive_date,
x.organization_subtype, x.UPDATE_TIME_STAMP)
VALUES(y.organization_id, y.organization_code, y.organization_type, y.organization_name,
y.organization_description, y.inactive, y.manager, y.leadership_role_assignee,
y.organization_owner, y.supervisory_organization, y.primary_site_reference, y.inactive_date,
y.organization_subtype, CURRENT_TIMESTAMP);
The code works fine when the destination table has records, but when the destination table is empty (has zero records than the merge).
Is there a way we can make the merge statement to work, when there are no records to start with in the target schema.
The Oracle database is 11g.
This is resolved.
I had not added a foreign key constraint present in source table because of which insert on zero records was not working.

Update multiple columns from a sub query

UPDATE PINPOINT_SUPPLEMENT
SET (ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (
SELECT key,'file'
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
)
WHERE ATTACHMENT_VALUE IS NULL
Getting Error when i execute this query
ERROR: syntax error at or near "SELECT"
LINE 3: SELECT key,'file
update PINPOINT_SUPPLEMENT
set
ATTACHMENT_VALUE = PINPOINT_DOCUMENT.key,
ATTACHMENT_TYPE = 'file'
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
or
update PINPOINT_SUPPLEMENT
set
(ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (PINPOINT_DOCUMENT.key, 'file')
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
Support for updating tuples was introduced in Postgres 9.5, so you can't use that syntax with your version.
But as the second value is a constant I don't see a reason to use that syntax to begin with:
UPDATE PINPOINT_SUPPLEMENT
SET ATTACHMENT_VALUE = (SELECT "key"
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID),
ATTACHMENT_TYPE = 'file'
WHERE ATTACHMENT_VALUE IS NULL
Note, that the sub-query might result in an error if there is more than one ATTACHMENT_VALUE for a document_id!

conditional handling of update sql script depending on input parameter

I am new to SQL and using Oracle 11. I need to write a sql script which uses different update command based on whether the input param is null or not null.
I need something like this
['&' followed by the parameter name is the way i see parameters being used in other such
script for our project]
IF &PRG_ID IS NULL
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID);
ELSE
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE
P.PROGRAM_ID = &PRG_ID AND EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID);
END IF;
Something like (is not tested):
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID)
AND (&PRG_ID IS NULL OR P.PROGRAM_ID = &PRG_ID);
But take into account this change may lead to the performance degradation for the case when PRG_ID has definite value.

Need help joining 3rd table to Stored Proc

I have a stored Procedure that works fine joining 2 tables together. I needed to add a new field from a new table that was not included in the original SP. What I am trying to do is sum a field from the new table for each record that is a child record of the Parent table which is in the original SP.
I tested the Sum based on th parent table in a test query and it works fine:
select totaldollars from TTS_EmpTime where emptimedaytotal_id='32878'
so then the next step would be to integrate into the SP. I did so and have set the new portions of the SP to be bold so you can see what was added. IF the bold portions are removed the SP works fine if not I get this error:
*Msg 8120, Level 16, State 1, Procedure TTS_RptTest2, Line 11
Column 'TTS_EmpTimeDayTotal.EmployeeID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause*.
Here is my Stored Proc:
USE [TTSTimeClock]
GO
/****** Object: StoredProcedure [dbo].[TTS_RptTest2] Script Date: 03/04/2011 12:29:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[TTS_RptTest2]
#BureauID nvarchar(36),
#CompanyID nvarchar(36),
#DivisionID nvarchar(10) ,
#punchDate smalldatetime,
#PeriodDays integer,
#EmployeeID nvarchar(20) = null
As
--with DayTotals as(
select
DayTotal.DivisionID,
DayTotal.EmployeeID,
EmpData.EmployeeFirstName AS First,
EmpData.EmployeeLastName AS Last,
EmpData.employeetypeid AS EmpId,
DayTotal.ID as DayTotalID,
-- Format the Date as MM/DD DOW or 2Digit Month & 2Digit Day and the 3Char Day of the week Uppercase
convert(varchar(5),DayTotal.PunchDate,101) + ' ' + upper(left(datename(dw,DayTotal.Punchdate),3))as PunchDate,
-- Format the in and out time as non military time with AM or PM No Dates
substring(convert(varchar(20), DayTotal.FirstDayPunch, 9), 13, 5) + ' ' + substring(convert(varchar(30), DayTotal.FirstDayPunch, 9), 25, 2)as TimeIn,
substring(convert(varchar(20), DayTotal.LastDayPunch, 9), 13, 5) + ' ' + substring(convert(varchar(30), DayTotal.LastDayPunch, 9), 25, 2) as TimeOut,
DayTotal.RegularHours,
DayTotal.NonOvertimeHours,
DayTotal.OvertimeHours,
DayTotal.TotalDayHRS,
DayTotal.PeriodRegular,
DayTotal.PeriodOtherTime,
DayTotal.PeriodOvertime,
DayTotal.PeriodTotal,
**sum(cast(EmpTime.TotalDollars as float)) as TotalDayDollars**
from TTS_EmpTimeDayTotal as DayTotal
INNER JOIN TTS_PayrollEmployees AS EmpData
ON DayTotal.EmployeeID = EmpData.EmployeeID
**inner JOIN TTS_Emptime as EmpTime
ON DayTotal.id = emptime.emptimedaytotal_id**
where
DayTotal.BureauID = #BureauID
AND DayTotal.CompanyID = #CompanyID
AND (DayTotal.DivisionID = #DivisionID)
AND daytotal.periodstart =
-- Period start date
(SELECT DISTINCT PeriodStart
FROM TTS_EmpTimeDayTotal
WHERE(BureauID = #BureauID) AND (CompanyID = #CompanyID) AND ( (DivisionID = #DivisionID))
AND (PunchDate = #punchDate)and periodend = dateadd(d,(#PeriodDays - 1),(periodstart)))
AND daytotal.periodend =
-- Period End Date
(SELECT DISTINCT PeriodEnd
FROM TTS_EmpTimeDayTotal
WHERE(BureauID = #BureauID) AND (CompanyID = #CompanyID) AND ( (DivisionID = #DivisionID))
AND (PunchDate = #punchDate)and periodend = dateadd(d,(#PeriodDays-1),(periodstart)))
-- Optional all employees or just one
AND (( #EmployeeID is Null) or (DayTotal.EmployeeID = #EmployeeID))
order by Empdata.employeetypeid,DayTotal.punchdate
I am not grouping at all so this must be caused by something else?
Any Help will be appreciated
Is this SQL Server? Looks like it. You're using SUM, an aggregate function, which I don't believe you can use without a GROUP BY clause. Did you always have the SUM in there, or did you add it alongside the new table?
If the latter, that may well be your problem.
Update
Based on OP's comment:
Wow that could be a pain would I do
somehing like groupby field1,field2,
and so on? as in a coma delimited
list. Is there another way to include
this one field that would be better?
Yes, in SQL Server you must be explicit with groupings when using an aggregate function. One alternative in your case would be to do the grouping as a subquery, and join on that, i.e.:
FROM TTS_EmpTimeDayTotal AS DayTotal
INNER JOIN TTS_PayrollEmployees AS EmpData ON DayTotal.EmployeeID = EmpData.EmployeeID
INNER JOIN (SELECT EmpTimeDayTotal_id, SUM(CAST(TotalDollars AS FLOAT)) AS TotalDayDollars
FROM TTS_Emptime
GROUP BY EmpTimeDayTotal_id) AS EmpTime ON DayTotal.id = EmpTime.EmpTimeDayTotal_id
And then simply reference EmpTime.TotalDayDollars in the SELECT list, instead of performing the SUM there.