如何在PostgreSQL查询中声明变量如何声明变量以便在PostgreSQL 8.3查询中使用?在MSSQLServer中,我可以这样做:DECLARE @myvar INTSET @myvar = 5SELECT *FROM somewhereWHERE something = @myvar如何在PostgreSQL中实现相同的操作?根据文档,变量被简单地声明为“name type;”,但是这给了我一个语法错误:myvar INTEGER;有人能给我举个正确语法的例子吗?
3 回答
Qyouu
TA贡献1786条经验 获得超11个赞
WITH ()
tuple
WITH master_user AS ( SELECT login, registration_date FROM users WHERE ...)SELECT *FROM usersWHERE master_login = (SELECT login FROM master_user) AND (SELECT registration_date FROM master_user) > ...;
慕桂英4014372
TA贡献1871条经验 获得超13个赞
DO $$DECLARE myvar integer;BEGIN SELECT 5 INTO myvar; DROP TABLE IF EXISTS tmp_table; CREATE TABLE tmp_table AS SELECT * FROM yourtable WHERE id = myvar;END $$;SELECT * FROM tmp_table;
以上要求Postgres 9.0或更高版本。
添加回答
举报
0/150
提交
取消