Does Postgres Plus Advanced Server (PPAS) 9.5 support altering (dropping/adding) attributes to composite object types? - composite-types

According to my reading of the PostgreSQL documentation on ALTER TYPE I should be able to do the following:
CREATE TYPE compfoo AS OBJECT (f1 int, f2 text);
CREATE TYPE compbar AS OBJECT (f1 int, f2 compfoo);
ALTER TYPE compbar DROP ATTRIBUTE f2;
ALTER TYPE compbar ADD ATTRIBUTE f2 varying(1000);
ALTER TYPE compbar ADD ATTRIBUTE f3 compfoo;
However, when I try it, I get the following error:
ERROR: ALTER TYPE ADD/DROP COLUMN statement not supported for object types
What am I missing here? Is there a server configuration that will allow me to do this? Is it because the type in question is a composite OBJECT type, as opposed to a composite of 'basic' types?
UPDATE 2015-02-10
I've updated the title, and the samples to more closely resemble the problem at hand. In short, within Postgres Plus Advanced Server, there's additional syntax for creating types, in the form CREATE TYPE foo AS OBJECT <--- that syntax is the root of the issue. You cannot alter attributes when the composite type was created AS OBJECT.

CREATE TYPE compfoo AS (f1 int, f2 text);
CREATE TYPE combbar AS (f1 int, f2 compfoo);
ALTER TYPE compbar DROP ATTRIBUTE f2;
---------------^ maybe you should write combbar?
To make it clear you are defining a type named "combbar" and then you try to alter a type with the name "compbar".
UPDATE
Besides that typo, the SQL seems to run fine.

Related

Cannot find data type user defined data type

I am getting Column, parameter, or variable #1: Cannot find data type dbo.SUBSYSTEM_CODE. error on user datatype.
CREATE TABLE #PREDEFINED_SUBSYSTEMS
(
SUBSYSTEM_CODE dbo.SUBSYSTEM_CODE PRIMARY KEY
);
After I checked user defined datatype I can see. I am using SQL 2012 and also I applied set compatibility_level = 110 on datatype still didn't work.
What other alternatives I have to fix this?
Probably you need to define type inside tempdb:
USE tempdb;
GO
CREATE TYPE dbo.SUBSYSTEM_CODE ...
Our problem was with schema
we moved table type from dbo to hub schema, and after that we got this error,
you need to use schema before table name for it to work
and dont forget about dynamic sqls which you might have

Oracle SQL: rename object attribute

Looking at the documentation for the ALTER TYPE statement, there seems to be no possibility to change the name, only the type. Is there any possibility to rename an attribute of an SQL object?
My first thought would be to first create an attribute with the new name, copy the data, and then drop the old attribute, but I hope there is a better solution, something like alter type my_type rename attribute abc to xyz.
I believe you can use CREATE OR REPLACE TYPE [type_name]

How can I alter a UDT in HSQLDB?

In HSLQDB v 2.3.1 there is a create type clause for defining UDTs. But there appears to be no alter type clause, as far as the docs are concerned (and the db returns a unexpected token error if I try this).
Is it possible to amend/drop a UDT in HSQLDB? What would be the best practice, if for example I originally created
create type CURRENCY_ID as char(3)
because I decide I'm going to use ISO codes. But then I actually decide that I'm going to store the codes as integers instead. What is the best way to modify the schema in my db? (this is a synthetic example, obviously I wouldn't use integers in this case).
I guess I might do
alter table inventory alter column ccy set data type int
drop type CURRENCY_ID
create type CURRENCY_ID as int
alter table inventory alter column ccy set data type CURRENCY_ID
but is there a better way to do this?
After trying various methods, I ended up writing a script to edit the *.script file of the database directly. It's a plain text file with SQL commands that recreates the DB programmatically. In detail:
open db, shutdown compact
Edit the script file: replace the type definition, e.g. create type XXX as int to create type XXX as char(4)
For each table, replace the insert into table XXX values (i,...) with insert into table XXX values('str',...). This was done with a script that had the mappings from the old (int) value into the new (char) value.
In my particular case, I was changing a primary key, so I had to remove the identity directive from the create table statement, and also I had to remove a line that had a alter table XXX alter column YYY restart sequence 123.
save and close script file, open db, shutdown compact
This isn't great, but it worked. Advantages :
Ability to re-define UDT.
Ability to map the table values programmatically.
Method is generic and can be used for other schema changes, beside UDTs.
Cons
No checking that schema is consistent (although it does throw up errors if it can't read the script).
Dangerous when reading file as a text file. e.g. what if I have a VARCHAR column with newlines in it? When I parse the script file and write it back, this would need to be escaped.
Not sure if this works with non-memory DBs. i.e. those that don't only have a *.script file when shutdown.
Probably not efficient for large DBs. My DB was small ~ 1MB.

How to reference CLR UDTs when creating a table

I have no idea why there is very little documentation on this so I'll ask here.
I have created some user defined data types and would like to use them when creating a table.
However I have no idea what the syntax is for calling to them.
Yes I know it's best to STAY AWAY from them but I'm in a position where I am forced to work with them.
You use User Defined Types (UDTs) just like the built-in types.
Defining UDT Tables and Columns
There is no special syntax for creating a UDT column in a table. You
can use the name of the UDT in a column definition as though it were
one of the intrinsic SQL Server data types.
The following CREATE TABLE Transact-SQL statement creates a table
named Points, with a column named ID, which is defined as an int
identity column and \ the primary key for the table. The second column
is named PointValue, with a data type of Point. The schema name used
in this example is dbo. Note that you must have the necessary
permissions to specify a schema name. If you omit the schema name, the
default schema for the database user is used.
CREATE TABLE dbo.Points
(
ID int IDENTITY(1,1) PRIMARY KEY,
PointValue Point
)
Registering User-Defined Types in SQL Server

HSQL - dropping/creating types

This should be simple but have yet to find an answer.
I want to create a type A only if it does not already exists and/or drop the type if it already exists and recreate it at the startup of my HSQL database.
Now I know the drop and create commands:
CREATE TYPE myType as VARCHAR(100)
DROP TYPE myType
However I have yet to discern how to check for the existence of the type.
Actually turned out to be very simple:
DROP TYPE myType IF EXISTS