View previous topic :: View next topic
|
Author |
Message |
rajandhla
Active User
Joined: 18 Oct 2005 Posts: 182 Location: Luton UK
|
|
|
|
Hi Guys,
Can you please let me know how to set default value to a column.
Select <C1>, <C2> from table1.
I want to set default value to column c2 irrespective of the data the table contains.
Regards,
JR |
|
Back to top |
|
|
sandeep1dimri
New User
Joined: 30 Oct 2006 Posts: 76
|
|
|
|
Select <C1>, 'default value' from table1. |
|
Back to top |
|
|
rajandhla
Active User
Joined: 18 Oct 2005 Posts: 182 Location: Luton UK
|
|
|
|
Cheers JR..... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Just to make sure we are all thinking the same way. . . .
This
Quote: |
Select <C1>, 'default value' from table1. |
will force the default value into the result from the execution of that select.
The data in the table will not contain the "default value" after that select is executed. A subsequent "SELECT C1, C2 FROM TABLE1" will not return the "defalut value", it will return whatever values were in C2 in the table. |
|
Back to top |
|
|
rajandhla
Active User
Joined: 18 Oct 2005 Posts: 182 Location: Luton UK
|
|
|
|
Hi,
Is it possible to set default value for the column in the cursor. And i want the default value in the corresponding value in the host variable after fetch executed?
Regards,
Jr |
|
Back to top |
|
|
sundar28
New User
Joined: 30 Jul 2005 Posts: 11 Location: Mysore
|
|
|
|
Rajandhla,
Clarify your question!
Quote: |
How to set default value to column ? |
is not the question for the answers you got.
Shall I restate your question as
How to get a common value in place of one column for all the result rows?
Quote: |
Is it possible to set default value for the column in the cursor |
Shall I restate your question as
Is it possible to set default value for the column in a result table fetched using cursor
It is possible. The query format is the same as sandeep1dimri had given.
Note: A common value not default value: "Default" is to fill up an absence of a requisite data
Quote: |
And i want the default value in the corresponding value in the host variable after fetch executed? |
But why? You can initialize your host variable with that default value before fetch. For this simple purpose, do not change your query to give DB2 an extra task?? |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 632 Location: Wisconsin
|
|
|
|
So you want to fetch the default value in your cursor and also push that default value back to the actual database?
The first solution give will set the value in your cursor only. If you want to push that value back to the database, you need to execute an UPDATE to push the value back to the table.
Use your SELECT cursor to read the values out to working storage with your default value for COLUMN2. Then use your UPDATE command to change the actual table.
Code: |
*Declare SELECT cursor to have this SQL
SELECT COL1,
'DefaultValue'
FROM TABLE1
WHERE COL1 = :WS-COL1Lookup
*Open the cursor
EXEC SQL FETCH select-cursor-name
INTO :WS-COL1,
:WS-COL2
END-EXEC.
EXEC SQL
UPDATE TABLE1
SET COL2 = :WS-COL2
WHERE COL1 = :WS-COL1
END-EXEC.
|
|
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 632 Location: Wisconsin
|
|
|
|
ignore the update-cursor-name I don't know what I was thinking there. |
|
Back to top |
|
|
|