Hi,
I have a table, something similar to:
create table my_table (
id char(32) not null primary key,
num integer not null,
code varchar(2) not null,
name varchar(60) not null,
[...]
)
Now I need to add another column 'calc_field' of type varchar that is
always automatically
derived from columns: id, num, code (concatenated)
I was trying to do something with concatenation and CAST but it always
fails.
Even if I try to test my expression with simple select to see generated
column,
the same expression fails in the 'generated always as' statement.
Any hint how to do it?
Thanks in advance
Darek 17 4662
In article <11*********************@i40g2000cwc.googlegroups. com>, dw*****@gmail.com says... Hi,
I have a table, something similar to:
create table my_table ( id char(32) not null primary key, num integer not null, code varchar(2) not null, name varchar(60) not null, [...] )
Now I need to add another column 'calc_field' of type varchar that is always automatically derived from columns: id, num, code (concatenated) I was trying to do something with concatenation and CAST but it always fails. Even if I try to test my expression with simple select to see generated column, the same expression fails in the 'generated always as' statement.
It might help if you post what you allready tried.
Darek wrote: Hi,
I have a table, something similar to:
create table my_table ( id char(32) not null primary key, num integer not null, code varchar(2) not null, name varchar(60) not null, [...] )
Now I need to add another column 'calc_field' of type varchar that is always automatically derived from columns: id, num, code (concatenated) I was trying to do something with concatenation and CAST but it always fails. Even if I try to test my expression with simple select to see generated column, the same expression fails in the 'generated always as' statement.
Have you tried?
id || CHAR(num) || code
I take a guess and suspect you tried to cast to VARCHAR which is not
supported directly.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Darek,
I don't think it should a problem. Here is a ddl that works:
create table my_table (
id char(32) not null primary key,
num integer not null,
code varchar(2) not null,
name varchar(60) not null,
gen generated always as (id concat char(num) concat code)
);
Probably you should modify the code to remove white characters from the
generated value (I suggest to write SQL function to do this).
I think I know what your problem is. If you have data in your table,
you cannot just alter the table to add a generated column. Here is what
you should do:
set integrity for my_table off;
alter table my_table
add column gen generated always as (id concat char(num) concat code)
;
set integrity for my_table immediate checked force generated;
One important remark. If your my_table is in reference with other
tables (primary, foreign keys) the first statement wil also move the
tables into "integrity off" state. So, in the last statement you must
put all the table names, and (this is important) as a one SQL
instruction, eg.
set integrity for my_table, fk_my_table, fk2_my table immediate checked
force generated;
Radosnych Swiat, Alleluja
-- Artur Wronski
I would try to avoid generated always in the design.
It's drifting away from the relational model (it is an expression but
not in a view), programmer's will use the column instead of the
expression, tools will have problems to cope with it.
Bernard Dhooghe
Bernard Dhooghe wrote: I would try to avoid generated always in the design.
It's drifting away from the relational model (it is an expression but not in a view), programmer's will use the column instead of the expression, tools will have problems to cope with it.
Generated columns are a performance feature just like MQTs and
replicated tables.
There are two main reasons for them:
1. Improve performance of where clauses with expressions in them.
2. Improve performance of queries with expensive expressions.
Functionally dependent columns are discouraged because they can become
inconsistent. Generated columns are designed from teh ground up to
remain consistent.
So, all you pay is storage.
So, while they may not belong into the first schema design they have
their rightful place in reality.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Thanks for all replies.
The hints above helped me a lot.
And yes, I need it to speed up some expensive "where" conditions.
Without doing this I had problems with full scans, now it started to
use indexes on generated columns. Just a few such columns in a big data
model should not be a problem.
Darek
Seeing colums in the physical data model that are there for query
purposes is to far away from the relation model. That "just ..." could
be more than ennoying in the long term.
Company's management could ask IBM to implement standards. In this case
of SQL 92...
Point n'est besoin d'espérer ...
Bernard Dhooghe
Darek wrote: Thanks for all replies. The hints above helped me a lot. And yes, I need it to speed up some expensive "where" conditions. Without doing this I had problems with full scans, now it started to use indexes on generated columns. Just a few such columns in a big data model should not be a problem. Darek
Bernard Dhooghe wrote: Company's management could ask IBM to implement standards. In this case of SQL 92...
Why use that old and deprecated standard?
The SQL standard doesn't give any specifications regarding performance
things. Not even regular indexes are defined there. So we're talking
product-specific anyway...
--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Knut Stolze wrote: Bernard Dhooghe wrote:
Company's management could ask IBM to implement standards. In this case of SQL 92...
Why use that old and deprecated standard?
The SQL standard doesn't give any specifications regarding performance things. Not even regular indexes are defined there. So we're talking product-specific anyway...
Matter of fact GENERATED COLUMNS ARE SQL Standard.
(and so are sequences and identity columns).
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Being part of standard (2003, IBM presentation on http://www.wiscorp.com/SQL2003Features.pdf) does not mean that in this
case it's usage will not bite.
SQL92 is the standard to use (and to have in DB2 UDB) to solve the
problem. Nicely relational, short SQL notation, no data overhead, no
sqlda problems
Bernard Dhooghe
Serge Rielau wrote: Knut Stolze wrote: Bernard Dhooghe wrote:
Company's management could ask IBM to implement standards. In this case of SQL 92...
Why use that old and deprecated standard?
The SQL standard doesn't give any specifications regarding performance things. Not even regular indexes are defined there. So we're talking product-specific anyway... Matter of fact GENERATED COLUMNS ARE SQL Standard. (and so are sequences and identity columns).
True. But this makes the discussion here even more strange. I still don't
get what standards have to do with the use of generated columns in this
particular case and what the company's management of the OP is supposed to
do.
--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Bernard Dhooghe wrote: Being part of standard (2003, IBM presentation on http://www.wiscorp.com/SQL2003Features.pdf) does not mean that in this case it's usage will not bite.
The national standardization bodies (i.e. ANSI, DIN, ...) participate in
ISO. Companies are only so much involved as they might sponsor a member of
these national bodies.
SQL92 is the standard to use (and to have in DB2 UDB) to solve the problem.
No, its not. SQL:2003 is the one.
--
Knut Stolze
DB2 Information Integration Development
IBM Germany
In this case, the answer is in SQL92, with row value constructors.
Select .. where (id ,num, code) > >= = < <= ...
Optimizer can just pick up the index, no table scans, easy semantic
mapping.
Would you not ask your management to ask the vendor to implement
standards that can help, this being here part of ... SQL92?
For those who want more complex things, index definition based on an
expression, working behind the scenes.
Bernard Dhooghe
Bernard Dhooghe wrote: In this case, the answer is in SQL92, with row value constructors.
Select .. where (id ,num, code) > >= = < <= ...
You can do a "(id, num, code) = (val1, val2, val3)" in DB2. The other
comparison operators are not handled that way, though. Semantically, this
is identical to "(id = val1 AND num = val2 AND code = val3)". So the
combined variation is just syntactic sugar.
Optimizer can just pick up the index, no table scans, easy semantic mapping.
The DB2 optimizer will do just that if it estimates that this would lead to
better execution times.
Would you not ask your management to ask the vendor to implement standards that can help, this being here part of ... SQL92?
Sure, I would. However, I wouldn't ask for minor syntactical things but
rather for real important features like the MERGE statement or range
partitioning.
p.s: I still don't understand why you're referring to a deprecated standard.
SQL92 is superseeded by SQL:1999, which in turn is out-of-date since
December 2003 wher SQL:2003 was published by ISO.
--
Knut Stolze
DB2 Information Integration Development
IBM Germany
I refer to SQL92 as in this standard, the row-value constructor clause
was introduced to solve the problem of this posting (and a number of
others in this newsgroup).
So it is part of a standard that is 13-14 years old. Let us respect
what was crafted by the committee then.
Is is not minor, there is no fully semantical equivalent way to express
what is contained in the clause. When (c1,c2) >= (v1,v2) is needed,
written as c1 > v1 or c1 = v1 and c2 >= v2 can the optimizer detect
what is meant exactly? This is certainly much more difficult than
implementing row value constructors.
Bernard Dhooghe
Knut Stolze wrote: Bernard Dhooghe wrote:
In this case, the answer is in SQL92, with row value constructors.
Select .. where (id ,num, code) > >= = < <= ...
You can do a "(id, num, code) = (val1, val2, val3)" in DB2. The other comparison operators are not handled that way, though. Semantically, this is identical to "(id = val1 AND num = val2 AND code = val3)". So the combined variation is just syntactic sugar.
Optimizer can just pick up the index, no table scans, easy semantic mapping.
The DB2 optimizer will do just that if it estimates that this would lead to better execution times.
Would you not ask your management to ask the vendor to implement standards that can help, this being here part of ... SQL92?
Sure, I would. However, I wouldn't ask for minor syntactical things but rather for real important features like the MERGE statement or range partitioning.
p.s: I still don't understand why you're referring to a deprecated standard. SQL92 is superseeded by SQL:1999, which in turn is out-of-date since December 2003 wher SQL:2003 was published by ISO.
-- Knut Stolze DB2 Information Integration Development IBM Germany2
Put this in a VIEW. The coce will be Standard, portable SQL and always
up to date. Do not make it hard and proprietary.
>> I still don't understand why you're referring to a deprecated standard.
SQL92 is superseeded by SQL:1999, which in turn is out-of-date since
December 2003 wher SQL:2003 was published by ISO. <<
But then you get into the problems of porting code to products that are
just getting to SQL-92 and the way the US Government (largest user of
computers on Earth) only wants SQL-92 code. Then local syntax for this
feature varies between SQL:2003 and SQL Server, etc.
I'd go with the VIEW fopr practical reasons right now and then switch
over when more products have it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: L Mehl |
last post by:
Hello --
Our user wants a column in the DataGrid formatted as "%", so that he sees
"nn.n%" instead of ".nnn".
I have seen mention of StdDataFormat, as in
Dim fmtPct1 As New StdDataFormat
...
|
by: marlene |
last post by:
Hi There
Does anyone know how to create a query or do something in a stored
procedure to output one column as the other columns name? The output I
want is something like this:
...
|
by: Prince Kumar |
last post by:
Is there any way I can define an Unique constraint or unique index
which allows more than one null values for the same column combination
in DB2?
ie, If my index is defined on (col3, col4) where...
|
by: Brian |
last post by:
This is causing me to not be able to create a relation in my dataset.
Here's my code:
dc1 = ds.Tables .Columns ;
dc2 = ds.Tables .Columns ;
dr1 = new System.Data.DataRelation...
|
by: Jan |
last post by:
I'm using the CSharpCodeProvider to buils some assemblies @ runtime which are
never saved as files (cp.GenerateInMemory = true;).
The generated assemblies are hierachically dependent on each other...
|
by: sherifffruitfly |
last post by:
Hi,
I'm using an adaptation of excel-reading code that's all over the
internet - I don't much like or understand it, but it has worked for me
in the past.... beggars can't be choosers... :
...
|
by: Fir5tSight |
last post by:
Hi,
I have a stored procedure that looks like the follows:
-------------------------------------------------------------------------------------
SELECT
ClientName AS 'Client Name',
Location,...
|
by: NewlytoSQL |
last post by:
Hi all, im fairly new to SQL and im stuck half way through a query,
im using DB2 here is what im tryng to do.
i have a query that brings back an item number , shelf req, sum of all orders columns,...
|
by: micarl |
last post by:
How would i print a report based on criteria selected from several
Combo Boxes as well as multiple Multi Select List Boxes, that are
located on the same form?
I can get one Multi List Box, just...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |