Teradata - get tablename and column name with the error message - sql

In Teradata Is there any way to get column name as well with the error message. For example I have a table
tablename column1 int,
tablename column2 timestamp,
tablename column3 timestamp,
tablename column4 timestamp,
tablename column5 char(20)
When i insert a wrong value in a column, it does not return me COLUMNNAME. For Example if i insert wrong time it just say 6760 : invalid timestamp
but which column is having problem we dont know.
is there is any mathod to know about it.

No, a SQL Insert will not return that info.
But when you use a MERGE with LOGGING errors instead you'll get a row in the error table indicating which column caused it (iirc).

Related

INSERT INTO a table with Computed and Default values

I have a TableA with several columns in it, one of which is a Computed value, and another is a DateTime with a default value of GETDATE().
Then I have another table, TableA_Staging, which I want to use as a raw dumping table for bulk inserts. This table looks a lot like TableA with a few expected differences, one of which being it doesn't have the Computed or the DateTime column in it.
Once I've done a bulk insert into TableA_Staging, I now need to move data from TableA_Staging to TableA. I'm running into a snag with those two columns. Let's assume TableA looks like this:
TableA
-----------
TableAId (INT non-unique, non-auto-incrementing PK)
Column1 (String PK)
Column2
ColumnComputed
ColumnDateTime
And...
TableA_Staging
-----------
TableAID (this value populated in C# code)
Column1
Column2
Now, I'm trying to do this:
INSERT INTO TableA
SELECT TableAID, Column1, Column2 FROM TableA_Staging WHERE TableAID > X
But I get this error:
Column name or number of supplied values does not match table definition.
I assume it's complaining because I am not providing anything for ColumnComputed or ColumnDateTime? But if so, I didn't think I would need to provide values for them, as one is computed, and the other has a default value.
You should always include the columns in your insert statement that you are inserting from your select, otherwise you always have to provide the same number of columns in your insert as your select.
Also if your TableID is autoincrement/identity you do not need to include that.
INSERT INTO TableA (TableBID, Column1, Column2)
SELECT TableAID, Column1, Column2
FROM TableA_Staging WHERE TableAID > X

All columns have NULLABLE problem when destination table with overwrite preference on Bigquery

I've running a query to make changes column data type and choose destination table queried table itself. I choose write preferece "Overwrite table". Table all columns are REQUIRED and table is not empty. But after run the query, all columns mode change NULLABLE. My cast query like :
SELECT
CAST(id AS STRING) as id, column1, column2
FROM
dataset.mytable;
Is it always that way, or I'm making a mistake?
I'm making a mistake?
Nope, it is by design, when you overwrite table original schema is lost and columns are nullable by default
Is it always that way?
You should use CREATE OR REPLACE TABLE DDL statement to achieve your goal. Something like below
CREATE OR REPLACE TABLE `project.dataset.mytable` (
id STRING NOT NULL,
column1 INT64 NOT NULL,
column2 INT64 NOT NULL
) AS
SELECT CAST(id AS STRING) as id, column1, column2
FROM `project.dataset.mytable`

SQL Server : INSERT INTO SELECT doesn't insert into the correct column

I'm using SQL Server 2012 to try to take the values of one column in a table and put them into the values of another column table in another. If I try to run the following query:
INSERT INTO table2 (column3)
SELECT column3
FROM table1
WHERE (ScopeID IS NOT NULL)
ORDER BY Name
For table2, column3 is the same type (an int), NULL values are allowed. But when I try to execute the query, it returns:
Cannot insert the value NULL into column 'column1', table 'dbo.table2';, column does not allow nulls. INSERT fails.
But I'm not trying to insert into column1... Is it just a syntax thing where the order of the columns HAVE to match?
You are inserting into column1. Remember, you are inserting entire rows of values, so you should really have a value for all columns. Your query is equivalent to:
INSERT INTO table2 (column1, column2, column3)
SELECT NULL, NULL, column3
FROM table1
WHERE (ScopeID IS NOT NULL)
ORDER BY Name;
(and so on for all the columns in the table.)
I am guessing that you actually want an update, but your question doesn't provide enough information to give further guidance.

ORA-00947 - not enough values: Occurs in one server but not another

I am work on a project which has to add one column to the exist table.
It is like this:
The OLD TBL Layout
OldTbl(
column1 number(1) not null,
column2 number(1) not null
);
SQL TO Create the New TBL
create table NewTbl(
column1 number(1) not null,
column2 number(1) not null,
**column3 number(1)**
);
When I try to insert the data by the SQL below,
on one oracle server,it was successful executed,
but on another oracle server, I got "ORA-00947 error: not enough values"
insert into NewTbl select
column1,
column2
from OldTbl;
Is there any oracle option may cause this kind of difference in oracle?
ORA-00947: not enough values
this is the error you received, which means, your table actually has more number of columns than you specified in the INSERT.
Perhaps, you didn't add the column in either of the servers.
There is also a different syntax for INSERT, which is more readable. Here, you mention the column names as well. So, when such a SQL is issued, unless a NOT NULL column is missed out, the INSERT still work, having null updated in missed columns.
INSERT INTO TABLE1
(COLUMN1,
COLUMN2)
SELECT
COLUMN1,
COLUMN2
FROM
TABLE2
insert into NewTbl select
column1,
column2
from OldTbl;
The above query is wrong, because your new table has three columns, however, your select has only two columns listed. Had the number and the order of the columns been same, then you could have achieved it.
If the number of the columns, and the order of the columns are different, then you must list down the column names in the correct order explicitly.
I would prefer CTAS(create table as select) here, it would be faster than the insert.
CREATE TABLE new_tbl AS
SELECT column1, column2, 1 FROM old_tbl;
You could use NOLOGGING and PARALLEL to increase the performance.
CREATE TABLE new_tbl NOLOGGING PARALLEL 4 AS
SELECT column1, column2, 1 FROM old_tbl;
This will create the new table will 3 columns, the first two columns will have data from the old table, and the third column will have value as 1 for all rows. You could keep any value for the third column as per your choice. I kept it as 1 because you wanted the third column as data type NUMBER(1).

SQL insert into fails using substring from same table

I would like to create process using substring from table1.column2 and insert results into table1.column6. I am trying the query below but my insert fails. It tries to insert into another column (column1) which is the id column. I specify column6 but it does not work.
Insert into table1(column6)
Select substring(column1,1,3)
from table1
Error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'Column1ID', table 'dbo.table1'; column does not allow nulls. INSERT fails.
If your table contain data , but column6 is empty (null), you must use update command
update table1 set column6 = substring(column1,1,3)
that use data stored into table1 and update column6
There is a fundamental difference between the insert and update statements - insert creates a brand new row, whereas update updates a row that already exists (even if data in a particular column for a row does not yet exist)
What are differences between INSERT and UPDATE in MySQL? - i realize you're sql server but same principle applies.
It sounds like you may actually be trying to update a column value, not insert a new row with a value in a column.
Given the following table and data:
table1
----
Id int primary key identity(1,1)
column1 varchar(50) not null
column6 varchar(50) null
id column1 column6
-----
1 some value null
2 some other value null
note that if you were to attempt your query:
insert into table1(column6) select substring(column1,1,3) from table1
this would attempt to take "some value" and "some other value" into new rows 3 and 4, with null in column1, and the substring values into column6. Note that this would fail because you're attempting to insert rows into table1 with null values (column1) in a not nullable column.
what you (probably) actually want is something like:
update table1
set column6 = substring(column1,1,3)
which will update the table for (in this case all rows) setting column6s value to the substring values of column1
I think you are trying to update the table. Use ISNULL to avoid null values.
UPDATE table1 SET column6 = ISNULL(SUBSTRING(column1,1,3), '')
If you insist on insert. Do check all the columns accept null other than Column6. And Query will insert a bulk of data.
Insert into table1(column6)
Select ISNULL(SUBSTRING(column1,1,3), '')
from table1