如何在列名列表周围添加单引号?

时间:2015-12-14 19:52:41

标签: postgresql

我有一张几列的表格。我通过将其作为array_agg然后将其作为array_to_string来获取列的名称。如下所示:

"hospitalaccountrecord,locationname,patientkey,inpatientadmitdatetime,readmission,no_null_days_btw_admissions,cohort_assignment,admit_mon_feb,admit_mon_mar,admit_mon_apr,admit_mon_may,admit_mon_june,admit_mon_july,admit_mon_aug,admit_mon_sep,admit_mon_oct,a (...)"

我使用的代码是:

select array_to_string(array_agg(column_name::text), ',') 
from 
(
select column_name 
from 
information_schema.columns 
where table_schema='a' and
table_name = 'b'
 order by columns.ordinal_position
) as v;

我要找的是同样的东西,但每个列名都应该用''括起来。喜欢

'hospitalaccountrecord','locationname','patientkey' 

等等。

2 个答案:

答案 0 :(得分:1)

select string_agg(quoted_column_name, ',') 
from 
(
select '''' || column_name || '''' as quoted_column_name
from 
information_schema.columns 
where table_schema='a' and
table_name = 'b'
 order by columns.ordinal_position
) as v;

答案 1 :(得分:1)

不仅需要两个SELECT

    select string_agg(''''||quoted_column_name||'''', ',')

应该做的。