| Author |
Topic |
|
jeanzz83
Starting Member
1 Post |
Posted - 2004-04-04 : 05:15:04
|
| i use this statemnet to add a column it will work but when i wanna drop that column it wont work "ALTER TABLE LA_LeaveBalance ADD abc SMALLINT NOT NULL DEFAULT 3" whereas if i use this statment "ALTER TABLE LA_LeaveBalance ADD abc SMALLINT" it works... and if i use the previous sql statement it gives me an error The object 'DF__LA_Balanc__accBa__540C7B00' is dependent on column 'accBalance'. ALTER TABLE DROP COLUMN accBalance failed because one or more objects access this column. anyone knows wats the problem ? thanx. btw i'm using vb.net |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-04-04 : 10:03:46
|
| You need to drop the default first. You have the name in the error message.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-04 : 14:36:03
|
| To help you with this, look up DROP DEFAULT in Books Online. It has a couple examples you can cut and paste out of the bottom.Start>All Programs>Microsoft SQL Server>Books OnlineMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2004-04-04 : 23:39:16
|
Here is a proc I wrote (you might want to sing it note for note.... ) that will drop any constraints on a column, then the column :/* * sp_DropCol * Pass in Table and Column name and it will drop any constraints on that column. * Then drop the column * Usage : Exec sp_DropCol 'Users', 'DepartmentID' */Create Proc sp_DropCol @TableName nVarchar(200), @ColName nVarchar(200)ASDeclare @CursSQL nvarchar(2000), @SQL nvarchar(2000), @ConstraintName nVarchar(300)SELECT @CursSQL = 'SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ' + 'WHERE TABLE_NAME = ''' + @TableName + ''' and ' + 'COLUMN_NAME = ''' + @ColName + ''''Create Table #constraints( ConstraintName nVarchar(300))INSERT INTO #constraintsExec(@CursSQL)Declare Cursor1 Cursor FOR SELECT * FROM #constraintsOPEN Cursor1FETCH NEXT FROM Cursor1 INTO @ConstraintNameWHILE @@FETCH_STATUS = 0 BEGIN SELECT @SQL = 'Alter Table M_Member Drop Constraint ' + @ConstraintName Exec(@SQL) FETCH NEXT FROM Cursor1 INTO @ConstraintNameENDClose Cursor1Deallocate Cursor1DROP Table #constraintsSELECT @SQL = 'Alter Table M_Member Drop Column ' + @ColNameExec(@SQL)GO Damian |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-04 : 23:42:00
|
Hey, that looks like a cursor. MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-04-04 : 23:51:23
|
| Lets shorten that a bit (haven't tried it so...)Create Proc sp_DropCol@TableName nVarchar(200),@ColName nVarchar(200)ASDeclare @SQL nvarchar(2000)SELECT identity(int,1,1) as id, CONSTRAINT_NAME into #aFROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGEWHERE TABLE_NAME = @TableNameand COLUMN_NAME = @ColNamedeclare @id intselect @id = 0while @id < (select max(id) from #a)beginselect @id = min(id) from #a where id > @idselect @sql = 'Alter Table ' + @TableName + ' Drop Constraint ' + CONSTRAINT_NAME from #a where id = @idexec (@sql)endSELECT @SQL = 'Alter ' + @TableName + ' Drop Column ' + @ColNameExec(@SQL)go==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2004-04-05 : 00:00:19
|
Fine then!But you lose points for not including a Bobby McFerrin reference Nice one Nigel, I hadn't thought about Information_Schema for that.Damian |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-04-05 : 00:02:51
|
| um - it's a direct copy from your post - I just got rid of the first dynamic sql query and used a temp table instead of a cursor.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-05 : 00:03:49
|
| Yeah, all of our code is too old to use that. They didn't even have views back then.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2004-04-05 : 00:28:05
|
quote: Originally posted by nr um - it's a direct copy from your post - I just got rid of the first dynamic sql query and used a temp table instead of a cursor.
Oh yeah, it is too OK, so I'm just an idiot today... I'll shut up nowDamian |
 |
|
|
|