Better alternative
Actually, there is nothing indicating a need for dynamic SQL. All identifiers are stable. Assuming you only want to parameterize the id
value, simplify to:
SELECT name,address from employee where id = $1
INTO variable1, variable2
Or:
SELECT INTO variable1, variable2
e.name, e.address
FROM employee e
WHERE e.id = _my_variable;
The appropriate chapter in the manual: "Executing a Query with a Single-row Result"
Dynamic SQL would be indicated when working with parameterized table or column names.
Here is a list of related question for dynamic SQL
Cause for immediate problem
The reason why your original code did not work, per documentation:
SELECT INTO
is not currently supported withinEXECUTE
; instead, execute a plainSELECT
command and specifyINTO
as part of theEXECUTE
itself.
@a_horse demonstrates valid syntax in his answer.
Either alternative relies on the query to return a single row. If your id is defined unique, that cannot break.