2

I have to create a table with some user defined types and insert data.

I tried this way.

  1. create type type1 as (i int);
  2. create function mycast(int) returns type1 as 'select $1;' language 'sql';
  3. create cast (int as type1) with function mycast(int) as implicit;
  4. create table table_type(t type1);
  5. insert into table_type values(1::type1);

Now the values are getting inserted but wrapped in parenthesis, like this

select * from table_type;

  t  
--------
 (1)

(1 rows)

I believe there might be an error in my function. Can someone help. I wanted basically a function that returns the same output as my input (without any () ).

Thanks.

1 Answer 1

2

The type you defined is a composite type containing one attribute of type int. The text representation of a composite type is parentheses around the attribute list. But judging from your cast function (which is broken, because it returns a scalar and not a composite type), this isn't what you want at all. The best you could do is to define a domain like this:

create domain type1 as int;

Then you don't even need a cast.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for being faster. For reference CREATE DOMAIN
@Peter thanks,but I need to use create type itself. My requirement is to test the create type syntaxes and user defined types.
Then please write a question that describes what you are actually trying to do.
@Peter: I want to create a type, create a table using that type, and insert data into the table. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.