SQL Server error 515 - sql

I have a table SINVOICE and another table called SINVOICE_LINE.
I need to put all columns of SINVOICE into SINVOICE_LINE.
I have created the corresponding columns and was trying to copy the values. The primary key of SINVOICE is SINVOICE_CODE, while the primary key for SINVOICE_LINE is a composite key (SINVOICE_CODE, SINVOICE_LINE_NUMBER).
I wrote the following query:
INSERT INTO SINVOICE_LINE (sinvoice.ITINERARY_CODE)
SELECT sinvoice_line.ITINERARY_CODE
FROM SINVOICE
INNER JOIN sinvoice_line ON sinvoice.sinvoice_code = sinvoice_line.sinvoice_code;
I get this error:
Cannot insert the value NULL into column SINVOICE_CODE, table SINVOICE_LINE; column does not allow nulls. INSERT fails.
I do not understand why I'm getting this error as I am not trying to insert any value in SINVOICE_CODE column.
Thanks!!!

Looks like you need UPDATE instead of INSERT here.
Try that:
UPDATE SINVOICE_LINE
SET ITINERARY_CODE = sinvoice.ITINERARY_CODE
from SINVOICE
WHERE sinvoice.sinvoice_code = sinvoice_line.sinvoice_code;

Related

How to Insert new Record into Table if the Record is not Present in the Table in Teradata

I want to insert a new record if the record is not present in the table
For that I am using below query in Teradata
INSERT INTO sample(id, name) VALUES('12','rao')
WHERE NOT EXISTS (SELECT id FROM sample WHERE id = '12');
When I execute the above query I am getting below error.
WHERE NOT EXISTS
Failure 3706 Syntax error: expected something between ')' and the 'WHERE' keyword.
Can anyone help with the above issue. It will be very helpful.
You can use INSERT INTO ... SELECT ... as follows:
INSERT INTO sample(id,name)
select '12','rao'
WHERE NOT EXISTS (SELECT id FROM sample WHERE id = '12');
You can also create the primary/unique key on id column to avoid inserting duplicate data in id column.
I would advise writing the query as:
INSERT INTO sample (id, name)
SELECT id, name
FROM (SELECT 12 as id, 'rao' as name) x
WHERE NOT EXISTS (SELECT 1 FROM sample s WHERE s.id = x.id);
This means that you do not need to repeat the constant value -- such repetition can be a cause of errors in queries. Note that I removed the single quotes. id looks like a number so treat it as a number.
The uniqueness of ids is usually handled using a unique constraint or index:
alter table sample add constraint unq_sample_id unique (id);
This makes sure that the database ensures uniqueness. Your approach can fail if two inserts are run at the same time with the same id. An attempt to insert a duplicates returns an error (which the exists can then avoid).
In practice, id columns are usually generated automatically by the database. So the create table statement would look more like:
id integer generated by default as identity
And the insert would look like:
insert into sample (name)
values (name);
If id is the Primary Index of the table you can use MERGE:
merge into sample as tgt
using VALUES('12','rao') as src (id, name)
on src.id = tgt.id
when not matched
then insert (src.id,src.name)

Copying values / columns from one table to another existing tabler in SQL Server Management Studio

I want to copy all columns from dbo.die to dbo.technology.
Both tables exist! In dbo.technology, the primary key is idTechnology
In dbo.die, the primary key is idDie and we have a foreign key, which is Technology_idTechnology in it, which connects the die table with the technology table.
How could I do that, so that the values got copied to the right rows, which match the same idTechnology?
I tried this:
INSERT INTO dbo.die
(Technology_idTechnology, Technology_D, Technology_Type, Technology_Manufacturer, Technology_SOI, Technology_Node, Technology_Name, Technology_Number_Metal, Technology_Number_Poly, Technology_Power_Cu, Technology_FEComplexity, Technology_FEComplexity_Sec, Technology_Trench, Technology_IMID, Technology_Remarks)
SELECT *
FROM dbo.technology tech
WHERE tech.idTechnology = idTechnology;
but I'm always getting an error!
Cannot insert duplicate key row in object 'dbo.die' with unique index 'ui_dieIdsample'. The duplicate key value is ().
Don't know what I should do.. I thought it's easy & simple
If a column is declared as NOT NULL (and has no default value), a value for the column must be specified in the INSERT statement.
In this specific case you should add Table2_Feld to the insert column list, and specify a value in the SELECT for it!
You will need to change your column list (lets say that its acceptable to insert a default value of 0 into column Table2_Feld)
INSERT INTO dbo.table2
(Table1_idTech, Tech_D, Techn_Type, Tech_Man,
Techn_Node, Tech_Name, Technology_Numb, Tech_Po,
Tech_FEC, Techn_Comp_Sec,
Tech_R,Table2_Feld)
select *,0 from table1 tech

Insert & Select

I am trying to create a column EAC into Projectdetails.
Table 1 : [Projectdetails]
Table 2 : ['RAC']
(SELECT b.[EAC] FROM [dbo].[Projectdetails] a
INNER JOIN [dbo].['RAC'] b ON a.[ProjectKey]= b.[Project _ID])
works fine and return me 421 rows.
However the following query does not work :
Insert into [dbo].[Projectdetails]([EAC])
(SELECT b.[EAC] FROM [dbo].[Projectdetails] a
INNER JOIN [dbo].['RAC'] b ON a.[ProjectKey]= b.[Project _ID])
returns me Cannot insert the value NULL into column ProjectKey
I suspect you really want an update:
update pd
set EAC = r.EAC
from [dbo].[Projectdetails] pd join
[dbo].['RAC'] r
on pd.[ProjectKey] = r.[Project _ID]);
By the way, putting single quotes in a table name seems like a really bad idea.
I guess, ProjectKey is a primary key as it's NOT NULL. So when you are
trying to insert new row in this table without providing any specific
value for this column, so sqlserver is trying to add NULL as a default
value. But as this is NOT NULL column it's giving you error.
The best solution to make it work is make this column as
auto-increment column. Like this
Sql Server add auto increment primary key to existing table
SQL Server, How to set auto increment after creating a table without data loss?

Inserting rows and updating rows on tables that may or may not have primary keys

I am trying to update a table so that rows that are missing are added and rows that are not up to date are updated using information from another table on a different database as a reference.
However, some tables have primary keys and some do not.
If there is a primary key the insert command will not run and if there is not a primary key, rows will duplicate.
Is there a way to have the insert command skip over primary key values that are already there?
I'm using sql server management studio 2005 and here is the code I have so far for a table with a primary key (PKcolumn):
INSERT [testDB].[dbo].[table1]
SELECT * FROM [sourceDB].[dbo].[table1]
UPDATE test
SET
test.[PKcolumn] = source.[PKcolumn]
,test.[column2] = source.[column2]
,test.[column3] = source.[column3]
FROM
[sourceDB].[dbo].[sourceDB] AS source
INNER JOIN
[testDB].[dbo].[PKcolumn] AS test
ON source.[PKcolumn] = test.[PKcolumn]
Update works perfectly but Insert will not run at all if there is even one duplicate.
Any suggestions on how to make this code work?
Also, any tips for doing the same thing on a table without a primary key?
You'll need to exclude rows that are already present in the table in your INSERT query, using a LEFT OUTER JOIN:
INSERT [testDB].[dbo].[table1]
SELECT * FROM [sourceDB].[dbo].[table1]
LEFT OUTER JOIN [testDB].[dbo].[table1] ON [sourceDB].[dbo].[table1].[PKcolumn] = [testDB].[dbo].[table1].[PKcolumn]
WHERE [testDB].[dbo].[table1].[PKcolumn] IS NULL
For a table without a primary key, I suppose you'd need to join on ALL columns to avoid duplicates.

Can't INSERT INTO SELECT into a table with identity column

In SQL server, I'm using a table variable and when done manipulating it I want to insert its values into a real table that has an identity column which is also the PK.
The table variable I'm making has two columns; the physical table has four, the first of which is the identity column, an integer IK. The data types for the columns I want to insert are the same as the target columns' data types.
INSERT INTO [dbo].[Message] ([Name], [Type])
SELECT DISTINCT [Code],[MessageType]
FROM #TempTableVariable
This fails with:
Cannot insert duplicate key row in object 'dbo.Message' with unique index
'IX_Message_Id'. The duplicate key value is (ApplicationSelection).
But when trying to insert just Values (...) it works ok.
How do I get it right?
It appears that the data "ApplicationSelection" is already in the database. YOu need to write the select to exclude records that are already in the database. YOu can do that with a where not exists clause or a left join. LOok up teh index to see what field is unique besides the identity. That will tell you what feild you need to check to see if teh record currently exists.