MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Do we need to populate length of varchar cols when inserting

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
haqshaik
Beginner


Joined: 11 May 2005
Posts: 49
Topics: 19

PostPosted: Tue Nov 08, 2005 3:44 pm    Post subject: Do we need to populate length of varchar cols when inserting Reply with quote

Hi All,

I have a very basic doubt. Do we need to supply the length protion when we are inserting data into a table which has varchar coloumns thoruh a cobol db2 program. Even if we do not supply the length protion, will db2 calculates the length and stores in the first two bytes or it will default to the length specified for the coloumn.

Please thorw some light on this..

Thanks,
Salauddin
Back to top
View user's profile Send private message
Ravi
Beginner


Joined: 27 Jun 2005
Posts: 88
Topics: 2

PostPosted: Tue Nov 08, 2005 4:21 pm    Post subject: Reply with quote

Check this link
http://www.mvsforums.com/helpboards/viewtopic.php?t=244&highlight=varchar
Back to top
View user's profile Send private message
haqshaik
Beginner


Joined: 11 May 2005
Posts: 49
Topics: 19

PostPosted: Tue Nov 08, 2005 4:40 pm    Post subject: Reply with quote

Ravi,
I have been to this link, before posting. My concern is do we need to calculate the length specifically and then move into the length part , before inserting through a cobol program. I am not loading the table from a flat file. In such a case you need to have the length.

Thanks for your time..

Thanks,
Salauddin
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Tue Nov 08, 2005 4:46 pm    Post subject: Reply with quote

haqshaik,

You need to populate the length field of the varchar column before inserting into the table.

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
haqshaik
Beginner


Joined: 11 May 2005
Posts: 49
Topics: 19

PostPosted: Tue Nov 08, 2005 5:09 pm    Post subject: Reply with quote

Thanks Kolusu.

This is helpful

Thaks,
Salauddin
Back to top
View user's profile Send private message
haqshaik
Beginner


Joined: 11 May 2005
Posts: 49
Topics: 19

PostPosted: Tue Nov 08, 2005 6:43 pm    Post subject: Reply with quote

Kolusu,
In my current assignement, I have most of the coloumns defined as VARCHAR. I do not have any idea how the data looks like so that I can calculate the length of the coloumn before inserting into the database. I mean I am looking for some kind of delimiter so that I can calculate the length of the actual data using INSPECT verb. Is there a way if we do not know the delimiters, to calculate the length of the actual data or string?.
Is there any funtion exists in cobol 370, that calculated the string length?
I have tried length function, but it will give the length of the 01 variable, but not the actual content length.

Please guide me ..

Thanks,
Salauddin
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Tue Nov 08, 2005 6:54 pm    Post subject: Reply with quote

haqshaik,

Are you selecting the DB2 varchar column in the pgm or are you reading the dump of the table as a file?

If you are selecting the varchar column, the length is automatically populated in the lengh field.

If you look at the dclgen of a varchar field you will find it as follows
Code:

01  TABLE-DEF.                                       
    10 VARCOL.                                       
       49 VARCOL-LEN          PIC S9(4) USAGE COMP. 
       49 VARCOL-TEXT         PIC X(n).     


when you select the column , the varcol-len field will contain the length of the field.

If for some reason the table was populated with spaces and low-values, then the length field may contain the full varchar column length.

In that case you need to use your own logic to determine the actual length. check this piece of code

http://www.mvsforums.com/helpboards/viewtopic.php?p=5743#5743


Hope this helps..

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
haqshaik
Beginner


Joined: 11 May 2005
Posts: 49
Topics: 19

PostPosted: Wed Nov 09, 2005 12:04 pm    Post subject: Reply with quote

Kolusu,
Thans for your inputs. I am trying to insert into the table. My concern is as i have many coloumns as VARCHAR, I want to find out a best way to calculate the lenght so that the same can be reused for everey coloumn with out any effect. I have tested with out supplying the length, its inserting nulls in to the column. After selecting the row from the column I put display for the length protion and its showing as zeros. when the length portion is populated, its inserting and displaying the length properly after selecting from the table.
Thanks for giving the link to calculate the length.This will do the trick for me.

Thanks,
Salauddin
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Wed Nov 09, 2005 1:35 pm    Post subject: Reply with quote

I just ran a test in SPUFI, the results are below. Is a SPUFI Insert different thean COBOL?
(DB2 V8.1.5)

Code:

CREATE TABLE HOURS.UFCWLETDESC               
    (FROMDATE          DATE          NOT NULL,
     TODATE              DATE          NOT NULL,
     LETTERNAME       CHAR(020)             ,
     LETTERDESC       CHAR(100)             ,
     LETTERPATH        VARCHAR(500)       ,
     IMIAGE                CHAR(01)              ,
     USERID               CHAR(40)              ,
     LASTUPDT           TIMESTAMP             ,
     COLLATE             CHAR(01)              ,
     CHILD_DOC         CHAR(01)              ,
     RECIPIENT          CHAR(50)               
                                 )           
       IN FDBHRS.FTSLDESC                     
       ;           

INSERT INTO HOURS.UFCWLETDESC                             
         (FROMDATE                                       
         ,TODATE                                         
         ,LETTERNAME                                     
         ,LETTERDESC                                     
         ,LETTERPATH                                     
         ,IMIAGE                                         
         ,USERID                                         
         ,LASTUPDT                                       
         ,COLLATE                                         
         ,CHILD_DOC                                       
         ,RECIPIENT                                       
          )                                               
       SELECT                                             
       '9999-12-31',                                     
       '9999-12-31',                                     
       'TESTN',                                           
       'TESTD',                                           
       'ABC123',                                         
       'Y',                                               
       'RON',                                             
       CURRENT TIMESTAMP,                                 
       'A',                                               
       'B',                                               
       'C'                                               
       FROM SYSIBM.SYSDUMMY1;                             
---------+---------+---------+---------+---------+--------
DSNE615I NUMBER OF ROWS AFFECTED IS 1                     
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------
                                                         
 SELECT LENGTH(LETTERPATH), LETTERPATH                   
   FROM HOURS.UFCWLETDESC                                 
     WHERE FROMDATE = '9999-12-31';             
---------+---------+---------+---------+---------+---------+---
             LETTERPATH                                       
---------+---------+---------+---------+---------+---------+---
          6  ABC123                                           
DSNE610I NUMBER OF ROWS DISPLAYED IS 1                         
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100   
---------+---------+---------+---------+---------+---------+---
---------+---------+---------+---------+---------+---------+---
DSNE617I COMMIT PERFORMED, SQLCODE IS 0                       
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0                                         

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Wed Nov 09, 2005 1:43 pm    Post subject: Reply with quote

NASCAR9,
Yes a SPUFI insert is different in this case. You don't specify the length thru SPUFI & also it has a limitation of 255 bytes. Thanks
Back to top
View user's profile Send private message
haqshaik
Beginner


Joined: 11 May 2005
Posts: 49
Topics: 19

PostPosted: Wed Nov 09, 2005 4:10 pm    Post subject: Reply with quote

Nascar,
As suresh pointed out, SPUFI execution is a dynamic SQL. But from cobol program I am executing a static sql. if we do not populate the length field,we will end up getting -311 sql code.

Kolusu ,

Just to share with you, here is other way to get the length of a varchar variable.

NAME VARCAHR(30)
Code:

DCL GEN:
01 NAME
     49 NAME-LEN                    PIC S9(4) USAGE COMP.
     49 NAME-TEXT                  PIC X(30).
MOVE 40  (length of the text part)
   TO NAME-LEN                   
EXEC SQL
  SET :NAME    =  RTRIM(:NAME)
END-EXEC.

Then the text and len fileds will get populated with no spaces and the length of the string.
we can use the host variable :NAME while inserting into the table.
I did a select after this and displayed the length field, which comes out correct.
Will there be any overhead in performance if we use the code as above in compared to the code that you have provided to find the length of a sring using cobol.
I guess the one you provided will be faster than as said above. The number of high level calls would be more I guess in the above said case.
Please advice me.

Thanks,
Salauddin
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group