Pass In “WHERE” parameters to PostgreSQL View?

前端 未结 5 1063
后悔当初
后悔当初 2020-12-04 15:43

I have a rather complicated query on my PostgreSQL database spanning 4 tables via a series of nested subqueries. However, despite the slightly tricky looking appearance and

5条回答
  •  -上瘾入骨i
    2020-12-04 16:09

    In most cases the set-returning function is the way to go, but in the event that you want to both read and write to the set, a view may be more appropriate. And it is possible for a view to read a session parameter:

    CREATE VIEW widget_sb AS SELECT * FROM widget WHERE column = cast(current_setting('mydomain.myparam') as int)
    
    SET mydomain.myparam = 0
    select * from widget_sb
    [results]
    
    SET mydomain.myparam = 1
    select * from widget_sb
    [distinct results]
    

提交回复
热议问题