Icon View Thread

The following is the text of the current message along with any replies.
Messages 11 to 15 of 15 total
Thread Best way to upgrade database structure
Wed, Oct 10 2012 4:43 PMPermanent Link

IQA

On 11/10/2012 12:17 AM, Roy Lambert wrote:
> Phil
>
>
> And here's the "better way"
>
> SCRIPT
> BEGIN
> DECLARE Moder VARCHAR DEFAULT '';
> SET Moder = AddFieldIfNotExists('Adam','Test2',Moder,'BOOLEAN');
> SET Moder = Moder + AddFieldIfNotExists('Adam','Test3',Moder,'INTEGER');
> SET Moder = Moder + AddFieldIfNotExists('Adam','Test4',Moder,'VARCHAR(17) COLLATE ANSI');
> IF Moder <> '' THEN
> EXECUTE IMMEDIATE 'ALTER TABLE Adam ' + Moder;
> END IF;
> END
>
>
> ALTER FUNCTION "AddFieldIfNotExists" (IN "Tbl" VARCHAR COLLATE ANSI_CI, IN "Fld" VARCHAR COLLATE ANSI_CI, IN Current VARCHAR COLLATE ANSI_CI, IN NewFldDef VARCHAR COLLATE ANSI_CI)
> RETURNS VARCHAR COLLATE ANSI_CI
> BEGIN
> DECLARE Source CURSOR FOR Cat;
> DECLARE Checker VARCHAR COLLATE ANSI_CI;
> DECLARE Result VARCHAR COLLATE ANSI_CI DEFAULT NULL;
> PREPARE Cat FROM 'SELECT Name FROM Information.TableColumns WHERE TableName = ? AND Name = ?';
> OPEN Source USING Tbl,Fld;
> FETCH FIRST FROM Source('Name') INTO Checker;
> IF Checker IS NULL THEN
> IF Current <> '' THEN
>    SET Result = ', ';
> END IF;
> SET Result = Result +'ADD COLUMN '+Fld+' '+ NewFldDef;
> END IF;
> RETURN Result;
> END
>
>
>  From this you should be able to work out how to delete columns or alter them if they're already there.
>
> Roy Lambert [Team Elevate]
>

That's brilliant, thanks soooo much Roy! Smile
Wed, Oct 10 2012 8:01 PMPermanent Link

IQA

Just a side note Roy... Fields where not getting added to the table and
I couldn't work out why, but I realised the Function was checking for <>
'' when the initial value was NULL.

So I changed:

IF Current <> '' THEN

to ...

IF Current IS NOT NULL THEN

and it worked perfectly.

Thanks again.
Thu, Oct 11 2012 4:02 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Phil


>Just a side note Roy... Fields where not getting added to the table and
>I couldn't work out why, but I realised the Function was checking for <>
>'' when the initial value was NULL.

That's interesting and I think its a bug.

DECLARE Moder VARCHAR DEFAULT '';
SET Moder = AddFieldIfNotExists('Adam','Test2',Moder,'BOOLEAN');

This to me means that Moder (which is passed in as Current) should be '' not NULL. I've sent an email to Tim for clarification.

Roy Lambert [Team Elevate]
Thu, Oct 11 2012 1:56 PMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Phil


Tim's pointed out the error of my ways. Alter

DECLARE Result VARCHAR COLLATE ANSI_CI DEFAULT NULL;

to

DECLARE Result VARCHAR COLLATE ANSI_CI DEFAULT '';

and change the test back and it should work.



Roy Lambert
Thu, Oct 11 2012 8:35 PMPermanent Link

IQA

On 12/10/2012 4:56 AM, Roy Lambert wrote:
> Phil
>
>
> Tim's pointed out the error of my ways. Alter
>
> DECLARE Result VARCHAR COLLATE ANSI_CI DEFAULT NULL;
>
> to
>
> DECLARE Result VARCHAR COLLATE ANSI_CI DEFAULT '';
>
> and change the test back and it should work.
>
>
>
> Roy Lambert
>

Thanks Roy,

Yes that makes sense now, and it works well.

Thank you again for your time on this.

Phil Smile
« Previous PagePage 2 of 2
Jump to Page:  1 2
Image