midrange.com code scratchpad
Name:
SQL CREATE VIEW with unqualified name goes where?
Scriptlanguage:
SQL
Tabwidth:
4
Date:
05/20/2016 07:32:13 pm
IP:
Logged
Description:
CREATE VIEW using *SYSTEM naming typically puts a CREATE VIEW into the first library in *LIBL, but not always.
Code:
  1. -- set *SYSTEM naming
  2. -- this should say *LIBL
  3. values current schema;
  4.  
  5. -- create a test environment
  6. cl: chgcurlib *crtdft;
  7. cl: rmvlible libsql1;
  8. cl: rmvlible libsql2;
  9.  
  10. cl: dltlib libsql1;
  11. cl: dltlib libsql2;
  12.  
  13. cl: crtlib libsql1;
  14. cl: crtlib libsql2;
  15.  
  16. cl: chglibl (libsql1 libsql2);
  17.  
  18. -- verify *LIBL 
  19. select *
  20. from qsys2.library_list_info;
  21.  
  22. drop view view_one;
  23. drop view view_two;
  24. drop view view_two_subselect;
  25.  
  26. drop table table_master;
  27. drop table table_child1;
  28. drop table table_child2;
  29.  
  30. -- at this point, there is no *CURLIB, CURRENT SCHEMA is *LIBL
  31. -- where will this table go?
  32. create table table_master
  33.   (id int,
  34.    text char(25));
  35.  
  36. -- it went into QGPL, as expected
  37. select *
  38. from systables
  39. where table_name = 'TABLE_MASTER';
  40.  
  41. -- delete the QGPL copy and put it into one of the test libraries
  42. drop table qgpl.table_master;
  43.  
  44. -- qualify these tables' creation
  45. create table libsql1.table_master
  46.   (id int,
  47.    text char(25),
  48.    foreign_key int);
  49.  
  50. create table libsql1.table_child1
  51.   (id int,
  52.    text char(25));
  53.    
  54. create table libsql2.table_child2
  55.   (id int,
  56.    text char(25));
  57.  
  58. -- now, what does the library list look like?
  59. -- no change.
  60. select *
  61. from qsys2.library_list_info;
  62.  
  63. -- create a simple view; one table only
  64. create view view_zero
  65.   (id, text)
  66.   as (select id, text from table_master);
  67.  
  68. -- create a view where both tables are in the same library
  69. create view view_one
  70.   (m_id, m_text, c_id, c_text)
  71.   as (select m.id, m.textm.foreign_key, c.text
  72.     from table_master m
  73.     left join table_child1 c on m.foreign_key = c.id);
  74.     
  75. -- where is it?
  76. -- first library in *LIBL - LIBSQL1
  77. select *
  78. from sysviews
  79. where table_name = 'VIEW_ONE';
  80.  
  81. -- now make a view where the child is in another library
  82. create view view_two
  83.   (m_id, m_text, c_id, c_text)
  84.   as (select m.id, m.textm.foreign_key, c.text
  85.     from table_master m
  86.     left join table_child2 c on m.foreign_key = c.id);
  87.     
  88. -- where is it?
  89. -- first library in *LIBL - LIBSQL1
  90. select *
  91. from sysviews
  92. where table_name = 'VIEW_TWO';
  93.  
  94. -- OK, so it's not going into LIBSQL2 if there's a JOIN in 2...
  95. -- how about if there's a subselect in 2?
  96. create view view_two_subselect
  97.   (m_id, m_text, c_id, c_text)
  98.   as (select m.id, m.textm.foreign_key, (select text from table_child2)
  99.     from table_master m);
  100.  
  101. -- where is it?
  102. -- second library in *LIBL - LIBSQL2
  103. -- BOOM!
  104. select *
  105. from sysviews
  106. where table_name like 'VIEW_%';
  107.  
  108. -- what's my library list?
  109. -- no change
  110. select *
  111. from qsys2.library_list_info;
  112.  
  113. -- still *LIBL
  114. values current schema;
  115.  
© 2004-2019 by midrange.com generated in 0.027s valid xhtml & css