我有一个包含许多表的Postgresql数据库,其中一些表有一个名为'description'的列。其中一些描述包含“狗”这个词。如何在“描述”列中的任何位置打印字符串“dog”的表的表名,不区分大小写?
我尝试使用脚本,但错误
$$ LANGUAGE plpgsql
ERROR: syntax error at or near "END"
LINE 16: END LOOP;**
这是剧本:
CREATE OR REPLACE FUNCTION findAllDogsInDescription()
RETURNS VOID
AS $$
DECLARE
my_row RECORD;
a int;
i boolean;
BEGIN
FOR my_row IN
SELECT table_name from information_schema.columns where column_name = 'description'
LOOP
execute 'select count(*) from ' || my_row.table_name || ' where description ilike ''%dog%'' ' into i;
if (i > 0) THEN
raise 'Found a dog in the description of the table %', my_row.table_name;
END IF
END LOOP;
END;
$$ LANGUAGE plpgsql;
SELECT findAllDogsInDescription();
答案 0 :(得分:1)
这是一个简单的语法错误。您只需;
后END IF
。您需要在a
声明中使用int
(i
}代替execute ... into
。
CREATE OR REPLACE FUNCTION findAllDogsInDescription()
RETURNS VOID
AS $$
DECLARE
my_row RECORD;
a int;
i boolean;
BEGIN
FOR my_row IN
SELECT table_name from information_schema.columns where column_name = 'description'
LOOP
execute 'select count(*) from ' || my_row.table_name || ' where description ilike ''%dog%'' ' into a;
if (a > 0) THEN
raise 'Found a dog in the description of the table %', my_row.table_name;
END IF;
END LOOP;
END;
$$ LANGUAGE plpgsql;
SELECT findAllDogsInDescription();