PLEASE HELP: I AM TRYING TO USE THE FOLLOWING SCRIPT TO :
1. FOR EACH TABLE IN A GIVEN DATABASE (I am using a cursor for this)
2. FIND IF A COLUMN OF NAME "row_id" exists
3. IF it does, delete it
4. if it doesnt, proceed to next table
Right now I am getting error messages when it comes to a table where the column does not exist, it simply stops because the drop statement fails - what am I doing wrong ???????????????????????????
code:
--ALTER TABLES SCRIPT
--change the line marked *** to suit your update needs....
IF EXISTS (SELECT*
FROMsysobjects o WITH(NOLOCK),
syscolumns b WITH(NOLOCK)
WHEREo.[id] = b.[id] AND b.name = 'row_id'
ANDO.name = ''+o.name)
BEGIN
DECLARE tableNamesCursor CURSOR
FOR
--***
--select 'ALTER TABLE ['+ o.name+ '] ADD row_id as '+o.name+'+'ID'
--CONSTRAINT AddDateDflt'+ cast(o.id as varchar(10))+ ' DEFAULT getdate() WITH VALUES'
-- removes the TimeStamp column, and constraint from all tables
select 'ALTER TABLE ['+ o.name+ '] DROP COLUMN row_id'
from sysobjects o, sysindexes i
where i.id = o.id
and indid in(0,1)
and o.name not like 'sys%'
and o.name not like 'dt%'
order by o.name
OPEN tableNamesCursor
DECLARE @alterTableSql nvarchar(400)
FETCH NEXT FROM tableNamesCursor INTO @alterTableSql
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
print @alterTableSql
exec sp_executesql @alterTableSql
END
FETCH NEXT FROM tableNamesCursor INTO @alterTableSql
END
CLOSE tableNamesCursor
DEALLOCATE tableNamesCursor
END