如何将值插入 FUNCTION 的 TABLE
How to Insert Value to FUNCTION'S TABLE
我的功能在这里;
CREATE FUNCTION "HS_getMaasHesapla"(EmpId INT,MaasTuru VARCHAR(1),Donem VARCHAR(50),SablonCode VARCHAR(50),
Maas DECIMAL(18,6),Brut DECIMAL(18,6),Pek DECIMAL(18,6),NetEkKazanc DECIMAL(18,6),BrutEkKazanc DECIMAL(18,6),
EkKesinti DECIMAL(18,6),DvrGvMatrah DECIMAL(18,6),SonBrutMaas DECIMAL(18,6),HesaplananMatrah DECIMAL(18,6),
IzinTutar DECIMAL(18,6),MesaiTutar DECIMAL(18,6),Agi DECIMAL(18,6),IsBes VARCHAR(1),IsIcra VARCHAR(1),
donembasi DATE,donemsonu DATE)
RETURNS TABLE
(
EmpId INT, GVMatrah DECIMAL(18,6),Brut DECIMAL(18,6),SGKIsv DECIMAL(18,6),IszCal DECIMAL(18,6),
IszIsv DECIMAL(18,6),SGKCal decimal(18,6),GV decimal(18,6),DV decimal(18,6),Net decimal(18,6),
EkKazanc decimal(18,6),Pek decimal(18,6),IzinTutar decimal(18,6),Bes decimal(18,6),Icra decimal(18,6),
Odenen decimal(18,6),Maliyet decimal(18,6),EkKesinti decimal(18,6),Mesai decimal(18,6)
,IcraOran decimal(18,6),DosyaNumarasi varchar(50)
)
LANGUAGE SQLSCRIPT
SQL SECURITY DEFINER AS
BEGIN
DV := "HS_DVTutar"(:Brut, :donembasi, :donemsonu);
IF (:MaasTuru = 'B') THEN Net := (:Brut) - :SGKCal - :IszCal - :GV - :DV;
EkKazanc := :BrutEkKazanc;
Pek := :Brut + :Pek;
ELSE Net := (:Brut) - :SGKCal - :IszCal - :GV - :DV;
EkKazanc := :BrutEkKazanc + :NetEkKazanc;
Pek := :Brut;
END IF;
Net := round(:Net, 2);
IF (:Icra > 0) THEN Icra := (:Net * :IcraOran) / 100.00;
END IF;
Odenen := :Net + :Agi - :Icra - :Bes - :EkKesinti;
Maliyet := :Brut + :SGKIsv + :IszIsv;
INSERT INTO TABLE("EmpId" , "GVMatrah", "Brut", "SGKIsv", "IszCal", "IszIsv", "SGKCal", "GV", "DV", "Net", "EkKazanc", "Pek", "IzinTutar", "Bes", "Icra", "Odenen", "Maliyet", "EkKesinti", "Mesai", "IcraOran", "DosyaNumarasi") VALUES (round("GvMatrah", 2), round(:Brut, 2), round(:SGKIsv, 2), round(:IszCal, 2), round(:IszIsv, 2), round(:SGKCal, 2), round(:GV, 2), round(:DV, 2), round(:Net, 2), round(:EkKazanc, 2), round(:Pek, 2), round(:IzinTutar, 2), round(:Bes, 2), round(:Icra, 2), round(:Odenen, 2), round(:Maliyet, 2), round(:EkKesinti, 2), round(:MesaiTutar, 2), round(:IcraOran, 2), round(:DosyaNumarasi, 2));
RETURN;
END;
但是我的错误在这里:
Could not find table/view TABLE in schema SBODEMOTR: line 79 col 13 (at pos 3861)
另一方面我不能使用Select;
你能告诉我怎么做吗?
我认为你的消息意味着你需要在你的 SBODEMOTR 架构中创建一个名为 TABLE 的 table,以便能够在你的函数的插入中使用。
此外,根据文档:
The CREATE FUNCTION statement creates read-only functions that are
free of side effects. Neither DDL or DML statements (INSERT, UPDATE,
and DELETE) are allowed in the function body. Also other functions or
procedures selected/called from the body of the function must be read
only.
相反,您的函数 could/should 看起来像这样:
DROP FUNCTION MYFUNCTION;
CREATE FUNCTION MYFUNCTION (Val1 INT, Val2 INT, Val3 INT)
RETURNS TABLE (col1 INT, col2 INT) LANGUAGE SQLSCRIPT AS
BEGIN
IF :Val1 > :Val2 THEN
Val3 := 2 * :Val3;
ELSE
Val3 := -1 * :Val3;
END IF;
RETURN SELECT :Val1 AS col1, :Val2 * :Val3 AS col2 FROM DUMMY;
END;
SELECT * FROM MYFUNCTION(2,3,2);
顺便说一句,您当前的代码缺少一些有效的变量声明。
马哈巴@J.Doe
您的函数 return 是 TABLE。 "TABLE" 不是您要 return
的 table 对象的名称
在您的函数代码中,只需同时使用 RETURN 和 SELECT 语句
这是我使用的示例函数
CREATE FUNCTION "DummySelect"(article int)
RETURNS TABLE
(
Id INT, Type varchar(10)
)
LANGUAGE SQLSCRIPT
SQL SECURITY DEFINER AS
BEGIN
declare myId int;
declare myType varchar(10);
select ARTICLE, "TYPE"
into "MYID", "MYTYPE"
from Article_Types
where "TYPE" = 'AA';
RETURN (select :myId, :myType from dummy);
END;
你在 RETURN 命令中看到,我正在 returning SELECT 的结果集,它具有与 TABLE 定义[=声明的完全相同的类型11=]
不要将 return 插入 table,只需使用 SELECT 到 return
希望对你有帮助
我的功能在这里;
CREATE FUNCTION "HS_getMaasHesapla"(EmpId INT,MaasTuru VARCHAR(1),Donem VARCHAR(50),SablonCode VARCHAR(50),
Maas DECIMAL(18,6),Brut DECIMAL(18,6),Pek DECIMAL(18,6),NetEkKazanc DECIMAL(18,6),BrutEkKazanc DECIMAL(18,6),
EkKesinti DECIMAL(18,6),DvrGvMatrah DECIMAL(18,6),SonBrutMaas DECIMAL(18,6),HesaplananMatrah DECIMAL(18,6),
IzinTutar DECIMAL(18,6),MesaiTutar DECIMAL(18,6),Agi DECIMAL(18,6),IsBes VARCHAR(1),IsIcra VARCHAR(1),
donembasi DATE,donemsonu DATE)
RETURNS TABLE
(
EmpId INT, GVMatrah DECIMAL(18,6),Brut DECIMAL(18,6),SGKIsv DECIMAL(18,6),IszCal DECIMAL(18,6),
IszIsv DECIMAL(18,6),SGKCal decimal(18,6),GV decimal(18,6),DV decimal(18,6),Net decimal(18,6),
EkKazanc decimal(18,6),Pek decimal(18,6),IzinTutar decimal(18,6),Bes decimal(18,6),Icra decimal(18,6),
Odenen decimal(18,6),Maliyet decimal(18,6),EkKesinti decimal(18,6),Mesai decimal(18,6)
,IcraOran decimal(18,6),DosyaNumarasi varchar(50)
)
LANGUAGE SQLSCRIPT
SQL SECURITY DEFINER AS
BEGIN
DV := "HS_DVTutar"(:Brut, :donembasi, :donemsonu);
IF (:MaasTuru = 'B') THEN Net := (:Brut) - :SGKCal - :IszCal - :GV - :DV;
EkKazanc := :BrutEkKazanc;
Pek := :Brut + :Pek;
ELSE Net := (:Brut) - :SGKCal - :IszCal - :GV - :DV;
EkKazanc := :BrutEkKazanc + :NetEkKazanc;
Pek := :Brut;
END IF;
Net := round(:Net, 2);
IF (:Icra > 0) THEN Icra := (:Net * :IcraOran) / 100.00;
END IF;
Odenen := :Net + :Agi - :Icra - :Bes - :EkKesinti;
Maliyet := :Brut + :SGKIsv + :IszIsv;
INSERT INTO TABLE("EmpId" , "GVMatrah", "Brut", "SGKIsv", "IszCal", "IszIsv", "SGKCal", "GV", "DV", "Net", "EkKazanc", "Pek", "IzinTutar", "Bes", "Icra", "Odenen", "Maliyet", "EkKesinti", "Mesai", "IcraOran", "DosyaNumarasi") VALUES (round("GvMatrah", 2), round(:Brut, 2), round(:SGKIsv, 2), round(:IszCal, 2), round(:IszIsv, 2), round(:SGKCal, 2), round(:GV, 2), round(:DV, 2), round(:Net, 2), round(:EkKazanc, 2), round(:Pek, 2), round(:IzinTutar, 2), round(:Bes, 2), round(:Icra, 2), round(:Odenen, 2), round(:Maliyet, 2), round(:EkKesinti, 2), round(:MesaiTutar, 2), round(:IcraOran, 2), round(:DosyaNumarasi, 2));
RETURN;
END;
但是我的错误在这里:
Could not find table/view TABLE in schema SBODEMOTR: line 79 col 13 (at pos 3861)
另一方面我不能使用Select; 你能告诉我怎么做吗?
我认为你的消息意味着你需要在你的 SBODEMOTR 架构中创建一个名为 TABLE 的 table,以便能够在你的函数的插入中使用。
此外,根据文档:
The CREATE FUNCTION statement creates read-only functions that are free of side effects. Neither DDL or DML statements (INSERT, UPDATE, and DELETE) are allowed in the function body. Also other functions or procedures selected/called from the body of the function must be read only.
相反,您的函数 could/should 看起来像这样:
DROP FUNCTION MYFUNCTION;
CREATE FUNCTION MYFUNCTION (Val1 INT, Val2 INT, Val3 INT)
RETURNS TABLE (col1 INT, col2 INT) LANGUAGE SQLSCRIPT AS
BEGIN
IF :Val1 > :Val2 THEN
Val3 := 2 * :Val3;
ELSE
Val3 := -1 * :Val3;
END IF;
RETURN SELECT :Val1 AS col1, :Val2 * :Val3 AS col2 FROM DUMMY;
END;
SELECT * FROM MYFUNCTION(2,3,2);
顺便说一句,您当前的代码缺少一些有效的变量声明。
马哈巴@J.Doe
您的函数 return 是 TABLE。 "TABLE" 不是您要 return
的 table 对象的名称在您的函数代码中,只需同时使用 RETURN 和 SELECT 语句
这是我使用的示例函数
CREATE FUNCTION "DummySelect"(article int)
RETURNS TABLE
(
Id INT, Type varchar(10)
)
LANGUAGE SQLSCRIPT
SQL SECURITY DEFINER AS
BEGIN
declare myId int;
declare myType varchar(10);
select ARTICLE, "TYPE"
into "MYID", "MYTYPE"
from Article_Types
where "TYPE" = 'AA';
RETURN (select :myId, :myType from dummy);
END;
你在 RETURN 命令中看到,我正在 returning SELECT 的结果集,它具有与 TABLE 定义[=声明的完全相同的类型11=]
不要将 return 插入 table,只需使用 SELECT 到 return
希望对你有帮助