Thread: Macro substitution in Postgres

Macro substitution in Postgres

From
"Louise Cofield"
Date:

Greetings -

 

I am trying to create a table, using variables and/or array elements for all required elements of a simple create table statement.  For example, m_array[4][1] holds the value "MyTable", m_array[4][2] holds "col1" m_array[4][3] holds "varchar2", and m_array[4][4] holds "2".

 

I want to be able to create the following: 

"CREATE TABLE" || <value of m_array[1]>  "(" || <value of m_array[2]> || "  "

      || <value of m_array[3] || "(" || <value of m_array[4] || "))"

 

Generated, would be:  "create table MyTable(col1 varchar(2))"

 

Making the macro substitution for the "value of " to work syntactically in the statement that has me scratching my head. 

(The array will be read in a C program, using LIBPQ as the interface.)

 

Thanks!   :-)