IBM i Global

 View Only
Expand all | Collapse all

Files - fields queries

  • 1.  Files - fields queries

    Posted 9 days ago

    Hi,

    I have below queries with respect to creating a file:-

    1) Suppose if my field name and record format name is too long then how can i fit it into Functions under DDS for below case( I am referring this link :-When to specify REF and REFFLD keywords for DDS files - IBM Documentation)

    5: FIELD6 has the same attributes as FLD6 in record format RECORDB in FILE4 in LIB1.

    currently what happening is that just for example for my case number 5:-

    FIELD6    R               REFFLD(RECORDB/FLD6 LIB1/FILE4)  (5)  --> here due to combination of all this it's not fitting in single line and going to next line so i can i complete remaing file name in next line here ?

    Any example to overcome this issue here please?

    2) Suppose if i have to create a new file with below conditions for it's fields:-

    field1 from file1 should be concatenated with field2 of this same file1 where FIELD2 NE BLANK ,else concatenate to FIELD3+FIELD4 of this same file1.

    Any example to fulfil this condition for this case please?

    Thanks.



  • 2.  RE: Files - fields queries

    Posted 9 days ago

    Hi, 

    If anyone knows answers of these queries then could someone please resply soon? 

    Thanks. 




  • 3.  RE: Files - fields queries

    IBM Champion
    Posted 9 days ago

    For me the biggest question is: WHY do you want to create physical and logical files with DDS instead of using SQL?

    With SQL (DDL=Data Definition Language) you can create tables, views and indexes as well as columns with long SQL names and short system names (when using long names without having specified a system name, a system name - First 5 characters followed by a 5 digit running no - is automatically generated.

    SQL view never have a key but can include everything that can be used in an SELECT statement (except ORDER BY - since a View has no key), so it is possible to join tables and create new columns if neccessary

    SQL Indexes include the keys.

    Derived indexes allow to generate new key fields based on mathematical operators (e.g. Quantity * Price) or scalar functions (for examples YEAR(SalesDate), Upper(Name), Left(Text, 5))

    Additionally it is possible to add WHERE conditions to an index (which can be much more powerful than SELECT/OMIT clauses).

    Even though an index cannot be specified in a SQL Statement (SELECT, UPDATE, INSERT, DELETE) an SQL index can be used in composition with native I/O like any keyed logical file.

    Since an index is built over a single file, it cannot include join information, but also in a DDS described logical join file all key columns must be from the same table.

    So if you create a view and an index with the appropriate key fields and use embedded SQL (instead of native I/O) your program will run correctly.

    https://developer.ibm.com/articles/i-sql-indexs-and-native-io/  



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Selfemployed - Modernization-Education-Consulting on IBM i
    Kaufering
    +49 170 5269964
    ------------------------------



  • 4.  RE: Files - fields queries

    Posted 9 days ago

    Thanks ,but even though if we have to create a new physical file  (or any new file) with these cases then how can we resolve these issues  in DDS here ? 

    Thanks. 




  • 5.  RE: Files - fields queries

    Posted 9 days ago

    Hi, 

    I appreciate idea of SQL table here. 

    But if it has to be done on physical file's DDS then Could someone please help at DDS level for the same caes here? 

    Thanks. 




  • 6.  RE: Files - fields queries

    Posted 8 days ago

    Hi,

    Could someone please advise here soon? Appreciate prompt response from experts in DDS for physical files here.

    Thanks.




  • 7.  RE: Files - fields queries

    Posted 8 days ago

    Hi,

    1) For my first query it's resolved by putting '+' sign at end and continued remaining characters in next line so this issuse is fixed now.

    2) for 2nd issue could someone please advise here?

    Thanks.




  • 8.  RE: Files - fields queries

    Posted 7 days ago

    Hi, 

    Unable to do it in DDS of PF so tried SQL but it's also not working:-

    "

    CREATE TABLE NewTable AS
    SELECT
        Field1 AS NewField1,
        Field2 AS NewField2,
        Field3 AS NewField3,
        CASE
            WHEN Field2 <> '' THEN Field1 || Field2
            ELSE Field3 || Field4
        END AS ConcatenatedField
    FROM File1

    "

    So could someone please advise correct SQL query here to create desired file here? 

    Also once it's created will it have record format name like PF which could be used to SETLL, READE,(inside RPGLE program) and RCVF command etc. in CL program etc. 

    Thanks. 




  • 9.  RE: Files - fields queries

    Posted 6 days ago

    Hi,

    Apar from that concatenation of the fields i want some fields to be reffered from fiel1 some from fiel2 and some from file 3 likewise and these files could be sometime in same library and may be in different library plus i want to add some custome fields with my choice of data type and length in this table's structure.

    but currently unable to form a table : 

    tried below sql query but it's not getting executed getting this error:-"Keyword FROM not expected. Valid tokens: FOR USE SKIP WAIT WITH FETC  "

    CREATE TABLE l1/t1 AS (                                               
        SELECT                                                            
            f1.field1 AS NAME1,                                
            f1.field2 AS NAME2,                                           
            f1.field3 AS NAME3 ,                                 
            f2.field4 AS NAME4  ,                                    
            f4.field5 AS NAME5  ,                                   
            f1.field6 AS NAME6  ,                               
            f1.field7 AS NAME7  ,                               
            f1.field8 AS NAME8      ,                               
            f1.field9 AS NAME9                                   
                                                                          
        FROM LIB1.FILE1   as f1                                     
        FROM LIB1.FILE2   as f2                                     
        FROM LIB2.FILE3   as f3                                     
    ) WITH NO DATA                                            

    Awaiting expert's advise here for the same to be able to create desire table(file) structure here.

    Thanks.           



    ------------------------------
    jerry ven
    ------------------------------



  • 10.  RE: Files - fields queries

    Posted 6 days ago
    You have to JOIN the tables - a SELECT statement can only have one (1) FROM clause. The other solution is the list all tables behind the FROM with "," (comma) separated - which means a CROSS JOIN.

    HTH
    Daniel




  • 11.  RE: Files - fields queries

    Posted 6 days ago

    Thanks.

    I think advice was here like either use below sql qry:-

    CREATE TABLE l1/t1 AS (                                               
        SELECT                                                            
            f1.field1 AS NAME1,                                
            f1.field2 AS NAME2,                                           
            f1.field3 AS NAME3 ,                                 
            f2.field4 AS NAME4  ,                                    
            f4.field5 AS NAME5  ,                                   
            f1.field6 AS NAME6  ,                               
            f1.field7 AS NAME7  ,                               
            f1.field8 AS NAME8      ,                               
            f1.field9 AS NAME9                                   
                                                                          
        FROM LIB1.FILE1   as f1,                                     
        LIB1.FILE2   as f2,                                    
        LIB2.FILE3   as f3                                     
    ) WITH NO DATA       

    or

    use Join but how can i use it here ?

    meeting this requirements :-

    "

    1) this new table( file) should have some fields reffered from existing files which are there in different libraries ,however some files could be in same library also.

    plus that concatenation codtion which i mentioned in my starting posts here "ield1 from file1 should be concatenated with field2 of this same file1 where FIELD2 NE BLANK ,
    else concatenate to FIELD3+FIELD4 of this same file1." plus i should have liberty to add my own custom fields in this file with my own choice of datatype and length.

    "

    So Could someone please suggest a SQL query meeting these requirements here?

    Thanks much...




  • 12.  RE: Files - fields queries

    IBM Champion
    Posted 6 days ago
    1. If you want to create something similar to a logical file, you need to create a VIEW and NOT a Table (the SQL table is the equivalent of a PF).
    2. There are several JOIN methods, just listing tables separated by a comma is an CROSS JOIN which will generate a cartesian product of data, i.e. the 1st row of the 1st table is joined with all rows of the 2nd table, the 2nd row of the 1st is joined with all rows of the 2nd table ... and so on.
    3. Joining tables is very basic SQL, you'll find a lot of examples in the internet, or even W3School will explain it:https://www.w3schools.com/sql/sql_join.asp 


    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Selfemployed - Modernization-Education-Consulting on IBM i
    Kaufering
    +49 170 5269964
    ------------------------------



  • 13.  RE: Files - fields queries

    Posted 6 days ago

    Hi,

    Thanks for responding to my queries.

    any SQL query example based on my requirements here to create such view here?

    plus  i don't want to use cross join here i just want to create a structure of this table based on my requirements here.

    so just to create a structure of such a table why do i need to use  cross join here?

    neither i believe i need any kind of other joins etc. as i simply neeed to have a table created based on my requirements here.

    i think once a table using this SQL query is created :- CREATE TABLE l1/t1 AS (                                               
        SELECT                                                            
            f1.field1 AS NAME1,                                
            f1.field2 AS NAME2,                                           
            f1.field3 AS NAME3 ,                                 
            f2.field4 AS NAME4  ,                                    
            f4.field5 AS NAME5  ,                                   
            f1.field6 AS NAME6  ,                               
            f1.field7 AS NAME7  ,                               
            f1.field8 AS NAME8      ,                               
            f1.field9 AS NAME9                                   
                                                                          
        FROM LIB1.FILE1   as f1,                                     
        LIB1.FILE2   as f2,                                    
        LIB2.FILE3   as f3                                     
    ) WITH NO DATA       

    then in this table t1 i just need to use alter table add colmn sql query like these examples here:"SQL ADD COLUMN - Add One or More Columns To a Table (sqltutorial.org)" to add fields as per my choice of desired length and datatypes here.

    only thing i am not sure currently that complex thing like "field1 from file1 should be concatenated with field2 of this same file1 where FIELD2 NE BLANK ,else concatenate to FIELD3+FIELD4 of this same file1" --How would i achieve it in this table ?

    Any example for this desired table's(file) structure which could form my desired table using any single SQL query please?

    Thanks.

    so 




  • 14.  RE: Files - fields queries

    Posted 5 days ago

    Appreciate prompt response from experts here.

    Thanks.




  • 15.  RE: Files - fields queries

    IBM Champion
    Posted 5 days ago
    1. A view is nothing else than a stored SQL SELECT Statement. When accessing the view the stored SQL Statement is executed.
    2. Just run the SELECT statement you created (without having it embedded in a view) and check if the result is what you expect. ... and I suspect it is NOT (I assume because of the CROSS JOIN you'll get too much data).
    3. You have to revise your SELECT statement, so it will deliver the data you expect ... otherwise you'll consume too much or wrong data


    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Selfemployed - Modernization-Education-Consulting on IBM i
    Kaufering
    +49 170 5269964
    ------------------------------



  • 16.  RE: Files - fields queries

    Posted 5 days ago
    Edited by Daniel Gross 5 days ago

    Jerry,

    there is no such thing as a REFFLD in SQL. So to get a field definitions for a new table, you have to cboose all the fields you like in a single SELECT statement.

    And to get fields from different tables into a single SELECT statement, you have to use some sort of JOIN. The simplest JOIN - from the syntactic point of view - is the CROSS JOIN, where you simply write:

       ... FROM table1, table2, table3 ...

    Every other JOIN needs conditions that you have to write up in the ON clause. 

    ...
    FROM table1
    JOIN table2 ON 2=1
    JOIN table3 ON 3=3
    ...

    In this case you use a "natural" JOIN with conditions that will never be met, like "2=1" - so there is definitely no data that is selected - and the query engine is quite smart.

    You will get the table structure - but without data. And as  you will do your CREATE TABLE with the NO DATA clause - you should have the desired result.

    But this would also work the that CROSS JOIN using an "WHERE 1=2" clause - also a condition that cannot be met. Even as I assume that the SQL engine is smart enough not to select any data, as you are using NO DATA .

    So just try it out.

    HTH

    ------------------------------
    Daniel Gross
    ------------------------------



  • 17.  RE: Files - fields queries

    Posted 5 days ago

    Hi,

    Thanks for the help provided so far. I am currently focused just only on preparing structure (Schema) of this table.

    I tried my SQL query and it worked and later on was able to add columns in it as per my name and data type and length using alter table sql query.

    SELECT                                                            
            f1.field1 AS NAME1,                                
            f1.field2 AS NAME2,                                           
            f1.field3 AS NAME3 ,                                 
            f2.field4 AS NAME4  ,                                    
            f3.field5 AS NAME5  ,                                   
            f1.field6 AS NAME6  ,                               
            f1.field7 AS NAME7  ,                               
            f1.field8 AS NAME8      ,                               
            f1.field9 AS NAME9                                   
                                                                          
        FROM LIB1.FILE1   as f1,                                     
        LIB1.FILE2   as f2,                                    
        LIB2.FILE3   as f3                                     
    ) WITH NO DATA       

    *******************

    1)

    For the adding columns in this structure of the table i have further query here that however i am able to add it with my desired column name but what to do if some column name is like 'ABCD WXYZ(sometext)'

    though using alter table l1/t1 add " 

    "ABCD WXYZ(sometext)"

    it got added with these double quotation marks ("") around with this kind of column name but i don't want these double quotation marks around in my field name(column name) so how to get rid off from this ?

    2)

    Also I am still not sure that how can i add that complex thing like "field1 from file1 should be concatenated with field2 of this same file1 where FIELD2 NE BLANK ,else concatenate to FIELD3+FIELD4 of this same file1" --How would i achieve it in this table's structure ? because my field1 should be structured based on this condition only.

    So could someone please help for these points here?

    Thanks much.....




  • 18.  RE: Files - fields queries

    Posted 4 days ago

    Hi,

    Could someone please respond to my queries here soon?

    Thanks much..




  • 19.  RE: Files - fields queries

    Posted 4 days ago

    Hi Jerry,

    to your latest questions:

    1. If your column name (or any SQL name) should incl. spaces or special chars or is a reserved SQL word (like SELECT) you have to enclose your column name in double quotes.

      In the case that you don't mean the column name, but the column description/label/text - you can add these with then following:
      LABEL ON COLUMN tablename (
        column1 is 'Column 1 Description',
        column2 is 'Column 2 Description'
      );

      If you want to add a text description to your table its:
      LABEL ON TABLE tablename IS 'Table Description';

    2. What you want is a condition - but this won't have and function if you use the WITH NO DATA clause? Anyway, if you want a conditional column in a select statement, it's done like this:

      SELECT f1.field1 AS NAME1,
           f1.field2 AS NAME2,
           f1.field3 AS NAME3,
           f2.field4 AS NAME4,
           f3.field5 AS NAME5,
           f1.field6 AS NAME6,
           f1.field7 AS NAME7,
           f1.field8 AS NAME8,
           f1.field9 AS NAME9,
      CASE
      WHEN f1.field2 <> ''

                       THEN f1.field1||f1.field2
      ELSE f1.field3||f1.field4
           END AS nameX
        FROM LIB1.FILE1 as f1,

        LIB1.FILE2 as f2,
        LIB2.FILE3 as f3
      ) WITH NO DATA


      The CASE works like an RPG SELECT-statement - check WHEN the condition - if true, the THEN clause is "executed" - otherwise go to next WHEN or ELSE.

      But when you only use the structure, the result of the concatenation will only be on the structure - means, the resulting column "nameX" will have an appropriate data type - lets assume field1-4 are char(10) - the new column "nameX" will be at least char(20).

      But it won't have any effect on the data, as long as you doesn't fill it that way. SQL knows about "calculated columns", but this will AFAIK only work using a CREATE TABLE statement.

    I hope this answers some questions for you. 

    HTH




    ------------------------------
    Daniel Gross
    ------------------------------



  • 20.  RE: Files - fields queries

    Posted 3 days ago

    So far you have had some great answers.  The only reason I am adding this is the one tidbit I found to use. The DEFINITION ONLY and INCLUDING COLUMN DEFAULTS key words helped me when I tried to use the reference file concept for a new file.  I normally do not use this design, but someone challenged me to not change everything about the current process with DDS.  

    What I like about using the ACS Run scripts to develop new concepts is the detailed error messages that help me inch forward. 

    CREATE or REPLACE TABLE Long_Table_Name
    for system name MYFILEPF AS
      (SELECT FIELD1  as  FIELD1,
              FIELD2  as  FIELD2,
              CAST(' ' AS CHAR(50))    AS NEWFIELD3,
              CAST(' ' AS CHAR(10))    AS NEWFIELD4 
              CAST('0001-01-01-00.00.00.000000' AS TIMESTAMP) AS AUDITTIME,
              CAST(0 AS INTEGER  )     AS Record_ID
         FROM REFFILEPF)
              DEFINITION ONLY
              INCLUDING COLUMN DEFAULTS
     RCDFMT MYFILERF;

    -- Using SQL, we can assign the identity field
    ALTER TABLE MYFILEPF
      ALTER COLUMN RECORD_ID SET DATA TYPE INTEGER GENERATED ALWAYS AS IDENTITY;
    ALTER TABLE MYFILEPF
      ALTER COLUMN AUDITTIME SET DEFAULT CURRENT_TIMESTAMP;

    LABEL ON TABLE MYFILEPF IS 'SQL Table Based on Reference File';

    -- The columns existing in the reference file pick up their descriptions from the reference file. 
    -- You need to define the manually created columns here.
        LABEL ON COLUMN MYFILEPF (
            NEWFIELD3   IS 'New Field 3 description',
            NEWFIELD4   IS 'New Field 4 description',
            AUDITTIME IS 'AUDIT TIME',
            RECORD_ID IS 'RECORD ID'
            ) ;

        LABEL ON COLUMN MYFILEPF (
            NEWFIELD3 TEXT IS 'New Field 3 description',
            NEWFIELD4 TEXT IS 'New Field 4 description',
            AUDITTIME TEXT IS 'AUDIT TIME',
            RECORD_ID TEXT IS 'RECORD ID'
           ) ;                          

     



    ------------------------------
    David Taylor
    Sr Application Developer
    Range Resources
    Fort Worth
    ------------------------------



  • 21.  RE: Files - fields queries

    Posted 3 days ago

    David, 

      That example is one of the few times anyone has given anything positive about SQL tables and field references, and is nearly identical to the style I've had my team use for a number of years.  It seems odd there are so many that say it cannot be done, when it is very doable.  We vary slightly, doing it like this, and I discourage the ad-hoc style of your CAST statements, preferring to add to the field reference if needed.   

    fictional example of a DDL source member:

    CREATE OR REPLACE TABLE WEBORDERS (

    SESSIONID ,

    DSERRMSG)

     As

    (Select

    CODE15,

    TEXT100

     FROM MASTERREF)

    WITH NO DATA INCLUDING COLUMN DEFAULTS

    RCDFMT RWEBORD;

    LABEL ON TABLE WEBORDERS IS 'WEB CART INFO';

    LABEL ON COLUMN WEBORDERS (

    SESSIONID IS 'SESSION ID                                              ' ,

    DSERRMSG IS 'ERROR MESSAGE                                            ');

    LABEL ON COLUMN WEBORDERS (

    SESSIONID TEXT IS 'SESSION ID      ' ,

    DSERRMSG TEXT IS 'ERROR MESSAGE ');

    ALTER TABLE WEBORDERS

    PRIMARY KEY (SESSIONID);    

    In addition, we often copy file definitions, using DDS keyword FORMAT().  This too, is very possible in SQL DDL:

    CREATE OR REPLACE TABLE WEBORDERSH

    LIKE WEBORDERS

    RCDFMT RWEBORD;

    LABEL ON TABLE WEBORDERSH IS 'WEB CART INFO PURGED HISTORY';

    ALTER TABLE WEBORDERSH

    PRIMARY KEY (SESSIONID);

      Also, with the REPLACE function, we do not use alter to add fields, we code them in the DDL and rerun the statement and replace handles it fine.



    ------------------------------
    Mike Overlander
    ------------------------------



  • 22.  RE: Files - fields queries

    Posted 2 days ago

    Thank you all very much for all these inputs here.

    Thanks much..