Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

sql - PostgreSQL save table columns results into variables

I have this function:

DECLARE 
    _first_name text;
    _last_name  text;
BEGIN
SELECT 
    emp.first_name INTO _first_name,
    emp.last_name INTO _last_name,
FROM employee emp LIMIT 1
END;

(I simplified the function to eliminate the information noise)

As you can see, i can get only 1 result because of 'LIMIT'. So i need to store this 2 columns into my 2 variables. But i am getting an error like 'INTO specified more than once'. How to bypass this, any ideas?

question from:https://stackoverflow.com/questions/65840619/postgresql-save-table-columns-results-into-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use INTO once as follows:

SELECT 
    emp.first_name, emp.last_name INTO _first_name, _last_name
FROM employee emp LIMIT 1

Columns and variables must be in the same sequence respectively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...