Invalid command, Data type unknown


You can often get this error when creating views with unions. The reason is that Firebird is very strict with datatypes, and when you have different datatype in queries that make up the UNION it throws an error. It can happen even if difference is something trivial like CHAR(5) vs CHAR(6). It also happens for decimal and double datatypes, and (one of the most annoying) when you put NULL or zero. Examples:

create view extreeme_employee (first_name, job_grade)
as
select 'Undisclosed', job_grade
from EMPLOYEE
where job_grade = (select max(job_grade) from EMPLOYEE)

union

select first_name, job_grade
from EMPLOYEE
where job_grade = (select min(job_grade) from EMPLOYEE);

or:

create view extreeme_employee (first_name, job_grade)
as
select first_name, job_grade
from EMPLOYEE
where job_grade = (select max(job_grade) from EMPLOYEE)

union all

select first_name, 0
from EMPLOYEE
where job_grade = 0;

The solution is to cast the value to appropriate datatype, so that all queries return the same datatype for each column.

Firebird 2 has much better type coercion as it detects the largest datatype, and allows smaller ones to fit automatically, i.e. if you have CHAR(5) in one query and CHAR(6) in other, it will automatically set view's column to be CHAR(6). Please note that datatypes have to be compatible for this to work (for example, you cannot union a Blob and Decimal column).


Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.



All contents are copyright © 2007-2024 FirebirdFAQ.org unless otherwise stated in the text.


Links   Firebird   News   FlameRobin   Powered by FB: Home Inventory   Euchre  
Add content   About  

Categories
 Newbies
 SQL
 Installation and setup
 Backup and restore
 Performance
 Security
 Connectivity and API
 HOWTOs
 Errors and error codes
 Miscellaneous