Postgres - 传递表名作为参数,并将结果存储在文件中 [英] Postgres - Passing table name as parameter and store result in file

查看:179
本文介绍了Postgres - 传递表名作为参数,并将结果存储在文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    Create Or Replace Function totalRecords (tablename TEXT) Returns integer as $total$
    Declare
      total integer;

      Begin
       select count  (*)  into  total  from''|| tablename ||' 'where now() - cast(date_dimension_year||'-'||date_dimension_month||'-'||date_dimension_day AS date) < INTERVAL '3 months' ;
        RETURN total;
       END;
$total$ LANGUAGE plpgsql;

我有一个任务是创建一个函数,应该输出结果到一个文本文件。上面的粘贴代码是我一直在玩,没有成功。我得到语法错误....任何人都可以指导我这个?我使用postgres DB。

i have a task which is to create a function which checks the DB for records on given condition if satisfied should output the result to a text file.The above pasted code is what i have been playing around with no sucess.I get syntax error....could anyone guide me on this? I am using postgres DB.

推荐答案

我以这种方式排序问题

 Begin
 execute 'select count(*) from ' ||tablename||
' where cast(date_dimension_year || ''-'' || date_dimension_month || ''-''||date_dimension_day as date) 
not between (current_date - interval ''13 months'') and current_date' into total ;

我必须指定整数总和结束,正确关闭引号。

I had to assign the integer total towards the end and proper closing of quotation (') worked for me.( I just pasted a portion of the function where i was getting issue)

感谢您的帮助。

这篇关于Postgres - 传递表名作为参数,并将结果存储在文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆