Thanks!
Here is what I get: Incorrect syntax near the word for, incorrect syntax near mycursor
ALTER PROCEDURE granting_all
as
declare @sql_string nvarchar(4000)
declare @mycursor cursor for
select name from sysobjects
where xtype = 'U'
and name like 'tbl_my_table%'
DECLARE @name varchar(40)
OPEN mycursor
FETCH NEXT FROM mycursor INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
set @sql_string = 'sp_changeobjectowner @objname = ''' + @name + ''', @newowner = ''dbo'''
exec sp_executesql @sql_string
set @sql_string = 'Grant update,select,insert On ' + @name + ' To web_serv'
exec sp_executesql @sql_string
END
FETCH NEXT FROM mycursor INTO @name
END
CLOSE mycursor
DEALLOCATE mycursor
GO
Main Topics
Browse All Topics





by: HillwaaaPosted on 2006-11-20 at 14:26:31ID: 17983292
Hi jtreher,
One way would be through a cursor (put this inside your procedure):
declare @sql_string nvarchar(4000)
-- get all tables starting with tbl_my_table...
declare mycursor cursor for
select name from sysobjects
where xtype = 'U'
and name like 'tbl_my_table%'
DECLARE @name varchar(40)
OPEN mycursor
FETCH NEXT FROM mycursor INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
set @sql_string = 'sp_changeobjectowner @objname = ''' + @name + ''', @newowner = ''dbo'''
exec sp_executesql @sql_string
set @sql_string = 'Grant update,select,insert On ' + @name + ' To web_serv'
exec sp_executesql @sql_string
END
FETCH NEXT FROM mycursor INTO @name
END
CLOSE mycursor
DEALLOCATE mycursor
GO
Cheers!