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
Related
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.
I have a table with an identity column (say Column1) and an integer column Column2 (nullable)
While inserting a row into this table, if the value passed to the Column2 is null, copy the Column1 value. If not null value is passed as argument for column2, retain the given value.
Only after the every insert, this check and update should be done.
But as per the instruction given to me, should not use default constraint and trigger (after insert).
This is for audit purpose. The record from this table will be moved/switched from one table to another table. The volume and transaction is very high (billions of records).
Can you help me on this?
Thanks,
Baskaran
Insert into tablename (column1, columns2)
Values (value1, ISNULL(Value2,value1))
I think this what you are expecting.. No trigger just on insert
If you really want to do this in a trigger, try something like this:
CREATE TRIGGER TrgAfterINsert
ON dbo.YourTable
AFTER INSERT
AS
UPDATE t
SET Column2 = i.Column1
FROM dbo.YourTable t
INNER JOIN Inserted i ON i.Column1 = t.Column1
WHERE t.Column2 IS NULL
This basically updates the YourTable table after an INSERT, and any rows that were inserted with a NULL in column2 are updated so that column2 is equal to the column1 value for that row
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).
I've found ways to pass an array to a stored procedure and ways to insert a table into another table. But I want to insert my array in a table as column2 with one other value I have as the value for column1:
INSERT INTO Table1 (column1, column2)
VALUES (SELECT #value, column2 from #otherTable)
I tried inserting the array into column2 first and then updating column1 to be the one value. But that didn't work and would be insanely expensive anyway.
Is there a reasonable way to do this?
If I'm understanding you correctly, then all you need is to get rid of the VALUES, like so:
INSERT INTO Table1 (column1, column2)
SELECT #value, column2 from #otherTable;
I think I will be asking a very confusing question so I hope I can make this very clear for you guys, so, thanks in advance, and I'm sorry if this is a bit long because I want to make sure that I am being clear enough.
I have two tables (in SQL 2005) which are kind of dependent to each other (let's name them TableA and TableB.) They have all the same columns, meaning, all TableA's columns are also TableB's columns, except that TableA has two more columns than TableB (it will be explained later why).
In the 'saving' process of my application (created using VB.Net), those records that has values for column1 and column2 gets inserted into TableA. But if there is no any input for column1 and column2, the record gets saved in Table2 (this is why TableA has two more columns than TableB: the records with column1 and column2 gets into TableA while those that don't have gets into TableB.)
Both tables have an identity primary key (auto-increment +1, starts at 1000), which we'll call KeyID_A for TableA and KeyID_B for TableB, and if you can already imagine the scenario, their key IDs have the same values (TableA has a row where the KeyID_A is '1000', and TableB also has a row (a different one) where KeyID_B is '1000').
In the application, the form (where the records being inserted in TableA/TableB are inputted) is auto-numbered (I set this using SELECT nextID=MAX(KeyID_A)+1 FROM TableA, yes, only from TableA, and that leads to my problem).
If I saved a record with values for column1 and column2, the record gets inserted in TableA, with KeyID_A's value as the key. But what if I don't have values for column1 and column2, and the record gets saved in TableB with KeyID_A's value as the key, how am I going to get the next ID (note: SELECT nextID=MAX(KeyID_A)+1 FROM TableA) if the supposed to be next ID for TableA is already used in TableB? I would want to get the next ID based on the existing records from both TableA and TableB, and not only based on TableA (because I thought, what if I have already used TableA's nextID for TableB (let's say '1001', and I want to save another record into TableB, but the auto-numbering that appears is still '1001' because it is based only from TableA's KeyID_A.
I am really confused on how to do this, if this is even possible. I hope I made my question clear enough.
Normally you should never do this the way you do.
Either you make it right which is:
solution 1:
TableA
Id : int PK (identity)
col1
col2
...
TableB
Id: int PK FK on TableA.Id
col8
col9
and create a view to make things simpler for you.
Solution 2:
Create table TableB with nullable col8 and col9
and a view called TableA if needed.
or the wrong way:
TableIdForAandB
Id : int PK identity
TableA
Id : int PK FK on TableIdForAandB.Id
col1
col2
...
TableB
Id : int PK FK on TableIdForAandB.Id
col1
col2
...
col8
col9
in which case you have to insert row to table TableIdForAandB and then insert row to table TableA or to TableB and set the Id to the new Id from TableIdForAandB.
Think about using ONE sequencer for both tables or if you don't have one think about using always
nextID = MAX(MAX(KeyID_A), MAX(KeyID_B)) + 1
in addition your IDs will be unique (very much better :-)
It is unclear why you are duplicating your data in 2 tables. However your given scenario can be handled using 1 to 1 link.
Your TableB will be your base table which has less columns, and TableA will be an extension table, but it will NOT have the columns which are already in TableB, it will only have the other 2 columns and a primary key.
In this way, you will always insert data in TableB first and if you have the data in other 2 columns then you will also insert a line in TableA, and at this point you will have the ID inserted in TableB, you will be using same ID.