on 2021 Sep 21 11:50 AM
Is it possible to have the description of a table as a result set?
Something like this
select * from (describe table mytable)
Request clarification before answering.
Thank you!!
The goal was to generate an insert statements for big tables to be used in a python script, at the end Syscolumns helped me a lot.
create or replace table table1(id1 int, name1 varchar(10));
create or replace table table2(id2 int, name2 varchar(10), surname2 varchar(10));
....
create or replace table table100(id100 int, name100 varchar(10), surname100 varchar(10), depart100 varchar(10));
select 'insert into ' || tname || '(' || list(cols) || ') values (' || list (vals) || ')' from (select tname, cname cols, (if coltype = 'varchar' then '''%S''' else '%S' endif) as vals from sys.syscolumns where tname in ('table1', 'table2', 'table100')) T1 group by tname;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In my understanding, unless you specify an ORDER BY clause, the SQL query engine is free to order result sets - and for LIKE, to order the different entries - to its preference according to the used access plans, i.e. it does not need to order them in any meaningful way.
And I'm quite sure you want column names and values in the "correct order" here.
Thanks, should it then look like this?
select 'insert into ' || tname || '(' || list(cols order by tname, cols) || ') values (' || list (vals order by tname, cols) || ')' from (select tname, cname cols, (if coltype = 'varchar' then '''%S''' else '%S' endif) as vals from sys.syscolumns where tname in ('table1', 'table2', 'table100')) T1 group by tname;
Yes, that's what I mean.
adding order by cols helps to keep both lists in the same order, but both come in a false order (alphabeitcally instead of the real order).
So, the correct statement should look like this:
select 'insert into ' || tname || '(' || list(cols order by colno) || ') values (' || list (vals order by colno) || ')' from (select colno, tname, cname cols, (if coltype = 'varchar' then '''%s''' else '%s' endif) as vals from sys.syscolumns where tname in ('table1', 'table2', 'table100')) T1 group by tname;
The DESCRIBE statement cannot be coded inside a server-side SQL operation because it a client-side ISQL statement.
However, you can SELECT FROM SYS.SYSCOLUMNS to get the same result:
DESCRIBE TABLE rroad_sample_memo; SELECT LEFT ( cname, 32 ) AS "Column", LEFT ( coltype, 32 ) AS "Type", IF nulls = 'N' THEN 0 ELSE 1 ENDIF AS "Nullable", IF in_primary_key = 'N' THEN 0 ELSE 1 ENDIF AS "Primary Key" FROM SYS.SYSCOLUMNS WHERE tname = 'rroad_sample_memo' ORDER BY colno; Column Type Nullable Primary Key -------------------------------- -------------------------------- -------- ----------- sampling_id unsigned int 0 0 sample_set_number unsigned bigint 0 1 memo long varchar 0 0 Column Type Nullable Primary Key -------------------------------- -------------------------------- -------- ----------- sampling_id unsigned int 0 0 sample_set_number unsigned bigint 0 1 memo long varchar 0 0 (3 rows) Execution time: 0.002 seconds
FYI the system catalog views contain vastly more information than any DESCRIBE statement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
47 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.