我如何将一个小查询从 SQL 服务器转换到 Oracle,我不知道如何在保留字中应用它们的等价物?
How can I convert a small query from SQL Server to Oracle, I don't know how to apply their equivalents in reserved words?
我一直使用 SQL Server,现在我想学习 Oracle,如何将这个小查询从 SQL Server 转换到 Oracle?
我在这个查询中有错误:
with cte as, OFFSET @start ROWS FETCH ,
and exists(select 1 from cte where cte.id_city = h.id_city),
(dateadd(second, h.days, h.datehome) >= @from and dateadd(second, h.days, h.datehome) <= @to)
我不知道 Oracle 的等效项是什么。
这是我的完整查询
CREATE OR REPLACE
PACKAGE PKG_HOME AS
CREATE PROCEDURE SP_HOME_DATA (
from in timestamp,
to in timestamp,
start number,
page number
)
AS
BEGIN
with cte as (
select isnull(h.id_city,'') as id_city
from main_bdd.home h
left join second_bdd.owner.clients cl on cl.idHome = h.idHome
where ((dateadd(second, h.days, h.datehome) >= @from and dateadd(second, h.days, h.datehome) <= @to))
order by h.datehome asc
OFFSET @start ROWS FETCH NEXT @page ROWS ONLY
)
select
isnull(h.id_city,'') as id_city
isnull(h.last_ubication,'') as last_ubication
from main_bdd.home h
left join second_bdd.owner.clients cl on cl.idHome = h.idHome
where ((dateadd(second, h.days, h.datehome) >= @from and dateadd(second, h.days, h.datehome) <= @to))
AND exists(select 1 from cte where cte.id_city = h.id_city)
order by h.datehome asc
END;
END PKG_HOME;
你想要这样的东西(尽管 second_bdd.owner.clients
看起来不对,应该是 schema_name.table_name
而不是 schema_name.table_name.something_else
):
CREATE PACKAGE PKG_HOME AS
PROCEDURE SP_HOME_DATA (
i_from IN timestamp,
i_to IN timestamp,
i_start IN number,
i_page IN number,
o_cursor OUT SYS_REFCURSOR
);
END PKG_HOME;
/
CREATE PACKAGE BODY PKG_HOME AS
PROCEDURE SP_HOME_DATA (
i_from IN timestamp,
i_to IN timestamp,
i_start IN number,
i_page IN number,
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor FOR
with cte as (
SELECT h.id_city
FROM main_bdd.home h
left join second_bdd.owner.clients cl
on cl.idHome = h.idHome
WHERE h.day + INTERVAL '1' SECOND * h.datehome
BETWEEN i_from AND i_to
ORDER BY h.datehome asc
OFFSET i_start ROWS FETCH NEXT i_page ROWS ONLY
)
SELECT h.id_city,
h.last_ubication
FROM main_bdd.home h
LEFT JOIN second_bdd.owner.clients cl
on cl.idHome = h.idHome
WHERE h.day + INTERVAL '1' SECOND * h.datehome
BETWEEN i_from AND i_to
AND EXISTS(
select 1 from cte where cte.id_city = h.id_city
)
order by h.datehome asc;
END;
END PKG_HOME;
/
注意:在 Oracle 中,''
和 NULL
是相同的,因此使用 COALESCE(value, '')
(ISNULL
函数的 Oracle 等价物)是相同的value
没有任何包装函数。
db<>fiddle here
我一直使用 SQL Server,现在我想学习 Oracle,如何将这个小查询从 SQL Server 转换到 Oracle?
我在这个查询中有错误:
with cte as, OFFSET @start ROWS FETCH ,
and exists(select 1 from cte where cte.id_city = h.id_city),
(dateadd(second, h.days, h.datehome) >= @from and dateadd(second, h.days, h.datehome) <= @to)
我不知道 Oracle 的等效项是什么。
这是我的完整查询
CREATE OR REPLACE
PACKAGE PKG_HOME AS
CREATE PROCEDURE SP_HOME_DATA (
from in timestamp,
to in timestamp,
start number,
page number
)
AS
BEGIN
with cte as (
select isnull(h.id_city,'') as id_city
from main_bdd.home h
left join second_bdd.owner.clients cl on cl.idHome = h.idHome
where ((dateadd(second, h.days, h.datehome) >= @from and dateadd(second, h.days, h.datehome) <= @to))
order by h.datehome asc
OFFSET @start ROWS FETCH NEXT @page ROWS ONLY
)
select
isnull(h.id_city,'') as id_city
isnull(h.last_ubication,'') as last_ubication
from main_bdd.home h
left join second_bdd.owner.clients cl on cl.idHome = h.idHome
where ((dateadd(second, h.days, h.datehome) >= @from and dateadd(second, h.days, h.datehome) <= @to))
AND exists(select 1 from cte where cte.id_city = h.id_city)
order by h.datehome asc
END;
END PKG_HOME;
你想要这样的东西(尽管 second_bdd.owner.clients
看起来不对,应该是 schema_name.table_name
而不是 schema_name.table_name.something_else
):
CREATE PACKAGE PKG_HOME AS
PROCEDURE SP_HOME_DATA (
i_from IN timestamp,
i_to IN timestamp,
i_start IN number,
i_page IN number,
o_cursor OUT SYS_REFCURSOR
);
END PKG_HOME;
/
CREATE PACKAGE BODY PKG_HOME AS
PROCEDURE SP_HOME_DATA (
i_from IN timestamp,
i_to IN timestamp,
i_start IN number,
i_page IN number,
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor FOR
with cte as (
SELECT h.id_city
FROM main_bdd.home h
left join second_bdd.owner.clients cl
on cl.idHome = h.idHome
WHERE h.day + INTERVAL '1' SECOND * h.datehome
BETWEEN i_from AND i_to
ORDER BY h.datehome asc
OFFSET i_start ROWS FETCH NEXT i_page ROWS ONLY
)
SELECT h.id_city,
h.last_ubication
FROM main_bdd.home h
LEFT JOIN second_bdd.owner.clients cl
on cl.idHome = h.idHome
WHERE h.day + INTERVAL '1' SECOND * h.datehome
BETWEEN i_from AND i_to
AND EXISTS(
select 1 from cte where cte.id_city = h.id_city
)
order by h.datehome asc;
END;
END PKG_HOME;
/
注意:在 Oracle 中,''
和 NULL
是相同的,因此使用 COALESCE(value, '')
(ISNULL
函数的 Oracle 等价物)是相同的value
没有任何包装函数。
db<>fiddle here