question

saitaladev_142721 avatar image
saitaladev_142721 asked Erick Ramirez edited

What is the syntax for inserting data into a UDT column?

I had created a UDT as follows:

CREATE TYPE killrvideo.video_encoding (
    bit_rates set<text>,
    encoding text,
    height int,
    width int
);

created table as follows:

CREATE TABLE test(
    name text PRIMARY KEY,
    mydata frozen<video_encoding>
);

trying to insert data:

INSERT INTO test (name, mydata) VALUES ('srini',"{
  {'data', 'ssssecond'}, 'tttttest', 23, 444}");

Getting error as:

SyntaxException: line 1:98 no viable alternative at input ')' (...name, mydata) values('srini',["{
  {'data', 'ssssecond'}, 'tttttest', 23, 444]}")...)

Please assist me on this issue

cqluser-defined type
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered

Hi @saitaladev_142721,

here is the correct syntax:

KVUser@cqlsh> insert into killrvideo.test(name, mydata) values('srini', { bit_rates: {'data', 'ssssecond'}, encoding: 'tttttest', height: 23, width: 444});

KVUser@cqlsh> select * from killrvideo.test ;

 name  | mydata
-------+----------------------------------------------------------------------------------
 srini | {bit_rates: {'data', 'ssssecond'}, encoding: 'tttttest', height: 23, width: 444}

(1 rows)

To insert into a UDT, you need to enclose the user-defined type with curly brackets, separating each key-value pair in the user-defined type by a colon.

Here is the relevant CQL documentation:

https://docs.datastax.com/en/cql-oss/3.3/cql/cql_using/useInsertUDT.html

I hope this helps!

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.