带有传递变量的SQL函数

时间:2018-01-24 06:24:19

标签: postgresql

这是我的PostgreSQL查询。在这里,我传递硬编码的商家ID(merchant_id = 11)。但我需要从商家ID列表中动态传递一个商家ID。但是我不知道如何使用PostgreSQL函数将id作为变量或数组传递。

   select s.enabled from loyalty_scheme L 
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) WHERE ((SELECT demoting_time_period from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = 11)GROUP BY scheme_id)< to_char((SELECT (DATE_PART('year', CURRENT_DATE::date) - DATE_PART('year', (select max(t.date_time) As maxdat FROM loyalty_scheme L 
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) where (T.unipoint_customer_ref_id = ( select unipoint_customer_ref_id FROM subscribe_merchant where schemerefid = (SELECT scheme_id from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = 11)GROUP BY scheme_id) ) )) ::date)) * 12 +
(DATE_PART('month', CURRENT_DATE::date) - DATE_PART('month', (select max(t.date_time) As maxdat FROM loyalty_scheme L 
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) where (T.unipoint_customer_ref_id = ( select unipoint_customer_ref_id FROM subscribe_merchant where schemerefid = (SELECT scheme_id from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = 11)GROUP BY scheme_id) ) ))::date))),'FM999MI')) 

这是查找商家ID列表的其他查询

select array_agg(DISTINCT merchant.merchant_id)
        from merchant

这是id数组{3,6,7,11,23,32,34,40,41,43}

在第一个代码中我硬编码11,但我需要逐个传递所有这些ID。任何人都知道如何做到这一点。请帮我这样做

1 个答案:

答案 0 :(得分:0)

最后我找到了这样做的方法

这是我的代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigrationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="All"></level>
      <appender-ref ref="AdoNetAppender_Oracle"></appender-ref>
    </root>
    <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
      <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess,Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
      <connectionString value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MYHOST)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MYDBNAME)));User Id=MYUSERID;Password=MYPASSWORD;" />
      <commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
      <bufferSize value="1" />
      <parameter>
        <parameterName value=":log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value=":thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value=":log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value=":logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value=":message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
    </appender>
  </log4net>
</configuration>