DECLARE db_cursor CURSOR FOR
SELECT name
FROM table
WHERE --some condition--
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
--sql query using @name goes here--
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
Jul 2, 2010
SQL Server cursor example
SQL server query to drop default constraint and a column
declare @default sysname, @sql nvarchar(max)
select @default = name
from sys.default_constraints
where parent_object_id = object_id('MYTABLENAME')
AND type = 'D'
AND parent_column_id = (
select column_id
from sys.columns
where object_id = object_id('MYTABLENAME')
and name = 'MYCOLUMNNAME'
)
set @sql = N'alter table MYTABLENAME drop constraint ' + @default
exec sp_executesql @sql
alter table MYTABLENAME drop MYCOLUMNNAME
go
Subscribe to:
Posts (Atom)