我有一個這樣的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。
提前致謝。
您可以將查詢包含在函數中,同時用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')
應該返回正確的查詢結果。
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras