سؤال

I have redesigned my Firebird based SQL database. As part of this process I created new table named HARMONICS. Using GUI based administration tool I created following columns:

HARM_LP_ID Integer NOT NULL
HNUMBER Integer NOT NULL
HAMPCHNL0 Double precision
HAMPCHNL1 Double precision
HAMPCHNL2 Double precision
HAMPCHNL3 Double precision
HAMPCHNL4 Double precision
HAMPCHNL5 Double precision
HPHCHNL0 Double precision
HPHCHNL1 Double precision
HPHCHNL2 Double precision
HPHCHNL3 Double precision
HPHCHNL4 Double precision
HPHCHNL5 Double precision

After that I inserted into new entities some test data. The changes included also other tables, but these are not related to my problem so I will not list them here. Then I customized my application in order to work with new structure of relational database.

When application was fully tested I enhanced utility named Upgrader. The task of Upgrader is to convert between various versions of SQL databases. To accomplish this task I wrote SQL script. Following erroneous guides on official Firebird site (http://firebirdsql.org/manual/migration-mssql-data-types.html) I wrote following command for creating above mentioned table:

CREATE TABLE HARMONICS
(
  HARM_LP_ID Integer NOT NULL,
  HNUMBER Integer NOT NULL,
  HAMPCHNL0 Float,
  HAMPCHNL1 Float,
  HAMPCHNL2 Float,
  HAMPCHNL3 Float,
  HAMPCHNL4 Float,
  HAMPCHNL5 Float,
  HPHCHNL0 Float,
  HPHCHNL1 Float,
  HPHCHNL2 Float,
  HPHCHNL3 Float,
  HPHCHNL4 Float,
  HPHCHNL5 Float,
  CONSTRAINT PKHARMONICS1 PRIMARY KEY (HARM_LP_ID,HNUMBER)
);

I thought that Firebird data type "Float" is double precision float (the same as data type "double" in C). I eventually found that I was wrong but this was after I created official release of Upgrader utility. So now I have two versions of databases in circulation - one with single precision floating point numbers and second with double precision floating point numbers.

I am searching for SQL script that would check actual data type of columns in table HARMONICS. If this is double precision then script would do nothing. If this is single precision, script would convert columns with data type "Float" to "Double precision" with conserving all existing data. The point is script would not return error in either case. Is this possible? If yes then how?

هل كانت مفيدة؟

المحلول

You can check an actual type of a column using next query

SET TERM ^ ;
EXECUTE BLOCK AS BEGIN
  if (exists(select
    rf.rdb$field_name,
    f.rdb$field_type
    from
      rdb$fields f join rdb$relation_fields rf
      on rf.rdb$field_source = f.rdb$field_name
    where
      rf.rdb$relation_name = 'YOUR_TABLE_NAME'
      AND rf.rdb$field_name = 'YOUR_FIELD_NAME'
      AND f.RDB$FIELD_TYPE = 10)) then
  execute statement 'ALTER TABLE YOUR_TABLE_NAME ALTER YOUR_FIELD_NAME TYPE DOUBLE    PRECISION';
END^
SET TERM ; ^

rdb$field_type for FLOAT is 10, for DOUBLE PRECISION is 27.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top