passing variable to OLE DB Source in oracle data source SSIS - oracle

I have the following query , I want to load only the data where last_update_date is greater than one of the value, I delure the value in correct way and its working in SQL Server data source but when I use Oracle date source its not working. Also I try to change the quotation mark with :VARIABLENAME it gives me this error:
not all variables bound
SELECT
PAPF.NATIONAL_IDENTIFIER NatnlIdNum,
PAY.PAYROLL_NAME PayrollName,
DEP.NAME DepartmentName,
PER_ASSIGNMENT_STATUS_TYPES_TL.USER_STATUS EmploymentStatDesc,
EMPLOYMENT_CATEGORY.MEANING EmploymentCatDesc,
PER_POSITION_DEFINITIONS.SEGMENT4 PositionName,
PAAF.EFFECTIVE_START_DATE EffectiveStartDate,
PAAF.EFFECTIVE_END_DATE EffectiveEndDate,
PG.NAME GradeName,
FROM PER_ALL_PEOPLE_F PAPF
INNER JOIN PER_ALL_ASSIGNMENTS_F PAAF
ON PAPF.PERSON_ID=PAAF.PERSON_ID
LEFT JOIN PAY_ALL_PAYROLLS_F PAY
ON PAAF.PAYROLL_ID=PAY.PAYROLL_ID
AND TRUNC(SYSDATE) BETWEEN PAY.EFFECTIVE_START_DATE
AND PAY.EFFECTIVE_START_DATE
LEFT JOIN PAY_PEOPLE_GROUPS PPG
ON PAAF.PEOPLE_GROUP_ID=PPG.PEOPLE_GROUP_ID
LEFT JOIN HR_ALL_ORGANIZATION_UNITS DEP
ON PPG.SEGMENT2=DEP.ORGANIZATION_ID
LEFT JOIN fnd_lookup_values EMPLOYMENT_CATEGORY
ON PAAF.EMPLOYMENT_CATEGORY=EMPLOYMENT_CATEGORY.lookup_code
AND EMPLOYMENT_CATEGORY.language='AR'
AND EMPLOYMENT_CATEGORY.lookup_type='EMP_CAT'
LEFT JOIN PER_ASSIGNMENT_STATUS_TYPES_TL
ON PAAF.ASSIGNMENT_STATUS_TYPE_ID=PER_ASSIGNMENT_STATUS_TYPES_TL.ASSIGNMENT_STATUS_TYPE_ID
AND PER_ASSIGNMENT_STATUS_TYPES_TL.language='AR'
LEFT JOIN HR_ALL_POSITIONS_F
ON PAAF.POSITION_ID=HR_ALL_POSITIONS_F.POSITION_ID
AND TRUNC(SYSDATE) BETWEEN HR_ALL_POSITIONS_F.EFFECTIVE_START_DATE
AND HR_ALL_POSITIONS_F.EFFECTIVE_START_DATE
LEFT JOIN PER_POSITION_DEFINITIONS
ON HR_ALL_POSITIONS_F.POSITION_DEFINITION_ID=PER_POSITION_DEFINITIONS.POSITION_DEFINITION_ID
LEFT JOIN PER_GRADES PG
ON PAAF.GRADE_ID=PG.GRADE_ID
LEFT JOIN HR_ALL_ORGANIZATION_UNITS SCHOOL
ON PPG.SEGMENT5=SCHOOL.ORGANIZATION_ID
LEFT JOIN HR_ALL_ORGANIZATION_UNITS ED
ON PPG.SEGMENT6=ED.ORGANIZATION_ID
WHERE TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE
AND PAPF.EFFECTIVE_END_DATE
AND PAAF.LAST_UPDATE_DATE >?
Parameters cannot be extracted from the SQL command. The provider might not help to parse parameter information from the command. In that case, use the "SQL command from variable" access mode, in which the entire SQL command is stored in a variable. ORA-00920: invalid relational operator (OraOLEDB)

You can build the query in a string variable, then use the "SQL command from variable" option in the OLE DB Source and add the parameter in the query. However when doing this, make sure that the date format passed into the Oracle data source is the date format that is expected. To concatenate the date parameter within the string variable holding the SQL, it will need to be cast to a string, the format will still be preserved, however this doesn't guarantee that it will be passed as a date data type and casting may be necessary. Also, note that the string casting function (DT_STR, number of characters, code page) will return an error if the length of the string that's converted is longer than the specified length and it's subsequently truncated. An example expression for the SQL in a string variable follows.
"SELECT
PAPF.NATIONAL_IDENTIFIER NatnlIdNum,
PAY.PAYROLL_NAME PayrollName,
DEP.NAME DepartmentName,
PER_ASSIGNMENT_STATUS_TYPES_TL.USER_STATUS EmploymentStatDesc,
EMPLOYMENT_CATEGORY.MEANING EmploymentCatDesc,
PER_POSITION_DEFINITIONS.SEGMENT4 PositionName,
PAAF.EFFECTIVE_START_DATE EffectiveStartDate,
PAAF.EFFECTIVE_END_DATE EffectiveEndDate,
PG.NAME GradeName,
FROM PER_ALL_PEOPLE_F PAPF
INNER JOIN PER_ALL_ASSIGNMENTS_F PAAF
ON PAPF.PERSON_ID=PAAF.PERSON_ID
LEFT JOIN PAY_ALL_PAYROLLS_F PAY
ON PAAF.PAYROLL_ID=PAY.PAYROLL_ID
AND TRUNC(SYSDATE) BETWEEN PAY.EFFECTIVE_START_DATE
AND PAY.EFFECTIVE_START_DATE
LEFT JOIN PAY_PEOPLE_GROUPS PPG
ON PAAF.PEOPLE_GROUP_ID=PPG.PEOPLE_GROUP_ID
LEFT JOIN HR_ALL_ORGANIZATION_UNITS DEP
ON PPG.SEGMENT2=DEP.ORGANIZATION_ID
LEFT JOIN fnd_lookup_values EMPLOYMENT_CATEGORY
ON PAAF.EMPLOYMENT_CATEGORY=EMPLOYMENT_CATEGORY.lookup_code
AND EMPLOYMENT_CATEGORY.language='AR'
AND EMPLOYMENT_CATEGORY.lookup_type='EMP_CAT'
LEFT JOIN PER_ASSIGNMENT_STATUS_TYPES_TL
ON PAAF.ASSIGNMENT_STATUS_TYPE_ID=PER_ASSIGNMENT_STATUS_TYPES_TL.ASSIGNMENT_STATUS_TYPE_ID
AND PER_ASSIGNMENT_STATUS_TYPES_TL.language='AR'
LEFT JOIN HR_ALL_POSITIONS_F
ON PAAF.POSITION_ID=HR_ALL_POSITIONS_F.POSITION_ID
AND TRUNC(SYSDATE) BETWEEN HR_ALL_POSITIONS_F.EFFECTIVE_START_DATE
AND HR_ALL_POSITIONS_F.EFFECTIVE_START_DATE
LEFT JOIN PER_POSITION_DEFINITIONS
ON HR_ALL_POSITIONS_F.POSITION_DEFINITION_ID=PER_POSITION_DEFINITIONS.POSITION_DEFINITION_ID
LEFT JOIN PER_GRADES PG
ON PAAF.GRADE_ID=PG.GRADE_ID
LEFT JOIN HR_ALL_ORGANIZATION_UNITS SCHOOL
ON PPG.SEGMENT5=SCHOOL.ORGANIZATION_ID
LEFT JOIN HR_ALL_ORGANIZATION_UNITS ED
ON PPG.SEGMENT6=ED.ORGANIZATION_ID
WHERE TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE
AND PAPF.EFFECTIVE_END_DATE
AND PAAF.LAST_UPDATE_DATE > " + (DT_STR, 25, 1252)#[$Package:YourDateParameter]

Related

how to use select right after outer join keyword

The code SQL below is what I want to do but it's not working. Eventually I will convert that to LinQ query but for now, I just want to know how to make this works. I have one line matching in TemplateFields when I put where templateId =29 table and I want to full outer join with the TemplateAvailableFields. How can I use select right after full outer join?
select
ta.TemplateAvailableFieldId, ta.FieldName, ta.Required
from
TemplateAvailableFields as ta
full outer join
(select DefaultValue, DisplayOrder
from TemplateField
where TemplateId = 29) as t on ta.TemplateAvailableFieldId = t.TemplateAvailableFieldId;

Missing Keyword Error in Oracle - Wrong Syntax WHERE Expression

I'm new to Oracle SQL, I'm being asked to do some scenarios to learn the different expressions and so on.
I'm currently working on this statement but I keep having trouble with syntax and trying to get my expressions in the correct place.
If you don't mind taking a look at what I'm doing wrong and helping me learn the correct syntax I'd appreciate it a lot.
I have to find everything in the Sale, SaleDetail, OrderStatus, Warehouse, User and StockDetail tables.
The fields I need to find are saleno, serialstart, serialend, the product description (label field), sale status (saleid (I think)), WarehouseName (WH.NAME)
Here below is the code I've written so far.
SELECT
S.SALENO,
SD.SERIALSTART,
SD.SERIALEND,
SDT.LABEL,
USR.USERNAME,
WH.NAME
FROM
ITR_SALE,
ITR_SALEDETAIL,
ITR_ORDER,
ITR_WAREHOUSE,
ITR_USER,
ITR_STOCKDETAIL
JOIN ITR_SALE S
JOIN ITR_SALEDETAIL SD ON S.ID = SD.SALENO
JOIN ITR_WAREHOUSE WH ON SD.ID = WH.NAME
JOIN ITR_ORDER ODR ON WH.ID = ODR.STATUSID
JOIN ITR_USER USR ON ODR.ID = USR.USERNAME
JOIN ITR_STOCKDETAIL ON USR.ID = SDT.LABEL
WHERE S.LASTSTATUSCHANGETIME
BETWEEN ('2016-01-01 00:00:00' AND '2016-12-31 23:59:59')
AND STATUSID = ('COMPLETED');
Below follows the error message
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Error at Line: 21 Column: 1
EDIT:
Finished code below, changed a few expressions and conditions.
SELECT
S.SALENO,
SD.SERIALSTART,
SD.SERIALEND,
SDA.LABEL,
USR.USERNAME,
WH.NAME
FROM
ITR_SALE S
INNER JOIN
ITR_SALEDETAIL SD ON S.ID = SD.SALEID
INNER JOIN
ITR_ORDERSTATUS ODS ON SD.ID = ODS.ID
INNER JOIN
ITR_WAREHOUSE WH ON ODS.ID = WH.NAME
INNER JOIN
ITR_USER USR ON WH.ID = USR.USERNAME
INNER JOIN
ITR_STOCKDETAIL SDA ON USR.ID = SDA.LABEL
WHERE 'DATE' BETWEEN '2016-01-01' AND '2016-12-31'
AND S.STATUSID = '4';`
Use proper join syntax. Edit. Need to remove parenthesis from last line or user IN clause.
SELECT
S.SALENO,
SD.SERIALSTART,
SD.SERIALEND,
SDT.LABEL,
USR.USERNAME,
WH.NAME
FROM
ITR_SALE S INNER JOIN ITR_SALEDETAIL SD ON S.ID = SD.SALENO
INNER JOIN ITR_SALEDETAIL SD ON S.ID = SD.SALENO
INNER JOIN ITR_WAREHOUSE WH ON SD.ID = WH.NAME
INNER JOIN ITR_ORDER ODR ON WH.ID = ODR.STATUSID
INNER JOIN ITR_USER USR ON ODR.ID = USR.USERNAME
INNER JOIN ITR_STOCKDETAIL STD ON USR.ID = SDT.LABEL
WHERE S.LASTSTATUSCHANGETIME
BETWEEN '2016-01-01 00:00:00' AND '2016-12-31 23:59:59'
AND STATUSID = 'COMPLETED';

Parameterize the Decode Function

In a oracle i'm using a Decode Statement as below. For security reasons i don't want the code to have the hardcoded values and i plan to create a new lookup table.
Select CONCAT( Decode(A.COUNTRY_INITIAL,
'A','America',
'B','Brazil',
'F','FINLAND',
NULL),
Decode(A.ADD_VALUE,
'M','YES',
NULL))
from (
Select SUBSTR(COUNTRY_BASE, -1,1) as COUNTRY_INITIAL,
SUBSTR(IS_VALUED, -1,1) as ADD_VALUE
from TBL1
)A
Refernece Table
*******************
Clmn1 Clmn2 Clmn3
--------------------------
cntry1 A America
cntry2 B Brazil
cntry3 F Finland
Value1 M YES
Could you please let me know how i can incorporate this in the decode logic. Also fyi im using this CODE SNIPPET in a Oracle Function.
If you're going to store the lookup information in a table, you wouldn't use a DECODE. You'd join the two tables
SELECT ref.clmn3
FROM tbl1 t1
LEFT OUTER JOIN <<reference table>> ref
ON( substr(t1.country_base, -1, 1) = ref.clmn2 )
Since your DECODE has a NULL, I'm guessing that you are expecting that some rows of tbl1 will not have a matching row in the reference table so I'm guessing that you want a LEFT OUTER JOIN rather than an INNER JOIN.
If you have these details in a table you can simply use a join to get desired output.
select t1.COUNTRY_BASE,ref.Clmn3,ref1.Clmn3
frorm TBL1 t1
left outer join reftable ref
on SUBSTR(t1.COUNTRY_BASE, -1,1)=ref.Clmn2
left outer join reftable ref1
on SUBSTR(t1.IS_VALUED, -1,1)=ref.Clmn2;
If your are using separate table to store the values, you don't need decode function. You can simply join the two tables.
select a.country_base,
a.is_valued,
b.clmn3,
c.clmn3
from tbl1 a left outer join reference_table b
on (substr(a.country_base, -1, 1) = b.clmn2
and b.clmn1 like 'cntry%' --extra clause needed if there are same country and value codes
)
left outer join reference_table c
on (substr(a.is_valued, -1, 1) = c.clmn2
and c.clmn1 like 'Value%' --extra clause needed if there are same country and value codes
);

Invalid Identifier SQL

So i have this:
SELECT p.plantnaam,o.levcode,o.offerteprijs
FROM plant p, offerte o
JOIN (SELECT plantcode , MIN(offerteprijs) AS offprijs
FROM offerte
GROUP BY plantcode) s
ON s.plantcode = p.plantcode
AND s.offprijs = o.offerteprijs
ORDER BY p.plantnaam,l.levcode
Appearently on the 6th row, p.plantcode is suddenly magically an invalid identifier. Why is this? and why are all the others from the exact same table perfectly fine before that point?
The problem is that you are mixing JOINs. You have both implicit and explicit joins. The explicit JOIN syntax with the ON clause has a higher precedence over the implicit join with the commas. As a result the alias for the plant and the offerte tables will not be available in the ON clause. Try using the same JOIN type throughout:
SELECT p.plantnaam, o.levcode, o.offerteprijs
FROM
(
SELECT plantcode , MIN(offerteprijs) AS offprijs
FROM offerte
GROUP BY plantcode
) s
INNER JOIN plant p
ON s.plantcode = p.plantcode
INNER JOIN offerte o
ON s.offprijs = o.offerteprijs
ORDER BY p.plantnaam, l.levcode

Oracle : How to use if then in a select statement

select ma.TITLE,ma.ID as aid,ur.USER_ID
from LEO_MENU_ACTIVITY_RELATION mr
inner join LEO_MENU_MASTER mm on mm.ID=mr.MENU_ID
INNER join LEO_MENUACTIVITY ma on mr.ACTIVITY_ID=ma.ID
LEFT OUTER JOIN LEO_USER_RIGHTS ur on ma.ID=ur.MENU_RELATION_ID and ur.MENU_ID=mm.ID and ur.USER_ID='141'
where mm.ID='1'
UNION (SELECT
'List' as TITLE,
1 as ID,
case (WHEN ur.MENU_RELATION_ID=1 THEN NULL ELSE USER_ID END)as USER_ID
from
LEO_USER_RIGHTS)
In the UNION i want perform a conditional select like if ur.MENU_RELATION_ID=1 then the USER_ID should be selected as NULL otherwise the the original value from the 'LEO_USER_RIGHTS' table must be retrieved.
How can i do this ? Please help
Krishnik
If you want to combine in a UNION something based on the first table I think you can only do it by repeating the whole thing like this:
select ma.TITLE,ma.ID as aid,ur.USER_ID
from LEO_MENU_ACTIVITY_RELATION mr
inner join LEO_MENU_MASTER mm on mm.ID=mr.MENU_ID
INNER join LEO_MENUACTIVITY ma on mr.ACTIVITY_ID=ma.ID
LEFT OUTER JOIN LEO_USER_RIGHTS ur on ma.ID=ur.MENU_RELATION_ID and ur.MENU_ID=mm.ID and ur.USER_ID='141'
where mm.ID='1'
UNION (SELECT
'List' as TITLE,
1 as ID,
case (WHEN ur.MENU_RELATION_ID=1 THEN NULL ELSE USER_ID END)as USER_ID
from
LEO_MENU_ACTIVITY_RELATION mr
inner join LEO_MENU_MASTER mm on mm.ID=mr.MENU_ID
INNER join LEO_MENUACTIVITY ma on mr.ACTIVITY_ID=ma.ID
LEFT OUTER JOIN LEO_USER_RIGHTS ur on ma.ID=ur.MENU_RELATION_ID and ur.MENU_ID=mm.ID and ur.USER_ID='141'
where mm.ID='1'
)
If this is used often I would create a view to avoid duplicate code. In ORACLE (I do not know for other SQL dialects) there is a WITH statement enables you to make a sort of "temporary view".

Resources