我有一個這樣的sql腳本:
select id,
name,
address,
age,
sex
from table1
left join table2 <condition>
left join table3 <condition>
where name = 'Harry Potter';
有沒有一種方法可以呼叫并運行此腳本,但在不編輯原始腳本的情況下更改感興趣的名稱?例如,我想改為運行此行。
where name = 'Lebron James'
我正在使用 PostgreSQL。
提前致謝。
uj5u.com熱心網友回復:
您可以將查詢包含在函式中,同時用name輸入引數替換字串input_name:
CREATE OR REPLACE FUNCTION test (input_name text)
RETURNS table (id int, name text, address text, age int, sex text)
LANGUAGE sql AS
$$
select id,
name,
address,
age,
sex
from table1
left join table2 <condition>
left join table3 <condition>
where name = input_name ;
$$
然后SELECT * FROM test('Lebron James')應該回傳正確的查詢結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346945.html
標籤:sql PostgreSQL
