开发者

Is it possible to append a WHERE clause in SELECT in plpgsql?

开发者_JAVA百科I have a function with several IN params.

I need to build a complex select and use the params in the WHERE CLAUSE.

Is there any way I can add some conditioning like this:

if ($1 > 0)  then
 condition1 ='col1 = $1';
end if;

SELECT * from table1 WHERE || condition1 ;


You can use dynamic SQL

http://www.postgresql.org/docs/current/static/plpgsql-statements.html


Yes, you can execute dynamic commands using "execute"

if ($1 > 0)  then
 condition1 ='col1 = '||$1::varchar;
end if;

execute 'select * from table1 where '||condition1;

http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜