How to make postgres tell which column is causing an error - sql

While inserting data into a table which has many columns:
INSERT INTO MyTable ("name", ..100+ columns)
VALUES ('Michel', ... 100+ values)
I made an error creating a specific value so PostgreSQL tells us:
ERROR: value too long for type character varying(2)
I would like to avoid going through the whole table schema to guess which column is failing.
Is there a way to configure PostgreSQL so it tells us which column is causing the error?

One quick way might be to query the information schema table for your database and look for character columns having a maximum width of 2 (to which your error is alluding):
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MyTable' AND
character_maximum_length = 2;

Related

How to find the name of a table based upon a column name and then access said table

I have a column name "CustomerIDClass" and I need to find the table it's associated with within an entire Oracle database.
I've run this to determine the owner and name of the table where this column name appears:
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CustomerIDClass%';
and I'm getting this response:
I don't have enough reputation to post the image, so here's the link: http://i.imgur.com/a7rcKoA.png
I have no idea how to access this (BIN$Csew==) table. When I try to use it as a table name I get errors or messages saying that no rows were returned.
My main goal here is to write a simple statement that lets me search the database for the "CustomerIDClass" and view the table that contains this column name.
This table is in the recycle bin. You have to issue FLASHBACK TABLE "Customer1"."BIN$Csew==$0" TO BEFORE DROP command, given you have the appropriate privileges.
Doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9012.htm
Do note that in oracle the column names are stored in capital but you are using mixed case in your like statement therefore the select clause will not return any result
Try the below
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CUSTOMERIDCLASS%';

How to select record of different data type from sql column

I have two a table and a view . The table if of two rows of datatypes nvarchar and money. I have being updating the table by selecting from the view like below.
Insert into MyTable
Select * from MyView
Recently, this update fails due to an error "String or binary data would be truncated." However, when i modified by select statement to something like.
Select * from Myview WHERE Column is not null
OR
Select * from Myview WHERE Column > 0
The above work with a warning saying Warning: Null value is eliminated by an aggregate or other SET operation. . It occurred to me that may may be one of the null value records contain something that's not null. My table column is of money type and accept null. I presumed the error may be due to something that's not of money data type. The record is huge. Is there any way i can filter and return those aliens records?
I also i learnt that i can eliminate the error by turning ANSI WARNING SETTION ON & OFF Here . My concern is wouldn't that result in loss of data. Please any help would be appreciated.
String or binary data would be truncated happened because the data coming from the MyView is larger than the column size in MyTable
Use
Select Max(Len(FieldName)) From MyTable
to check the maximum length of the nvarchar field in the MyTable
Or you can use Left when inserting data something Llike this
Insert into MyTable
Select Left(FieldName,50), Column1 from MyView
Note the 50 should be the size of the nvarchar field in MyTable
String or binary data would be truncated is a very common error. It usually happens when we try to insert any data in string (varchar,nvarchar,char,nchar) data type column which is more than size of the column. So you need to check the data size with respect to the column width and identify which column is creating problem and fix it.
Here is another thread of the same problem as yours in stackoverflow.
string or binary data would be truncated
Hope this will help.
Regards
looks like the data in some column in table MyView exceeds the limit of the corresponding one in table MyTable

Mysql Query data filled check

I had created the table with 200 columns and i had inserted data
Now i need to check that specific 100 columns in one row are filled or not,how can we check this using mysql query .the primary key is defined .please help me out how to resolve this.
select * from tablename where column1 != null or column2 != null ......
That is a lot of columns so at the risk of being mysql server version specific you can use the information schema to get the column names and then write a SQL procedure or something in your chosen shell / language that iterates over them performing a test.
select distinct COLUMN_NAME as 'Field', IS_NULLABLE from information_schema.columns where TABLE_SCHEMA="YourDatabase" and TABLE_NAME="YourTableName" and TABLE_NAME not like "%view%" escape '!' ;
The example above will tell you the column name as "Field" and tell you if it can hold a NULL. Having the field name may give you a better way of automating a field name specific test.

Performing An SQL Update With Subqueries For Column Names

This is a really conviluted piece of SQL, I know, but basically one of the data entry monkeys where I work screwed up big-time, and the copy has gotten deep into our system.
There is a rather large number of tables that need to have a value changed from VAL1 to VAL2, if another value is equal to VAL3. The problem is that I don't know all of the tables where this is the case, and there is strict column naming policy that means that all tables have unique column names.
I wrote the following SQL to attempt to do this update, but it doesn't work:
UPDATE
(SELECT DISTINCT TABLE_NAME AS tbTableName
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME LIKE '%MAJR%')
SET
(SELECT COLUMN_NAME AS tbColumnName
FROM tbTableName
WHERE COLUMN_NAME LIKE '%MAJR%') = 'VAL2'
WHERE
(SELECT COLUMN_NAME AS tbColumnNameWhere
FROM tbTableName
WHERE COLUMN_NAME LIKE '%PROGRAM%') = 'VAL3'
AND tbColumnName = 'VAL1';
But yeah, this falls over with the error: invalid user.table.column, table.column, or column specification
01747. 00000 - "invalid user.table.column, table.column, or column specification"
Any help would be appreciated.
You can't do subqueries like this, unfortunately. Your best bet is to use PHP or whatever your primary scripting language is and query the database to build all the SQL statements for you.

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'