将函数从 Oracle 转换为 SQL Server 时出错
我正在将一个函数从 Oracle 迁移到 SQL Server 2008。此函数会引发函数中包含的 SELECT 语句无法将数据返回到客户端的错误。我该如何解决这个问题?
原始 PLSQL 代码
CREATE OR REPLACE function f_birim_cevrim_katsayi (p_ID_MAMUL in number, p_ID_BIRIMDEN in number, p_ID_BIRIME in number)
return number
is
v_katsayi number;
begin
v_katsayi:=0;
if p_ID_BIRIMDEN!=p_ID_BIRIME then
for c in (
select * from CR_BIRIM_CEVRIM
where ID_MAMUL = p_ID_MAMUL
and (
(ID_BIRIM = p_ID_BIRIMDEN and ID_BIRIM2 = p_ID_BIRIME)
OR ( ID_BIRIM2 = p_ID_BIRIMDEN and ID_BIRIM = p_ID_BIRIME) )
and VALID = 1)
loop
if c.ID_BIRIM=p_ID_BIRIMDEN then
v_katsayi:=c.MT_ORAN;
else
v_katsayi:=1/c.MT_ORAN;
end if;
end loop;
else
v_katsayi:=1;
end if;
return round(v_katsayi,10);
exception
when others then
return 0;
end;
T-SQL 代码:
If Exists ( SELECT name
FROM sysobjects
WHERE name = 'f_birim_cevrim_katsayi'
AND type = 'FN')
DROP FUNCTION f_birim_cevrim_katsayi
GO
CREATE FUNCTION f_birim_cevrim_katsayi
(
@p_ID_MAMUL FLOAT ,
@p_ID_BIRIMDEN FLOAT ,
@p_ID_BIRIME FLOAT
)
RETURNS float
AS
BEGIN
DECLARE @adv_error INT
DECLARE @v_katsayi FLOAT
SELECT @v_katsayi = 0
IF @p_ID_BIRIMDEN != @p_ID_BIRIME
BEGIN
DECLARE cursor_for_inline_select1 CURSOR LOCAL FOR
SELECT *
FROM CR_BIRIM_CEVRIM
WHERE ID_MAMUL = @p_ID_MAMUL
AND ((ID_BIRIM = @p_ID_BIRIMDEN
AND ID_BIRIM2 = @p_ID_BIRIME)
OR (ID_BIRIM2 = @p_ID_BIRIMDEN
AND ID_BIRIM = @p_ID_BIRIME))
AND VALID = 1
OPEN cursor_for_inline_select1
FETCH NEXT FROM cursor_for_inline_select1
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF c.ID_BIRIM = @p_ID_BIRIMDEN
BEGIN
SELECT @v_katsayi = c.MT_ORAN
END
ELSE
BEGIN
SELECT @v_katsayi = 1/c.MT_ORAN
END
END
CLOSE cursor_for_inline_select1
DEALLOCATE cursor_for_inline_select1
END
ELSE
BEGIN
SELECT @v_katsayi = 1
END
DEALLOCATE cursor_for_inline_select1
return ROUND(@v_katsayi, 10)
GOTO ExitLabel1
Exception1:
BEGIN
DEALLOCATE cursor_for_inline_select1
return 0
END
ExitLabel1:
return ROUND(@v_katsayi, 10)
END
GO
I am migrating a function from Oracle to SQL Server 2008. This function raises SELECT
statements included within a function cannot return data to a client as error. How can I solve this problem?
Original PLSQL Code
CREATE OR REPLACE function f_birim_cevrim_katsayi (p_ID_MAMUL in number, p_ID_BIRIMDEN in number, p_ID_BIRIME in number)
return number
is
v_katsayi number;
begin
v_katsayi:=0;
if p_ID_BIRIMDEN!=p_ID_BIRIME then
for c in (
select * from CR_BIRIM_CEVRIM
where ID_MAMUL = p_ID_MAMUL
and (
(ID_BIRIM = p_ID_BIRIMDEN and ID_BIRIM2 = p_ID_BIRIME)
OR ( ID_BIRIM2 = p_ID_BIRIMDEN and ID_BIRIM = p_ID_BIRIME) )
and VALID = 1)
loop
if c.ID_BIRIM=p_ID_BIRIMDEN then
v_katsayi:=c.MT_ORAN;
else
v_katsayi:=1/c.MT_ORAN;
end if;
end loop;
else
v_katsayi:=1;
end if;
return round(v_katsayi,10);
exception
when others then
return 0;
end;
T-SQL code:
If Exists ( SELECT name
FROM sysobjects
WHERE name = 'f_birim_cevrim_katsayi'
AND type = 'FN')
DROP FUNCTION f_birim_cevrim_katsayi
GO
CREATE FUNCTION f_birim_cevrim_katsayi
(
@p_ID_MAMUL FLOAT ,
@p_ID_BIRIMDEN FLOAT ,
@p_ID_BIRIME FLOAT
)
RETURNS float
AS
BEGIN
DECLARE @adv_error INT
DECLARE @v_katsayi FLOAT
SELECT @v_katsayi = 0
IF @p_ID_BIRIMDEN != @p_ID_BIRIME
BEGIN
DECLARE cursor_for_inline_select1 CURSOR LOCAL FOR
SELECT *
FROM CR_BIRIM_CEVRIM
WHERE ID_MAMUL = @p_ID_MAMUL
AND ((ID_BIRIM = @p_ID_BIRIMDEN
AND ID_BIRIM2 = @p_ID_BIRIME)
OR (ID_BIRIM2 = @p_ID_BIRIMDEN
AND ID_BIRIM = @p_ID_BIRIME))
AND VALID = 1
OPEN cursor_for_inline_select1
FETCH NEXT FROM cursor_for_inline_select1
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF c.ID_BIRIM = @p_ID_BIRIMDEN
BEGIN
SELECT @v_katsayi = c.MT_ORAN
END
ELSE
BEGIN
SELECT @v_katsayi = 1/c.MT_ORAN
END
END
CLOSE cursor_for_inline_select1
DEALLOCATE cursor_for_inline_select1
END
ELSE
BEGIN
SELECT @v_katsayi = 1
END
DEALLOCATE cursor_for_inline_select1
return ROUND(@v_katsayi, 10)
GOTO ExitLabel1
Exception1:
BEGIN
DEALLOCATE cursor_for_inline_select1
return 0
END
ExitLabel1:
return ROUND(@v_katsayi, 10)
END
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自创建函数
这意味着这是错误的
您没有将 CURSOR 输出分配给局部变量。
请描述您想要对示例数据执行的操作:我确信这是一个带有 CASE 的简单 SELECT
编辑:除非我的 pl/sql 已关闭,否则您不需要 CURSOR
From CREATE FUNCTION
This means this is wrong
You are not assigning the CURSOR output to local variables.
Please describe what you want to do with sample data: I'm sure this is a simple SELECT with a CASE
Edit: You don't need a CURSOR unless my pl/sql is way off
尝试将这两行更改
为
“错过了其他几行”。无论您在哪里使用 SELECT 简单地设置变量值,请改为 SET...
另外:更改光标以抓取字段并将其放入变量中
Try changing these two lines
to
Missed a couple others. Anywhere you are using SELECT to simply set a variable value, change to SET instead...
Also: change your cursor to grab the field and put it into a variable