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 

Varchar

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


Joined: 16 May 2008
Posts: 8
Topics: 4

PostPosted: Tue May 20, 2008 1:48 am    Post subject: Varchar Reply with quote

While using embedded sql in cobol application program to insert data into a varchar field , is it necessary to populate the length host variable as well..or does Db2 automatically store the actual length ( i.e without the trailing spaces )..I have the same question in case of Updating a varchar field with embedded sql.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Tue May 20, 2008 3:05 am    Post subject: Reply with quote

you always need to populate the lvl 49 length field if you are supplying the source data.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
deeptik
Beginner


Joined: 16 May 2008
Posts: 8
Topics: 4

PostPosted: Tue May 20, 2008 4:29 am    Post subject: Reply with quote

ok..but why is it necessary ? Will the query fail if we don't populate the lvl 49 length variable ?

Also , again if the insert/update is done outside the application program .i.e say through QMF it may not be possible to provide the length so I guess DB2 would automatically take care to store the actual length of the varchar field ?

Thanks
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Tue May 20, 2008 7:58 am    Post subject: Reply with quote

You must provide the length on INSERT and UPDATE when using static SQL. If you don't, DB2 will use whatever residual value is stored in the length variable. If you never populate the length, that residual value will be 0, and that column will wind up with 0 length for all rows processed. To your point about QMF. QMF is a DB2 reporting tool. When you INSERT/UPDATE varchar columns using QMF, QMF utilizes dynamic SQL to PREPARE and EXECUTE the statements (probably EXECUTE IMMEDIATE, but I'm not certain). DB2 handles the lengths because it's dynamic SQL.
Back to top
View user's profile Send private message
CraigG
Intermediate


Joined: 02 May 2007
Posts: 202
Topics: 0
Location: Viginia, USA

PostPosted: Tue May 20, 2008 9:03 am    Post subject: Reply with quote

SPUFI and QMF populate the length based on the delimiters for the character variables. It a program there are no delimiters used and the host variable field may be much longer then the value contained in it. Trailing characters in the host variable may or may not be meaningful. DB2 has no way of knowing if they are meaningful.
Back to top
View user's profile Send private message
deeptik
Beginner


Joined: 16 May 2008
Posts: 8
Topics: 4

PostPosted: Wed May 21, 2008 1:24 am    Post subject: Reply with quote

Thanks for your replies, I think what I understood is that you will need to tell Db2 what length needs to be stored in case of static SQL.

I have a couple of more q's, I could have tried out myself but since I don't have access to compile Db2 test programs in my installation I will put them in this forum..

1) EMP TABLE (ENAME VARCHAR(50)
EID CHAR(7));

Corresponding dclgen

10 ENAME.
49 ENAME-LEN PIC S9(4) USAGE COMP.
49 ENAME-TEXT PIC X(50).
10 EID pic x(7).

If I need to insert Name = 'JOHN' and if I populate ENAME-TEXT='JOHN' and

1)ENAME-LEN = 4, value of ENAME stored in my table = 'JOHN'
2)ENAME-LEN = 3, will ENAME stored = 'JOH' or 'JOHN'
3)ENAME-LEN =0 (i.e I never populate this length) , what will be the value of ENAME ?

Basically, does value populated in length host variable determine the content of TEXT to be stored ( Well I guess the answer is a NO but still want to confirm) or is it only that you store the length for your own reference and and if it is stored wrongly DB2 can actually do nothing about it ?

2) Update condition. Suppose my current value in the database before update is 'JOHN'. Now I want to update 'JOHN' to 'IAN' but I forget to update the length host variable , what will be the value now in my database...will it be 'JIAN' ?
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Wed May 21, 2008 7:49 am    Post subject: Reply with quote

1) correct, ENAME='JOHN' after the update
2) ENAME='JOH'
3) ENAME='', that is, a zero length string; this is not the same as null

Again, the program determines the length that DB2 uses for the column value. If you specify 3 and the entire 50 characters of the text variable are populated, DB2 will store the first 3 in the target column.

2) If you forget to update the length variable, DB2 will use whatever value is in the length variable at the time you issue the update. DB2 doesn't know you forgot. So the answer depends on what is in the length and text variables when you issue the update.
Back to top
View user's profile Send private message
deeptik
Beginner


Joined: 16 May 2008
Posts: 8
Topics: 4

PostPosted: Wed May 21, 2008 11:25 pm    Post subject: Reply with quote

Thank you so much Sharon ! I am clear on this now...
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