Alter Field Type from nvarchar(255) to nvarchar(Max) in SQL Server 2005 - sql-server-2005

I would like to alter column type in table stk020. So, I used this query..
ALTER TABLE [stk020]
ALTER COLUMN [t16] nvarchar(Max) not null
The original column type of [t16] is nvarchar(255). I get this error
Msg 5074, Level 16, State 1, Line 1
The object 'DF_STK020_t16__725CC34D' is dependent on column 't16'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN t16 failed because one or more objects access this column.
Any solution?

You must first drop what we presume is the Default constraint on the column before you alter its data type:
Alter Table stk020 Drop Constraint DF_STK020_t16__725CC34D
GO
Alter Table stk020 Alter Column t16 nvarchar(max) not null
GO
Alter Table stk020 Add Constraint DF_STK020_t16__725CC34D
Default ... For t16

Related

My INSERT statement conflicted with the FOREIGN KEY constraint

enter image description here
Those are my diagrams - I create procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[hasta]
#Ad nchar(50),
#Soyadı nvarchar(100),
#TcKimlik nchar(11),
#DogumTarihi varchar(8),
#TelefonNo nvarchar(11)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].Hastalar ([Ad], [Soyadı], [TcKimlik], [DogumTarihi], [TelefonNo])
VALUES (#Ad, #Soyadı, #TcKimlik, #DogumTarihi, #TelefonNo)
END
After I want add data in Hastalar table, I write
exec hasta 'Emir','Yılmaz','35635993564','1995.11.19','05347331085'
but I get this error
Msg 547, Level 16, State 0, Procedure hasta, Line 13
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Hastalar_Testler". The conflict occurred in database "hastane", table "dbo.Testler", column 'TestID'. The statement has been terminated.
In your table [dbo].Hastalar, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help '[dbo].Hastalar'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.
What part of the error do you not understand?
The message is saying that one of the columns -- "TestId" -- is referring to another table. And the value being inserted is not already in that table.
You are not explicitly inserting a value for TestId, so that value is being set using a default constraint or trigger. I would suggest that you explicit provide the value in the insert -- even if that value is NULL.

postgresql: whats wrong in my sql statement of altering a column in a table

I am trying to alter a tables column with a default value of -11 and it can be null
ALTER TABLE devicedata ALTER COLUMN "weather" smallint NULL DEFAULT -11 ;
it says error
In Postgres the syntax for this is:
alter table device_data alter column wheather type smallint;
Then you can change the default:
alter table device_data alter column wheather set default 10;
The column keyword is optional.

Error while dropping a column

I just created a table as below in MS SQL
create table Employee
(emp_id smallint not null,
employee_name varchar (30) not null,
salary money not null,
department varchar(30),
address varchar(40),
primary key(emp_id)
)
After creating the table, I feel like auto populating the emp_id column( using Identity). So, I was trying to drop the column emp_id as below:
alter table Employee
drop column emp_id
Even though, I haven't inserted any rows in the table yet, I am getting the error
Msg 5074, Level 16, State 1, Line 1 The object
'PK__Employee__1299A86183CA4FBC' is dependent on column 'emp_id'. Msg
4922, Level 16, State 9, Line 1 ALTER TABLE DROP COLUMN emp_id failed
because one or more objects access this column.
Please help!!!
Something like this can help .
ALTER TABLE Employee
DROP CONSTRAINT PK__Employee__1299A86183CA4FBC;
alter table Employee
drop column emp_id
I solved the problem by executing below query: I need to remove a column and all the entries from that column to free my DB size my initial table structure is as shown below:
CREATE TABLE words(_id,word,synonyms,favorite,history,updDate)
And I wanted the table in below form
CREATE TABLE words(_id,word,favorite,history,updDate)
So I executed below query and it removed "synonyms" column
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(_id,word,favorite,history,updDate);
INSERT INTO t1_backup SELECT _id,word,favorite,history,updDate FROM words;
DROP TABLE words;
CREATE TABLE words(_id,word,favorite,history,updDate);
INSERT INTO words SELECT _id,word,favorite,history,updDate FROM t1_backup;
DROP TABLE t1_backup
COMMIT;

sql server modify error

i m not able to modify table in SQL server. i m new to databases.
use work
go
alter table employee
modify id varchar(20)
error message is-
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'modify'
here is an screenshot
thanks
You have the syntax for altering a table wrong. You need:
ALTER TABLE YourTable
ALTER COLUMN ExistingColumn VARCHAR(20)
The syntax should be
ALTER TABLE Employee ALTER COLUMN ID VarChar (20)
Here is the ALTER COLUMN syntax.
http://msdn.microsoft.com/en-us/library/ms190273.aspx
Now, having said all that, I have a question for you.. Why is your ID column a VarChar as opposed to an Identity Column?

How do I drop a column with object dependencies in SQL Server 2008?

The error message I'm obtaining when trying to drop a column:
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 43
ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or more objects access this column.
I have already tried to find the default constraints, as described here:
SQL Server 2005 drop column with constraints
Unfortunately without any success :( The line returned is:
fkKeywordRolleKontakt 2 814625945 0 defEmptyString
And I cannot remove either of fkKeywordRolleKontakt and defEmptyString.
What is the correct way to get rid of this dependency?
EDIT: Perhaps this is of importance too. The column fkKeywordRolleKontakt is of type udKeyword (nvarchar(50)) with default dbo.defEmptyString.
Edit 2: Solved
I could solve the problem now. I'm sorry, I did not copy the full error message, which was:
Msg 5074, Level 16, State 1, Line 1
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 1
The object 'FK_tlkpRolleKontakt_tlkpKeyword' is dependent on column 'fkKeywordRolleKontakt'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or more objects access this column.
I could generate a script to drop the column by right-clicking on the column entry (dbo.tlkpRolleKontakt > Columns > fkKeywordRolleKontakt) (in MSSQL Server Manager), selecting Modify and deleting the column. Then Table Designer > Generate Change Script generated the necessary commands:
ALTER TABLE dbo.tlkpRolleKontakt
DROP CONSTRAINT FK_tlkpRolleKontakt_tlkpKeyword
EXECUTE sp_unbindefault N'dbo.tlkpRolleKontakt.fkKeywordRolleKontakt'
ALTER TABLE dbo.tlkpRolleKontakt
DROP COLUMN fkKeywordRolleKontakt
That's it :)
Did you try first:
ALTER TABLE <tablename> DROP CONSTRAINT defEmptyString;
?
drop the constraint which is dependent on that column with
ALTER TABLE TableName DROP CONSTRAINT dependent_constraint
Then Drop Column:
ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME
dependent_constraint : this constraint is shown in the error when we try to delete dependent column.
Example: trying to drop some column IsDeleted2
Error
The object 'DF__Employees__IsDel__15502E78' is dependent on column
'IsDeleted2'.
ALTER TABLE DROP COLUMN IsDeleted2 failed because one or more objects
access this column.
Error clearly states that we need to delete DF__Employees__IsDel__15502E78 constraint
ALTER TABLE Employess
DROP CONSTRAINT DF__Employees__IsDel__15502E78;
Drop Column: ALTER TABLE Employess DROP COLUMN IsDelted2
I could solve the problem now. I'm sorry, I did not copy the full error message, which was:
Msg 5074, Level 16, State 1, Line 1
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 1 The object
'FK_tlkpRolleKontakt_tlkpKeyword' is dependent on column
'fkKeywordRolleKontakt'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or
more objects access this column.
I could generate a script to drop the column by right-clicking on the column entry (dbo.tlkpRolleKontakt > Columns > fkKeywordRolleKontakt) (in MSSQL Server Manager), selecting Modify and deleting the column. Then Table Designer > Generate Change Script generated the necessary commands:
ALTER TABLE dbo.tlkpRolleKontakt
DROP CONSTRAINT FK_tlkpRolleKontakt_tlkpKeyword
EXECUTE sp_unbindefault N'dbo.tlkpRolleKontakt.fkKeywordRolleKontakt'
ALTER TABLE dbo.tlkpRolleKontakt
DROP COLUMN fkKeywordRolleKontakt
use this script to cancel the checking of constraint :
ALTER TABLE #tablename NOCHECK CONSTRAINT #constraintname
I ran into a simpler solution.
DELETE the data of that column.
Once the column has no value inside it do -
ALTER TABLE <table_name> DROP COLUMN <column_name>
This way the column is easily dropped.
P.S - This is a headache if you have like extreme amounts of data in the column.