[作业]
我编写了一个plpgsql
函数,该函数将表名作为该列中每列的输入,运行一些涉及LIKE
运算符的查询。
对于包含类型为integer
的列的表,我得到以下内容,不足为奇,错误:
ERROR: operator does not exist: integer ~~ unknown
这是有道理的,因为LIKE
对整数类型没有意义。
我可以检查以确保每列的类型不是integer, date, ...
但是硬编码不支持LIKE
操作的数据类型似乎很麻烦,即不是& #34; 文本&#34 ;.此外,DB中使用了许多自定义数据类型来表示某种字符串。
我想知道什么是解决这个问题的好方法。有没有办法检查某种类型的某个列是否支持LIKE
查询?
答案 0 :(得分:1)
不确定是否可以检查一般类型是否支持LIKE运算符,但是如果发生错误,您始终可以运行查询并捕获错误。
LIKE运算符在这种情况下抛出CREATE or replace FUNCTION test() RETURNS void AS
$BODY$
BEGIN
BEGIN -- begin of the exception block
PERFORM 1 LIKE '10'; -- <--this code throws a exception
EXCEPTION
WHEN undefined_function THEN
RAISE WARNING '%', 'could not execute query!';
END; -- end of the exception block
END;
$BODY$
LANGUAGE plpgsql;
错误。您可以在异常块中捕获此错误,而是执行其他操作。请参阅此示例函数:
// ignore the time, just compare the date
if (_lastRun.Date <= DateTime.Now.Date)
{
GetRule();
}
}
有关详情,请参阅documentation和list all error codes