Update table but shows errors in another table - sql-server

I'm currently maintaining an old system in my company. I want to update a table but it's giving errors in another table. Here is my query :
USE [Corporate]
GO
ALTER TABLE transfer_detail NOCHECK CONSTRAINT ALL
GO
UPDATE [dbo].[Transfer_Detail]
SET [Satuan] = 'DUS'
WHERE [Satuan] = 'CRT'
GO
ALTER TABLE transfer_detail CHECK CONSTRAINT ALL
GO
But the errors says :
Msg 515, Level 16, State 2, Line 22
Cannot insert the value NULL into column 'Transfer_Out', table 'Corporate.dbo.Saldo_Gudang'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
I've trying to figure out this problem with no success. What am I missing?

Check if there is any trigger exists on the table '[dbo].[Transfer_Detail]'?

Related

ALTER TABLE failed because one or more objects access this column. after running NOCHECK CONSTRAINT ALL

ALTER TABLE RoomReservation NOCHECK CONSTRAINT ALL
ALTER TABLE RoomReservation ALTER COLUMN HotelNumber INTEGER NOT NULL
I thought removing the contraint would allow me to make some necessary changes, but I was wrong.
Msg 5074, Level 16, State 1, Line 5
The object 'DF_RoomReservation_HotelNumber' is dependent on column 'HotelNumber'.
Msg 4922, Level 16, State 9, Line 5
ALTER TABLE ALTER COLUMN HotelNumber failed because one or more objects access this column.
Is there a way to make this work. I am not sure why removing all constraint on a table wouldn't allow me to run an alter table query.
Just guessing by looking at the name, 'DF_RoomReservation_HotelNumber' is probably a DEFAULT constraint, not a CHECK constraint. So try this statement first.
ALTER TABLE RoomReservation DROP CONSTRAINT DF_RoomReservation_HotelNumber

sql - update two key columns without conflict

I want to to update a column from table patient, and add a prefix before all its fields, like
update patient
set contactdetailsID = '99999'+ contactdetailsID
where patient.id = 5294
So, row with id = 5294, will have the same value in column contactdetailsID with prefix 99999.
Before | After
012345 99999012345
the issue i am facing is that patient.contactdetailsID is the foreign key for another table "contactdetails".
So i get the following error.
Msg 547, Level 16, State 0, Line 1 UPDATE statement conflicted with
COLUMN FOREIGN KEY constraint 'ContactDetails_Patient_FK1'. The
conflict occurred in database 'A', table 'ContactDetails',
column 'id'. The statement has been terminated.
(0 row(s) affected)
How can i make that change? I want to change both patient.contactdetailsID and its key Contactdetails.ID, with the same prefix in order not to lose the connection between them.
You can temporary disable the Foreign-Key Constraint using:
ALTER TABLE *Tablename*
NOCHECK CONSTRAINT *ForeinKey_Name*;
and update both the ContactDetails Table and Patient Table.
But make sure that you enable the ForeignKey afterwards!
To enable a Foreign Key use
ALTER TABLE *Tablename*
CHECK CONSTRAINT *ForeinKey_Name*;
You could implement ON UPDATE CASCADE in your table.
see MySQL documentation

Add column and update it within a transaction

I'm trying to add and update a column. This code is within a transaction.
ALTER TABLE [Foo] ADD SomeId INT NULL
UPDATE [Foo] SET SomeId = 1
ALTER TABLE [Foo] ALTER COLUMN SomeId INT NOT NULL
I get this error:
Msg 207, Level 16, State 1, Line 5 Invalid column name 'SomeId'.
I tried adding a GO statement after the firstALTER TABLE, but apparently that's invalid inside a transaction. How can I make this work inside a transaction?
Try this:
Begin Try
Begin Tran
Alter Table [Foo] Add SomeId INT NOT NULL Constraint TempConstraint Default (1)
Alter Table [Foo] Drop TempConstraint
End Tran
Essentially what this is doing is adding the new column with a default value constraint of 1. All current rows will get a value of 1. Then the default value constraint is being removed, so there will be no default value.
Since you can't mix DDL and DML statements in a single transaction, like what you wanted to do originally, this is your only alternative.

Change datatype of a column and set a column as identity in SQL

----------------------------------------------
DepartmentCode varchar(30) AllowNulls
----------------------------------------------
Does anyone know how to change the datatype of a column in SQL 2008? This is the column I want to alter but when I try this query,
ALTER TABLE SystemDepartment ALTER COLUMN DepartmentCode smallint NOT NULL
I get the following error:
Msg 5074, Level 16, State 1, Line 1
The object 'PK_SystemDepartment' is dependent on column
'DepartmentCode'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE
ALTER COLUMN DepartmentCode failed because one or more objects access
this column.
My question is how to force my query to cope with it? and I also would like to set this column as primary key and identity
You will first need to drop Primary Key constraint.
ALTER TABLE SystemDepartment DROP CONSTRAINT PK_SYSTEMDEPARTMENT
Then only you can ALTER that column.
You can not force existing column to identity. In this case you will need to add new column with identity and then do sp_rename to old name.
If your constraint is on a user type, then don't forget to see if there is a Default Constraint, usually something like DF__TableName__ColumnName__6BAEFA67, if so then you will need to drop the Default Constraint, like this:
ALTER TABLE TableName DROP CONSTRAINT [DF__TableName__ColumnName__6BAEFA67]
For more info see the comments by the brilliant Aaron Bertrant on this answer.
Try this ,
as you told you are getting primary key constraint error , 1st you have to drop the primary key and use the following query ,
ALTER TABLE SystemDepartment MODIFY DepartmentCode int(3)
Thanks,
Venkat.

what are the correct steps to alter a PK column of a table that is a merge replication source in SQL SVR 2005?

I have a table that has an identity column as the Primary Key. None of the data in the table is important at this point so I can delete it all, but I want to change the column from int to bigint and possibly reseed it to bigint.minvalue so as to give the largest possible range before running out of values.
I would like to do this without having to rebuild the replication or having to modify the subscribers.
If I execute
alter table MyTable
alter column MyTableId bigint not null
I get the errors:
Msg 5074, Level 16, State 1, Line 5
The object 'repl_identity_range_CEEB13F5_11D2_435C_BE5D_EBF91EBF8DE7' is dependent on column 'MyTableId'.
Msg 5074, Level 16, State 1, Line 5
The object 'PK_MyTable' is dependent on column 'MyTableId'.
Msg 4922, Level 16, State 9, Line 5
ALTER TABLE ALTER COLUMN MyTableId failed because one or more objects access this column.
You cant change a primary key on a published table as its part of the system used to push the data around ; so remove the table from the publication; make the changes and republish it.
You can drop your table using sp_droparticle http://msdn.microsoft.com/en-us/library/ms173832.aspx
Then make your table / your key changes
Finally use sp_addmergearticle to add it back in http://msdn.microsoft.com/en-us/library/ms174329.aspx

Resources