Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 How to drop column in sqlserver including constraints

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-05 : 08:50:15
Srisahana writes "How to drop column in sqlserver including constraints for that column. I want this to be done in ASP.

I mean, I want to wite that query in ASP using Stored Pocedures."

izaltsman
A custom title

1139 Posts

Posted - 2001-12-05 : 16:32:16
I would strongly question the need to manipulate database structure from ASP. In most cases database structure should be static. It is probably better to modify your application, so that front end would not need to make these sort of changes.

Having said that, if you still feel you need to drop columns from ASP, you can drop them using a statement like:

ALTER TABLE mytable DROP COLUMN mycolumn

However, if a column you are trying to drop is a part of an index, or has other tables referencing it, you will not be able to drop it. The stored procedure below will drop all foreign keys referencing your column and all indexes containing it prior to attempting to drop the column. I have done very little testing, so be sure to test thoroughly if you decide to implement...


CREATE PROCEDURE drop_column
@owner varchar (100) = 'dbo'
, @table sysname
, @col sysname
AS

DECLARE @sql varchar (8000)
DECLARE @idx_name sysname
DECLARE @stat int
-- Make sure that such table/column exists

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table
AND TABLE_SCHEMA =@owner
AND COLUMN_NAME = @col)
BEGIN
RAISERROR ('Invalid owner, table or column parameter', 16, 1)
RETURN 1
END

--Pick a moment while nr isn't looking, create a cursor of foreign keys, referencing the field
--Loop through and drop all of them.
DECLARE cur_fks CURSOR FAST_FORWARD READ_ONLY TYPE_WARNING
FOR
select 'ALTER TABLE '+ object_name(fkeyid) +' DROP CONSTRAINT [' + object_name(constid) + ']'
FROM sysreferences WITH (NOLOCK) WHERE object_name(rkeyid) = @table
and @col IN ( col_name(rkeyid, rkey1)
, col_name(rkeyid, rkey2)
, col_name(rkeyid, rkey3)
, col_name(rkeyid, rkey4)
, col_name(rkeyid, rkey5)
, col_name(rkeyid, rkey6)
, col_name(rkeyid, rkey7)
, col_name(rkeyid, rkey8)
, col_name(rkeyid, rkey9)
, col_name(rkeyid, rkey10)
, col_name(rkeyid, rkey11)
, col_name(rkeyid, rkey12)
, col_name(rkeyid, rkey13)
, col_name(rkeyid, rkey14)
, col_name(rkeyid, rkey15))


OPEN cur_fks

FETCH NEXT FROM cur_fks INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
exec (@sql)
FETCH NEXT FROM cur_fks INTO @sql
END

CLOSE cur_fks
DEALLOCATE cur_fks

-- Pray that nr still isn't looking. Create a cursor of
-- indexes and statistics, that the field is participating in.
-- Loop through and drop indexes/stats
DECLARE cur_idx CURSOR FAST_FORWARD READ_ONLY TYPE_WARNING
FOR
select distinct i.name, i.status as stat
from sysindexkeys k with (nolock)
inner join sysindexes i with (nolock )
on k.id = i.id and k.indid = i.indid
where k.id = object_id('['+@owner+'].['+@table+']')

OPEN cur_idx

FETCH NEXT FROM cur_idx INTO @idx_name, @stat

WHILE @@FETCH_STATUS = 0
BEGIN
IF @stat & 64 = 0 -- Actual index (not statistics)
BEGIN
IF ((@stat & 2048 <>0) OR (@stat & 4096<>0)) --Primary Key or unique key
BEGIN
SET @sql = 'ALTER TABLE ['+@owner+'].['+@table+'] DROP CONSTRAINT ['+@idx_name+']'
END
ELSE -- just another index
BEGIN
SET @sql = 'DROP INDEX ['+@owner+'].['+@table + '].['+@idx_name +']'
END
END
ELSE -- Statistics
BEGIN
SET @sql = 'DROP STATISTICS ['+@owner+'].['+@table + '].['+@idx_name +']'
END
exec (@sql)
FETCH NEXT FROM cur_idx INTO @idx_name, @stat

END

CLOSE cur_idx
DEALLOCATE cur_idx

SET @sql = 'ALTER TABLE ['+@owner+'].['+@table + '] DROP COLUMN [' + @col + ']'

EXEC (@sql)




Edited by - izaltsman on 12/05/2001 16:36:57
Go to Top of Page
   

- Advertisement -