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
Post a Comment