【问题标题】:Postgres sql table return statementPostgres sql表返回语句
【发布时间】:2021-02-04 06:13:51
【问题描述】:

我有一个关于从 SQL Server 到 Postgres 的翻译的问题。

我得到了一个表返回语句,但我不知道如何将它翻译成 Postgres。

我的代码:

CREATE FUNCTION [dbo].[GetLastUpdatedFluidTreated](@InjectionPoint_ID UNIQUEIDENTIFIER)  
RETURNS @Return TABLE (
    [RT_FluidTreated_ID] [uniqueidentifier] NOT NULL,
    [CL_FluidTreatedType_ID] [uniqueidentifier] NOT NULL,
    [Timestamp] [datetime] NULL,
    [Last_Updated_Time] [datetime] NULL,
    [NewOrModified] [bit] NULL,
    [Origin_ID] [uniqueidentifier] NULL,
    [IsNew] [bit] NULL
    )
AS   
BEGIN  
    

    INSERT INTO @Return
    Select Top 1
        x1.[RT_FluidTreated_ID],
        x1.[CL_FluidTreatedType_ID],
        x1.[Timestamp],
        x1.[Last_Updated_Time],
        x1.[NewOrModified],
        x1.[Origin_ID],
        x1.[IsNew]
    from RT_FluidTreated x1
    WHERE x1.RE_InjectionPoint_ID = @InjectionPoint_ID
    Order by x1.[Last_Updated_Time] desc
    RETURN;
END

上面的这段代码在 sql server 上。 有人能帮我吗?谢谢

【问题讨论】:

    标签: sql-server postgresql database-migration


    【解决方案1】:

    我认为您正在寻找一个简单的集合返回函数:

    create function getlastupdatedfluidtreated(p_injectionpoint_id uuid)  
      returns table (
          rt_fluidtreated_id uuid not null,
          cl_fluidtreatedtype_id uuid not null,
          timestamp timestamp null,
          last_updated_time timestamp null,
          newormodified boolean null,
          origin_id uuid null,
          isnew boolean null)
    as   
    $$
      select
          x1.rt_fluidtreated_id,
          x1.cl_fluidtreatedtype_id,
          x1.timestamp,
          x1.last_updated_time,
          x1.newormodified,
          x1.origin_id,
          x1.isnew
      from rt_fluidtreated x1
      where x1.re_injectionpoint_id = p_injectionpoint_id
      order by x1.last_updated_time desc
      limit 1 --<< replaces TOP 1
    $$
    language sql;
    

    我将使用的数据类型调整为 Postgres 中最合理的类型。您需要将其调整为您在表中使用的实际类型rt_fluidtreated

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2014-01-15
      • 2012-07-07
      • 1970-01-01
      • 2019-01-07
      • 2015-01-05
      相关资源
      最近更新 更多