IBM i Global

 View Only
Expand all | Collapse all

Omitting certain fields from a file using sql query

  • 1.  Omitting certain fields from a file using sql query

    Posted Fri July 26, 2024 10:31 AM

    Hi,

    Rather than selecting select field1,field2,... field99 from a file if i want to omit only certain fields and let the other fields be selected as it is so there any way for the same using select /omit criteria in sql query ?

    Thanks



  • 2.  RE: Omitting certain fields from a file using sql query

    Posted Sun July 28, 2024 05:06 AM

    Hi, 

    Could someone please respond to my query here, if anyone knows it's answer? 

    Thanks much... 




  • 3.  RE: Omitting certain fields from a file using sql query

    IBM Champion
    Posted Mon July 29, 2024 08:11 AM

    Hello,

    As far as I know, Db2 for i (and most other DBMSs) does not have such function. You would need to achieve equivalent functionality in one of the following ways.

    Hope this helps.



    ------------------------------
    Hideyuki Yahagi
    ------------------------------



  • 4.  RE: Omitting certain fields from a file using sql query

    Posted Tue July 30, 2024 03:51 AM

    An alternative is to define those columns that you do NOT want to see as IMPLICITLY HIDDEN. There are some caveats behind doing that on inserting rows in to the table; default value etc., but if your SQL statement is SELECT * FROM SCHEMA.TABLE hidden columns are not returned. To view the column you need to EXPLICITLY specify it on the SELECT statement. 



    ------------------------------
    Keith Pryke
    ------------------------------



  • 5.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 06:03 AM

    Hi,

    Thanks for the response here but when I tried below SQL query just for example:-

    " SELECT * EXCEPT(FIELD1,FIELD2)                         

    FROM mylib/myfile        "           

    Then I got this error:" Keyword EXCEPT not expected. Valid tokens: FROM INTO.  

    And creating a view here will also involve selecting all those desired columns one by one so it looks it will be adding extra step here.

    · And what does 'Use RCAC if you simply do not want to show the value of a particular column.' What does It mean here? could someone please clarify it in detail?

          

    "An alternative is to define those columns that you do NOT want to see as IMPLICITLY HIDDEN. There are some caveats behind doing that on inserting rows in to the table; default value etc., but if your SQL statement is SELECT * FROM SCHEMA.TABLE hidden columns are not returned. To view the column you need to EXPLICITLY specify it on the SELECT statement. "

    Any example with SQL query for this 'IMPLICITLY HIDDEN' SQL Query for above point please?

    Thanks much….




  • 6.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 06:49 AM

    EXCEPT is not a valid keyword for Db2i. That won't work. 

    RCAC is Row Column Access Control. See https://www.ibm.com/docs/en/i/7.5?topic=overview-row-column-access-control

    You define a column as IMPLICITLY HIDDEN. See https://www.ibm.com/docs/en/i/7.5?topic=statements-alter-table for syntax. You can also define this attribute on the CREATE TABLE SQL statement. 



    ------------------------------
    Keith Pryke
    ------------------------------



  • 7.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 07:24 AM

    Thanks for 'IMPLICITY HIDDEN' in the mentioned IBM i link but I could not find any real example there.

    If anyone has any example with any SQL query example using the same here, then could someone please share the same here explaining this?

      

    Thanks much...




  • 8.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 07:47 AM

    Hi,

    Just for example I was able to create a table here "CREATE TABLE l1/Emp (                           
        EmployeeID INT NOT NULL,                    
        FirstName VARCHAR(50),                      
        LastName VARCHAR(50) IMPLICITLY HIDDEN,     
        Salary DECIMAL(10, 2)                       
    )                                               
    Table EMP in L1 created but was not journaled.  "

    and when I do 'select * from l1/emp' I cannot find 'LastName' here as 'IMPLICITLY HIDDEN' has been specified against this field name here, but my question here is like suppose there is a file which already exists ( and we are not sure whether this 'IMPLICTLY HIDDEN' has already been used while forming that table/file or not  in it's DDS) in the system then how can we filter out I mean omit certain fields and select rest other fields so any example for the same using this 'IMLICITLY HIDDEN' here please for SELECT SQL query here please ?

    Thanks much...




  • 9.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 08:01 AM

    select * from qsys2.syscolumns where table_schema = 'L1' and table_name = 'EMP' ;

    This will return column information about the table you just created. There is a column there called HIDDEN that will tell you if it is hidden or not. Just replace the table_schema and table_name where clause values for the table you want to query. You could build the SELECT statement from that. 



    ------------------------------
    Keith Pryke
    ------------------------------



  • 10.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 08:12 AM

    I think my query is still misunderstood here what I meant to ask was that if a table let's say it's not created using this 'implict hidden' then in such table/file how will we be able use SELECT query to get only those columns whose records we want to see excluding certain fields from that table/file? 

    Also i tried this SQL query it shows data here like :- 

    What does these 'N' and 'P' mean here?

    Thanks..




  • 11.  RE: Omitting certain fields from a file using sql query

    Posted Thu August 01, 2024 09:12 AM

    First the "easy" answer. HIDDEN is ''P when Implicitly Hidden is defined, otherwise it is 'N'. No idea why IBM chose P! 

    Taking you L1.EMP table let's say you want to see all columns except SALARY and any implicitly hidden columns. Executing select * from qsys2.syscolumns where table_schema = 'L1' and table_name = 'EMP' and HIDDEN = 'N' AND COLUMN_NAME NOT IN ('SALARY') would return you the list of all columns you want to query. I've chosen NOT IN so that if you wanted to add additional columns to EXCLUDE they can be added easily. Your application would need to build and execute a SQL statement that you construct from the rows read. You should obviously check that you have at least one row returned! 

    Your application can then "Fetch" the rows into local host variables. If this is to be a generic "any table in any schema" application you would need to do prepare / describe statements to get the column information. This is a fun area! See IBM's documentation for this, and sample statements too. 



    ------------------------------
    Keith Pryke
    ------------------------------



  • 12.  RE: Omitting certain fields from a file using sql query

    IBM Champion
    Posted Thu August 01, 2024 10:00 PM

    > What does It mean here?

    If the purpose of excluding certain columns is "not to show sensitive values such as salary," RCAC would be suitable.

    As mentioned previously, there are several ways to exclude certain columns with "SELECT *" as discussed in "Exclude a column using SELECT * [except columnA] FROM tableA?". For example,

    a) Create temporary table : Realistic for small DB. Run "ADDRPYLE SEQNBR(99) MSGID(CPA32B2) RPY(I)" in advance. 

    SELECT * FROM QIWS.QCUSTCDT;
    CREATE OR REPLACE TABLE QTEMP.QCUSTCDT AS (SELECT * FROM QIWS.QCUSTCDT) WITH DATA ON REPLACE DELETE ROWS
    CL:CHGJOB INQMSGRPY(*SYSRPYL);
    ALTER TABLE QTEMP.QCUSTCDT DROP COLUMN BALDUE; 
    SELECT * FROM QTEMP.QCUSTCDT;

    b) Create view : Efficient for continuous use. May be inefficient if you want to exclude columns dynamically or have a large number of exclusion patterns.

    For example, a list of columns for a particular table can be obtained from the SYSCOLUMNS table.

    SELECT COLUMN_NAME || ',' AS COLUMNS  FROM QSYS2.SYSCOLUMNS WHERE TABLE_NAME = 'QCUSTCDT' AND TABLE_SCHEMA = 'QIWS'

    Create a View by deleting unnecessary columns from the list of columns.

    ----

    To get the desired answer, please explain the purpose of excluding certain columns.

    If the table is to be processed by an embedded SQL program, it does not matter if some columns are not used, as they can simply be ignored by the logic. If you have a table that you want to make available to the public through a query, SQL script, ACS download, etc., and this table contains columns that you want to keep private, you can implement security with RCAC.



    ------------------------------
    矢作 英幸
    ------------------------------



  • 13.  RE: Omitting certain fields from a file using sql query

    Posted Fri August 02, 2024 05:49 AM

    Thanks to all for these helpful inputs!