If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > extracted referentiel intégrity from db2 catalog

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-07, 11:48
mario dionne mario dionne is offline
Registered User
 
Join Date: Jun 2007
Posts: 1
extracted referentiel intégrity from db2 catalog

The following SQL request work, but l am not able to put the CHAR "," between more then one FK_COLNAMES. the output look like that:
FOREIGN KEY (x,,,,,y) and I need FOREIGN KEY(x,y). Who can help me

db2 +o -x +v +c -r/$DB2TMP/db2refint.bk "SELECT 'ALTER TABLE ' \
||RTRIM(SUBSTR(tabschema,1,10))|| '.' ||RTRIM(SUBSTR(TABNAME,1,50)), \
'ADD CONSTRAINT ' || RTRIM(CONSTNAME),'FOREIGN KEY \
(' ||REPLACE(RTRIM(LTRIM(substr(FK_COLNAMES,1,100))), ' ',',')|| ')', \
'REFERENCES ' ||RTRIM(SUBSTR(tabschema,1,10)) || \
'.' ||RTRIM(SUBSTR(REFTABNAME,1,17)) \
,' ON DELETE ' || case deleterule \
when 'A' then 'NO ACTION' \
when 'C' then 'CASCADE' \
when 'N' then 'SET NULL' \
when 'R' then 'RESTRICT' \
end, \
' ON UPDATE ' || case updaterule \
when 'A' then 'NO ACTION' \
when 'R' then 'RESTRICT' \
end \
|| ';' \
FROM \
SYSCAT.REFERENCES \
where \
tabschema like '${schema}%'"
Reply With Quote
  #2 (permalink)  
Old 06-12-07, 14:30
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
It looks like you are trying to generate a script. Why not just edit it with your favorite text editor when it is generated by replacing ",," with "," several times until there is nothing that matches?

Since FK_COLNAMES will be removed in future versions, you should use SYSCAT.KEYCOLUSE. Maybe you can come up with a subselect thaty does a pivot to do what you want.

Andy
Reply With Quote
  #3 (permalink)  
Old 06-13-07, 09:53
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Ii tried to tackle your problem. There is no simple solution. It seems DB2 reserves 20 characters per column in its FK_COLNAMES and PK_COLNAMES fields.

The easiest solution to me seems to have the result reprocessed by a program to get the desired result. If that is not possible you will have to rely on recursive SQL.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #4 (permalink)  
Old 06-25-07, 05:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
FK_COLNAMES is broken. As soon as you have longer column names or delimited column names, it doesn't work anymore. As Andy said, use SYSCAT.KEYCOLUSE instead. There you can use a simply recursive query to concatenate column names with ',' as separators (and enclosed in double-quotes for delimited names).
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On