sql - How do I add a null row in Postgres in a generic manner -


i do

select col1, col2 foo union values (null, null) 

but null given default type of text, error "union types [e.g.] integer , text cannot matched". in specific cases can provide types of columns of foo, constructing sql statements programatically , preferable if didn't have carry around column type information me.

is there workaround this?

you can query information_schema table columns using query this:

select column_name, data_type information_schema.columns table_name = 'mytable' 

or can use postgresql specific form:

select attname, atttypid::regtype pg_attribute attrelid = 'public.mytable'::regclass   , attnum > 0 

this give data types columns of interest in table. having this, in automated framework can generate union string add empty row casting nulls required data type, this:

select col1, col2 foo union values (null::varchar, null::integer) 

probably more important question why want empty row? perhaps can around without having synthetic empty row in first place?


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -