I have this query in TSQL and i want to convert it into LINQ.
Please help me to convert this one. Thank you in advance. :)
enter code here
declare #EmployeeId as varchar(10)
set #EmployeeId = 'P24710002'
SELECT [HCIS_ID]
WHERE [OHCD_EmployeeInfo].[HCIS_ID]=[OHCD_PersonalData].HCIS_ID)
as ImmediateSupervisor
,[LastName]
,[FirstName]
,[MiddleName]
FROM [OHCD_PersonalData]
Where (((HCIS_ID in
(Select HCIS_ID From OHCD_employeeInfo Where ImmediateSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)))
or
(HCIS_ID in (Select HCIS_ID From OHCD_employeeInfo Where NextLevelSupervisor
= (Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId))))
or (hcis_id in (select hcis_id from ohcd_employeeinfo where
(ImmediateSupervisor = (Select HCIS_ID From OHCD_employeeInfo Where
EmployeeID = #EmployeeId)
or NextLevelSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)))
or hcis_id in (select hcis_id from ohcd_employeeinfo Where
ImmediateSupervisor in
(select hcis_id from ohcd_employeeinfo where (ImmediateSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where
EmployeeID = #EmployeeId)
or NextLevelSupervisor = (Select HCIS_ID From OHCD_employeeInfo
Where EmployeeID = #EmployeeId))))
or hcis_id in (select hcis_id from ohcd_employeeinfo Where NextLevelSupervisor
in (select hcis_id from ohcd_employeeinfo where (ImmediateSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)
or NextLevelSupervisor = (Select HCIS_ID From OHCD_employeeInfo
Where EmployeeID = #EmployeeId))))
or hcis_id in (select hcis_id from ohcd_employeeinfo Where ImmediateSupervisor
in (select hcis_id from ohcd_employeeinfo Where NextLevelSupervisor
in (select hcis_id from ohcd_employeeinfo where (ImmediateSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)
or NextLevelSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)))))
or hcis_id in (select hcis_id from ohcd_employeeinfo Where NextLevelSupervisor
in (select hcis_id from ohcd_employeeinfo Where NextLevelSupervisor
in (select hcis_id from ohcd_employeeinfo where (ImmediateSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)
or NextLevelSupervisor =
(Select HCIS_ID From OHCD_employeeInfo Where EmployeeID = #EmployeeId)))))))
The main reason is the hierarchy of the
employee from Manager upto his/her subordinates.
Related
I need to save values from cte into variables in the procedure.
My example:
WITH test_cte AS (
SELECT *
from(
SELECT date_a,lastName,firstName,birthDate, rank() over(ORDER BY date_a desc) rnk
FROM test_table a
join test_table b ON b.id = a.bid
)a1
WHERE rnk =1)
SELECT count(*) into count_a
FROM test_table a
join test_table b ON b.id = a.bid
WHERE a.code = code_name;
IF count_a > 0 THEN
SELECT date_a,lastName,firstName,birthDate
into date_a_var,lastName_var,firstName_var,birthDate_var
FROM test_cte;
ELSE
date_a_var := NULL;
lastName_var := NULL;
firstName_var := NULL;
birthDate_var := NULL;
END IF;
but when I try to compile it I'm getting next error:
PL / SQL: ORA-00942: table or view does not exist
on FROM test_cte;
What can I do to solve this problem?
In your code:
-- Start of first SELECT statement.
WITH test_cte AS (
SELECT *
from(
SELECT date_a,lastName,firstName,birthDate, rank() over(ORDER BY date_a desc) rnk
FROM test_table a
join test_table b ON b.id = a.bid
)a1
WHERE rnk =1
)
SELECT count(*) into count_a
FROM test_table a
join test_table b ON b.id = a.bid
WHERE a.code = code_name;
-- End of first SELECT statement.
IF count_a > 0 THEN
-- Start of second SELECT statement.
SELECT date_a,lastName,firstName,birthDate
into date_a_var,lastName_var,firstName_var,birthDate_var
FROM test_cte;
-- End of second SELECT statement.
ELSE
date_a_var := NULL;
lastName_var := NULL;
firstName_var := NULL;
birthDate_var := NULL;
END IF;
It won't work because the test_cte only exists for the one statement and when you finish the statement's final SELECT statement then it no longer exists for the subsequent statements. (However, you do not use the sub-query factoring clause [a.k.a. CTE] in the SELECT for that statement so it is not clear why you need the WITH clause.)
Instead of trying to use COUNT, just get the data and handle the NO_DATA_FOUND exception if it occurs (also, from Oracle 12, you don't need to use RANK and can use FETCH FIRST ROW WITH TIES instead):
DECLARE
date_a_var test_table.date_a%TYPE;
lastName_var test_table.lastname%TYPE;
firstName_var test_table.firstname%TYPE;
birthDate_var test_table.birthdate%TYPE;
BEGIN
BEGIN
SELECT date_a,
lastName,
firstName,
birthDate
INTO date_a_var,
lastName_var,
firstName_var,
birthDate_var
FROM test_table a
join test_table b ON b.id = a.bid
ORDER BY date_a DESC
FETCH FIRST ROW WITH TIES;
EXCEPTION
WHEN NO_DATA_FOUND THEN
date_a_var := NULL;
lastName_var := NULL;
firstName_var := NULL;
birthDate_var := NULL;
END;
-- Continue processing
END;
(Note: You may also get a TOO_MANY_ROWS exception if there are duplicate dates. Either use FETCH FIRST ROW ONLY or, before Oracle 12, the ROW_NUMBER analytic function.)
I have the following code:
WHILE (v_Count > 0)
LOOP
Select v_Columns || CHR(10) || CHR(9) || ColumnName || ',' ,
v_Values || CHR(10) || CHR(9) ||
CASE ISNUMERIC(Val)
WHEN 1 THEN Val
WHEN 0 THEN '''' || REPLACE(LTRIM(RTRIM(Val)), '''','''''') || ''''
END
|| ',' into v_Columns,v_Values
FROM
(
SELECT D.TableName, D.ColumnName, D.Val
FROM
(
SELECT ID, TableName, ColumnName, Val
FROM tblTemplates_Load_OtherObjects_Raw OO
JOIN tblTemplates_Fields F ON OO.OtherObjectsField = F.FieldName
WHERE TemplateType IN ('All', v_TemplateType)
AND OO.Val IS NOT NULL
AND (TemplateVersion = p_TemplateVersion or (TemplateVersion is null
and p_TemplateVersion <> 'V8'))
UNION
SELECT FieldID, TableName, ColumnName, Val
FROM tblTemplates_Fields_OtherDestinations OD
JOIN
(
SELECT ID, Val
FROM tblTemplates_Load_OtherObjects_Raw OO
JOIN tblTemplates_Fields F ON OO.OtherObjectsField = F.FieldName
WHERE TemplateType IN ('All', v_TemplateType)
AND OO.Val IS NOT NULL
AND (TemplateVersion = p_TemplateVersion or (TemplateVersion is
null and p_TemplateVersion <> 'V8'))
) UsedIDs
ON OD.FieldID = UsedIDs.ID
) D
JOIN USER_TAB_COLS C ON upper(D.TableName) = upper(C.TABLE_NAME) AND
upper(D.ColumnName) = upper(C.COLUMN_NAME)
WHERE (v_UpdateComp = 0 OR D.TableName <> 'tblComp')
AND (v_UpdateCompInd = 0 OR D.TableName <> 'tblCompInd')
ORDER BY D.TableName, D.ColumnName
)
WHERE TableName = v_TableName ;
v_Count := v_Count -1;
end loop;
where v_count is a number and can range in values from 5 to 6.
The issue here is that because of the looping, more than one value is tried to be stored into v_Columns and v_Values and getting exact fetch returns more than requested number of rows error.
I am unable to figure out how to rewrite this piece of code so that it works fine.
How can i rewrite this code?
To me, the most suspicious thing is UNION which - if both SELECTs return at least a row - produce two rows. You can't fit two rows into those variables and got the error.
What to do? If possible, rewrite it to a cursor FOR loop which will return row-by-row.
SELECT DISTINCT(UI.TABLE_NAME)
,UI_INDEX_NAME
FROM USER_INDEXES UI
INNER JOIN USER_TABLES UT ON UT_TABLE_NAME = UI.TABLE_NAME
INNER JOIN TMP ON TMP.TABLE_NAME = UI.TABLE_NAME
INNER JOIN USER_CONS_COLUMNS UC ON UC.TABLE_NAME = UI.TABLE_NAME
WHERE UT.IOT_TYPE IS NULL
AND UC.COLUMN_NAME IN (
'STAFF_ID'
,'STAFF_UNIQUE_ID'
)
AND UC.TABLE_NAME LIKE 'HR_%'
OR UC.TABLE_NAME = 'PAYROLL';
Above table query the list of indexes referring to the table that having PK as staff_id or staff_unique_id from the table name PAYROLL and HR_*. TMP is the table that maintain these list of table name, as I don't want it to search through entire USER_TABLE or USER_INDEX.
I have no issue when manually select it, however error encountered:
ORA-02270: no matching unique or primary key for this column-list
in PL/SQL while selecting the result and doing INDEX REBULD
The result return seem incorrect, many redundant INDEX_NAME that point to same table. I put a DISTINCT for UI.TABLE_NAME to solve the duplicate. But in PLSQL, i do not need to populate UI.TABLE_NAME, so how can the error come from?
--index rebuild
FOR r IN (....) LOOP
l_sql := 'ALTER INDEX '||r.index_name||' REBUILD';
EXECUTE IMMEDIATE l_sql;
END LOOP;
--disable constraint
FOR m IN (SELECT UC.CONSTRAINT_NAME ,UC.TABLE_NAME FROM USER_CONSTRAINTS UC
INNER JOIN TMP ON UC.TABLE_NAME = TMP.TABLE_NAME
WHERE UC.TABLE_NAME = TMP.TABLE_NAME AND UC.CONSTRAINT_TYPE IN
('R', 'C', 'U') AND UC.TABLE_NAME LIKE 'HR_%' OR UC.TABLE_NAME =
'PAYROLL') LOOP
l_sql := 'ALTER TABLE '||m.TABLE_NAME||' DISABLE CONSTRAINT
'||m.CONSTRAINT_NAME||' CASCADE';
EXECUTE IMMEDIATE l_sql;
END LOOP;
--enable constraint
FOR n IN (SELECT UC.CONSTRAINT_NAME ,UC.TABLE_NAME FROM USER_CONSTRAINTS UC
INNER JOIN TMP ON UC.TABLE_NAME = TMP.TABLE_NAME
WHERE UC.TABLE_NAME = TMP.TABLE_NAME AND UC.CONSTRAINT_TYPE IN
('R', 'C', 'U') AND UC.TABLE_NAME LIKE 'HR_%' OR UC.TABLE_NAME =
'PAYROLL') LOOP
l_sql := 'ALTER TABLE '||n.TABLE_NAME||' ENABLE CONSTRAINT
'||n.CONSTRAINT_NAME||'';
EXECUTE IMMEDIATE l_sql;
END LOOP;
is it possible to define a select statement as variable, somethings like this:
Define (Select t1.id, t1.var1, t1.var2, t2.id, t2.var1, t2.var2 From
table1 t1, table2 t2 Where t1.id = t2.id) as namevariable
Thanks for your help,
Andrea
Define (Select t1.id, t1.var1, t1.var2, t2.id, t2.var1, t2.var2 From
table1 t1, table2 t2 Where t1.id = t2.id) as namevariable
You can do it as :
DECLARE
var VARCHAR2 (100);
v_emp_id number;
BEGIN
--Defining as a variable in PLSQL
var := 'Select employee_id from employee where employee_id = :1';
EXECUTE IMMEDIATE var into v_emp_id using 1 ;
--Showing the result
DBMS_OUTPUT.PUT_LINE(v_emp_id);
END;
simply with select ... into ...
DECLARE
my_id NUMBER;
my_val VARCHAR2(32);
BEGIN
SELECT t1.id, t1.val into my_id, my_val from my_table t1 where... ;
END;
declare
v_cnt NUMBER;
C SYS_REFCURSOR;
TMP_TBL_NM VARCHAR2(100);
stmt VARCHAR2(1000);
the_name varchar2 (50);
cursor c_table is
(SELECT a.table_name
--, a.column_name, a.constraint_name, c.owner,
--c.r_owner , c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name
WHERE c.owner like '%LTR' and c_pk.table_name like '%EnumerationValue%');
begin
stmt := 'SELECT table_name FROM ' || TMP_TBL_NM || ' ORDER BY 1';
OPEN C FOR stmt;
for t in C
loop
FETCH C INTO the_name;
EXIT WHEN C%NOTFOUND;
--my query for each table goes here
end loop;
end;
i want to use table names in loop as to check the records as per my query.
it is giving me error only by getting table name in the loop.
how can i get table name in a loop so that i can fetch rows as per my requirement from each table in a loop.
thanks in advance.
I'm not entirely sure what you want to achieve here, but if it's just the table names you're interested in, you can make your code a lot less complicated doing it this way:
declare
cursor c_table is
SELECT a.table_name
--, a.column_name, a.constraint_name, c.owner,
--c.r_owner , c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name
begin
for t in c_table loop
--do something with the table name
dbms_output.put_line(t.table_name);
end loop;
end;
This kind of for loop also handles the opening and closing of the cursor and is the recommended way to do these kind of operations.