Update query error - sql

I am trying to update records in table tciSTran that are present in the table tarRecurInvcLineDS
Here is the query I wrote...
update [mas500_test_app].[dbo].[tciSTaxTran]
set STaxSchdKey = 310
where STaxTranKey in
(select STaxTranKey from [mas500_test_app].[dbo].[tarRecurInvcLineDs]
I'm getting this error
Unable to update the tciSTaxTran record because it is trying to reference a record in tciSTaxSchedule that does not exist.

Seems you have a foreign key between tciSTaxTran.STaxSchdKey and the tciSTaxSchedule table. Does the tciSTaxSchedule table have a row with a primary key of 310?

Related

Why is UPDATE statement not working in SQL code?

I am trying to update one row of the storeID column. When I do the first code below, it runs but will not affect any row. However, the bottom two are producing the following error " The UPDATE statement conflicted with the REFERENCE constraint "Products_FK". The conflict occurred in database "group7", table "dbo.Products", column 'storeID'."
Could anyone help? Thanks!
Table in SQL
UPDATE Store
SET storeID='E50'
WHERE storeID='D50'
AND storeID is Null;
UPDATE Store
SET storeID='E50'
WHERE storeID='D50'
UPDATE Store
SET storeID='E50'
WHERE storeName='A Plus Cables'
Above is the code that I have tried and nothing is being updated.
The first one can't possibly match any rows, because a single value can't equal two different things at the same time:
WHERE storeID='D50'
AND storeID is Null
So that one is kind of a moot point. You'd need to update the WHERE clause to target the record(s) you want to target.
For the latter two, the error is telling you what's wrong. You're trying to write this value to a column:
SET storeID='E50'
But the error is telling you:
That column is a foreign key to another table. (Products?)
The value you're writing isn't presentin that other table.
So your options are:
Use a value that is present in the other table.
Use NULL (and update the column to allow NULL if necessary).
Remove the foreign key constraint to that other table.

How to prevent delete query in SQL

I have created a database through Entity Framework Code First Approach and My application is ready and running live . The problem is that I did not turned "False" on Cascade Delete at the time of creating database.
Now if I delete any record from one table that is referenced with another table through foreign so all the record containing foreign key of deleted row is deleted from another table .
Practically demonstration :
Let say I have a Table called Passenger:
ID Name CategoryID
1 ABC 1
CategoryID here is a foreign key
Here is the category Table
ID Name
1 Gold
Let say I run my query on category table
delete from Category where ID = 1
Now all the record from my Passenger Table is deleted . I want to restrict it. Is it Possible through SQL now ?
I suppose
This is what you are looking for :
alter TRIGGER customers_del_prevent
ON dbo.customers
INSTEAD OF DELETE
AS
BEGIN
insert into dbo.log
values ('DELETE')
RAISERROR ('Deletions not allowed from this table (source = instead of)', 16, 1)
END
Hope this helps you. :)

SQL Delete Query Foreign Key constraint

I am trying to Delete record with primary key as foreign key in two tables. Everytime I try to run the query it gives me this error:
Query:
DELETE FROM BusinessPlus_Post
FROM BusinessPlus_Post
INNER JOIN BusinessPlus_PostImage ON BusinessPlus_Post.Post_ID = BusinessPlus_PostImage.BusinessPlusPost_ID
INNER JOIN BusinessPlus_PostTag ON BusinessPlus_Post.Post_ID = BusinessPlus_PostTag.BusinessPlusPost_ID
WHERE
(BusinessPlus_Post.Post_ID = 3)
AND (BusinessPlus_PostImage.BusinessPlusPost_ID = 3)
AND (BusinessPlus_PostTag.BusinessPlusPost_ID = 3)
Error:
The DELETE statement conflicted with the REFERENCE constraint
"FK_BusinessPlusPostImage". The conflict Occurred in database
"BusinessPlus_AdminPanel_Database", table
"dbo.BusinessPlus_PostImage", column 'BusinessPlusPost_ID'. The
statement was terminated.
Right now, you are only stating that you want to delete the BusinessPlus_Post record, but not the BusinessPlus_PostImage and BusinessPlus_PostTag records. This would lead to problems, as we then would have 'orphan' PostImage and PostTag records with no corresponding Post records.
Apparently, it is not possible to delete from multiple tables in SQL Server (it is supported in MySQL, for example).
You have to split your queries, and delete from the 'child' tables first:
DELETE FROM BusinessPlus_PostImage
WHERE BusinessPlusPost_ID = 3
DELETE FROM BusinessPlus_PostTag
WHERE BusinessPlusPost_ID = 3
DELETE FROM BusinessPlus_Post
WHERE Post_ID = 3
Error: The DELETE statement conflicted with the REFERENCE constraint
"FK_BusinessPlusPostImage". The conflict Occurred in database
"BusinessPlus_AdminPanel_Database", table
"dbo.BusinessPlus_PostImage", column 'BusinessPlusPost_ID'. The
statement was terminated.
Error denotes that you have data referencing the foreign ,hence you cannot delete.
Delete the datas in BusinessPlus_AdminPanel_Database table
dbo.BusinessPlus_PostImage, column BusinessPlusPost_ID ,and then try delete

SQL Server error 515

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;

SQL Server : Changing an ID to an already existing one (merge) HOW TO?

I have two records that are the same in a table (entered by mistake). Both IDs are used as foreign key in other tables. I want to update the foreign keys to one "orignal" element and delete the other one. The problem is that it's possible that the UPDATE of the foreign key will generate a constraint exception (if the foreign key with the original element already exists).
So I would do something like :
UPDATE foreignTable SET id=1 WHERE id=2
DELETE FROM firstTable WHERE id=2
The problem is with the UPDATE, I would like to do the UPDATE if the row doesn't already exists, if yes just DELETE the row. How do you do that?
UPDATE ft
SET id = 1
FROM foreignTable ft
LEFT JOIN foreignTable ft2
ON ft.PrimaryKey = ft2.PrimaryKey
AND ft2.id = 1
WHERE ft.id = 2
AND ft2.PrimaryKey IS NULL
DELETE FROM foreignTable
WHERE id = 2
If you are using SQL Server 2008, have a look at the MERGE statement.
It allows you to insert the missing rows, update the existing one and delete those who have to be deleted.
http://technet.microsoft.com/en-us/library/bb510625.aspx
If you use an older version, you will have to copy your data to a temporary table, delete the data from the existing one and reinsert from the temp table.
Be sure to use a transaction and make a backup of your table to avoid data loss.