table 类型的删除操作的 postgresql 代码

postgresql code for delete operation in table type

我有一个代码,用于删除 table 中的 MSSQL.Have 类型以转换为 PGSQL,但出现错误。 期待从用户定义的 Table 类型中删除的 PGSQL 代码: 下面的 MSSQL 代码:

declare @Entity TRef_StructureTree readonly //input parameter from procedure
DELETE Tef_StructureTree 
FROM Tef_StructureTree
inner join (select * from @Entity) as source
on Tef_StructureTree.ChildCodeBDR=source.ChildCodeBDR AND 
Tef_StructureTree.ChildScheme=source.ChildScheme;

下面是用户定义的定义Table类型:

CREATE TYPE [dbo].[Tef_StructureTree] AS TABLE(
    [ChildCodeBDR] [numeric](10, 0) NOT NULL,
    [ChildScheme] [nvarchar](100) NOT NULL,
    )

低于 PGSQL 代码:

CREATE OR REPLACE FUNCTION UpdateStrucTree(v_entity Tef_StructureTree[])
 as 
begin
    DELETE FROM v_entity   
        where v_entity."ChildCodeBDR" in(select "source"."ChildCodeBDR" from  unnest(v_entity)  as "source" )
        and v_entity."ChildScheme" in (select "source"."ChildScheme"  from  unnest(v_entity)  as "source" );
 end;

错误:关系“v_entity”不存在 请提供等效的帮助!!

您的参数名为 v_entitydata 而不是 v_entity,因此您需要更改参数名称或对它的引用。您可能想从 table Tef_StructureTree 中删除而不是从传递的数组中删除。

您的函数结构对于 Postgres 也无效,因为函数体需要作为字符串提供,例如使用 dollar quoting.

您也可以只使用一个 sub-query 来简化条件。

所以将所有这些放在一起,函数应该如下所示:

CREATE OR REPLACE FUNCTION UpdateStrucTree(v_entity "Tef_StructureTree"[])
as 
$$
  DELETE FROM "Tef_StructureTree"
  where ("ChildCodeBDR", "ChildScheme") in (select "ChildCodeBDR", "ChildScheme"
                                            from unnest(v_entity) )
$$
language sql;