Skip to main content
Question

use schema name in default column value expression

  • March 13, 2019
  • 2 replies
  • 16 views

mkheir
Forum|alt.badge.img+1
  • Participating Frequently

Hello Experts,

I need to extract an information from the current schema name and make it a default for a column value.

For example, I have the following schemas:
DAQ1DQ
DAQ2DQ
DAQ3DQ
DBQ1DM

I need to extract the digit at the position 4 and make it the default value for a column.

I tried :
CREATE TABLE t
(
a IDENTITY(1,1) NOT NULL,
b CHAR(10) DEFAULT SUBSTRING(CURRENT_SCHEMA,4,1)
)

but I m getting the following error:
[Code: 7323, SQL State: 42883] [Vertica][VJDBC](7323) ERROR: Meta-functions cannot be used in default expressions

The documentation says that the system information function (including CURRENT_SCHEMA) are supported as expressions.

Source: https://www.vertica.com/docs/9.1.x/HTML/index.htm#Authoring/AdministratorsGuide/Tables/ColumnManagement/ColumnDefaultValue.htm

is there any other way to be able to use current schema name (or part of it) in default expressions.

Many thanks.
Mkheir.

2 replies

marcothesane
Forum|alt.badge.img+1
  • Participating Frequently
  • March 13, 2019

Hi Mkheir

No, there is not, I'm afraid.

And it's logical, in a way: CURRENT_USER, CURRENT_DATABASE don't change for the duration of your session, while CURRENT_SCHEMA [()] will return the first valid schema found in the SEARCH_PATH , which you can change with every SET SEARCH_PATH command as often as you like in your session.

Workaround can only be that you hard-wire '1' or '2', etc into your DEFAULT clause, I'm afraid ...


Jim_Knicely
Forum|alt.badge.img+2
  • Participating Frequently
  • March 13, 2019

A "flattened table" might help?

dbadmin=> CREATE USER jane;
CREATE USER

dbadmin=> CREATE SCHEMA DAQ3DQ;
CREATE SCHEMA

dbadmin=> ALTER USER jane SEARCH_PATH DAQ3DQ;
ALTER USER

dbadmin=> GRANT ALL ON SCHEMA DAQ3DQ TO jane;
GRANT PRIVILEGE

dbadmin=> CREATE TABLE t
dbadmin->    (
dbadmin(>       a IDENTITY(1,1) NOT NULL,
dbadmin(>       b CHAR(10) DEFAULT (SELECT SUBSTR(SPLIT_PART(search_path, ',', 1), 4, 1) FROM users WHERE user_name = user)
dbadmin(>    );
ERROR 7347:  Default queries must refer to tables

Bummer. We have to create a Vertica ROS table:

dbadmin=> \c - dbadmin
You are now connected as user "dbadmin".

dbadmin=> CREATE TABLE public.users_table AS SELECT user_name, search_path FROM users;
CREATE TABLE

dbadmin=> GRANT SELECT ON public.users_table TO jane;
GRANT PRIVILEGE

Now user JANE can create the table:

dbadmin=> \c - jane
You are now connected as user "jane".

dbadmin=> CREATE TABLE t
dbadmin->   (
dbadmin(>     a IDENTITY(1,1) NOT NULL,
dbadmin(>     b CHAR(10) DEFAULT (SELECT SUBSTR(SPLIT_PART(search_path, ',', 1), 4, 1) FROM public.users_table WHERE user_name = user)
dbadmin(>   );
CREATE TABLE

dbadmin=> INSERT INTO t (b) VALUES (DEFAULT);
 OUTPUT
--------
      1
(1 row)

dbadmin=> SELECT * FROM t;
 a |     b
---+------------
 1 | 3
(1 row)