错误'在sql查询

时间:2018-06-06 06:53:52

标签: sql postgresql

我需要编写一个查询,如果我传递一些字符串列表,那么它将检查特定的表是否存在并返回不存在的表。我写了以下查询

SELECT temp_table.configuration_key FROM (#PLACEHOLDER) temp_table 
LEFT JOIN configuration c ON (c.configuration_key = temp_table.configuration_key) WHERE c.configuration_key IS NULL;

其中#PLACEHOLDER将动态形成,如下所示

1。如果传递单个字符串,则#PLACEHOLDER将

SELECT 'xyz' configuration_key

如果传递了多个字符串,则#PLACEHOLDER将

SELECT 'xyz' configuration_key UNION ALL SELECT 'abc'

等等。

此处第二个查询工作正常,但会向第一个查询提供以下错误

failed to find conversion function from unknown to text 

配置表有一列名为configuration_key

有人能告诉我这里有什么不对吗?如果只传递一个字符串,则如何形成查询(即形成一个包含单个元素的表)。我知道我可以通过整个不同的查询来做到这一点。但我不能使用相同类型的查询吗?

2 个答案:

答案 0 :(得分:1)

我试图在数据库中只记录一个记录时遇到了类似的错误(未能找到从未知到字符变化的转换函数)。我的in-DB_Write工具直接通过自定义查询从in-DB_Connect工具获取输入。我正在使用Redshift Bulk Load驱动程序进行写操作(我看不到将特定的in-DB_Write工具配置为使用常规驱动程序的方法)。批量加载驱动程序使用COPY命令从S3加载。

在阅读您的文章之后,我怀疑in-DB_Write工具假定第一行是标题行,而实际数据从第二行开始。因此,我在in-DB_Connect中对完全相同的SQL进行了合并,并且有效。

总之,这失败了:

Select Coalesce(Max(etl_process_control.process_id), 0) + 1 as process_id
, 1 as job_id
, getdate() as process_start_dttm
, to_timestamp('1899-12-31 00:00:00','YYYY-MM-DD HH24:MI:SS') as process_end_dttm
, 'I' as process_status_cd
, 'In-Progress' as process_status_desc
, null as process_error_rsn
, -999 as rows_updated
, -999 as rows_inserted
, -999 as rows_deleted
, -999 as rows_failed
 From etl_process_control

但这可行:

Select Coalesce(Max(etl_process_control.process_id), 0) + 1 as process_id
, 1 as job_id
, getdate() as process_start_dttm
, to_timestamp('1899-12-31 00:00:00','YYYY-MM-DD HH24:MI:SS') as process_end_dttm
, 'I' as process_status_cd
, 'In-Progress' as process_status_desc
, null as process_error_rsn
, -999 as rows_updated
, -999 as rows_inserted
, -999 as rows_deleted
, -999 as rows_failed
 From etl_process_control
union
Select Coalesce(Max(etl_process_control.process_id), 0) + 1 as process_id
, 1 as job_id
, getdate() as process_start_dttm
, to_timestamp('1899-12-31 00:00:00','YYYY-MM-DD HH24:MI:SS') as process_end_dttm
, 'I' as process_status_cd
, 'In-Progress' as process_status_desc
, null as process_error_rsn
, -999 as rows_updated
, -999 as rows_inserted
, -999 as rows_deleted
, -999 as rows_failed
 From etl_process_control

最后,您会注意到我没有使用联合ALL,因此上述查询实际上只返回一行。这不是理想的解决方案,但是可以避免错误。

答案 1 :(得分:0)

我不确定为什么单行子查询发生此错误,但您可以使用显式类型转换,如

SELECT temp_table.configuration_key 
FROM (SELECT 'xyz'::text configuration_key
) temp_table 
LEFT JOIN configuration c ON (c.configuration_key = temp_table.configuration_key) 
WHERE c.configuration_key IS NULL;

SQL Fiddle