midrange.com code scratchpad
Name:
Generate RPG DS from a table
Scriptlanguage:
SQL
Tabwidth:
4
Date:
07/19/2022 03:42:08 pm
IP:
Logged
Description:
An SQL statement that can be used to generate either
> a comma separated list of column names
> a free format RPG Data structure
using either the short or long columns names
Code:
  1. -- This SQL statement can be used to generate either
  2. --   > a comma separated list of column names
  3. --   > a free format RPG Data structure 
  4. -- using either the short or long columns names
  5. --
  6. --  to use:
  7. --    simply point it to the table of interest by replacing 
  8. --         MYTABLE and MYLIB with appropriate names
  9. --    uncomment one of the SELECT at the bottom corresponding to
  10. --      the type of list you want to generate
  11. with selected (system_table_name, system_table_schema) 
  12.   as (values ('MYTABLE','MYLIB') )
  13. , tbl as (
  14. select 
  15.         case
  16.            when data_type = 'DECIMAL' then 'packed'
  17.            when data_type = 'NUMERIC' then 'zoned'
  18.            when data_type = 'TIMESTMP' then 'timestamp'
  19.            when data_type = 'INTEGER' then 'int'
  20.            else lower(data_type)  
  21.          end
  22.         concat case
  23.                  when data_type = 'INTEGER' and length >= 8 then '(20'
  24.                  when data_type = 'INTEGER' and length >= 4 then '(10'
  25.                  when data_type = 'INTEGER' and length >= 2 then '(5'
  26.                  when data_type in ('TIMESTMP','DATE','TIME'then '' 
  27.                  else '(' concat length
  28.                end
  29.         concat case 
  30.                  when numeric_scale is null then ''
  31.                  when data_type = 'INTEGER' then ''
  32.                  else ':' concat numeric_scale
  33.                end
  34.         concat case
  35.                  when data_type in ('TIMESTMP','DATE','TIME'then ';'   
  36.                  else ');' 
  37.                end 
  38.         as rpg_type 
  39. ,system_column_name, length, numeric_scale,
  40. column_text, column_name, ordinal_position
  41. from qsys2.syscolumns syscolumns
  42.  join selected using(system_table_name,system_table_schema)
  43. )
  44. -- build RPG DS
  45. , rpg_ds as (
  46.  select lower(system_column_name) concat ' ' concat rpg_type as rpg_ds_subfield
  47.    from tbl
  48.  order by ordinal_position
  49. )
  50. -- build rpg long name ds
  51. , rpg_ds_long_name as (
  52.  select lower(column_name) concat ' ' concat rpg_type as rpg_ds_subfield
  53.    from tbl
  54.  order by ordinal_position
  55. )
  56. ---- build a string of all columns in the table using short names
  57. , list_short_names as (
  58. select listagg(lower(trim(system_column_name)),', '
  59.     within group (order by ordinal_position)
  60. from tbl
  61. )
  62. ---- build a string of all columns in the table using short names
  63. , list_long_names as (
  64. select listagg(lower(trim(column_name)),', '
  65.     within group (order by ordinal_position)
  66. from tbl
  67. )
  68. select * from list_short_names;
  69. --select * from list_long_names;
  70. --select * from rpg_ds_long_name;
  71. --select * from rpg_ds;
© 2004-2019 by midrange.com generated in 0.04s valid xhtml & css