January 26, 2011 at 1:30 pm
Hey guys,
I am attempting to give select permissions to all Views in a database for a particular user, and I do not want to make them db_datareader for security issues.
I am trying to use the script from http://www.tech-recipes.com/rx/2298/sql_server_2005_easily_grant_select_all_tables_views/
but I get "Must declare the scalar variable" for @rowid & @sqlstr. I read up and this has to do with dynamic SQL?
I'm a coding newb so if anyone can decifer this I would very much appreciate it!
DECLARE @login varchar(50)
SET @login = 'loginname'
DECLARE @views TABLE(ROWID int IDENTITY(1,1), SQLSTR varchar(500))
INSERT INTO @views
SELECT 'GRANT SELECT ON ' + NAME + ' TO '+@login
FROM sysobjects
WHERE TYPE = 'V'
SET @rowid = 0
SET @sqlstr = ''
DECLARE grant_vw_cursor CURSOR FOR
SELECT ROWID, SQLSTR
FROM @views
ORDER BY ROWID
OPEN grant_vw_cursor
FETCH NEXT FROM grant_vw_cursor
INTO @rowid,@sqlstr
WHILE @@FETCH_STATUS = 0
BEGIN
EXECUTE (@sqlstr)
FETCH NEXT FROM grant_vw_cursor
INTO @rowid,@sqlstr
END
CLOSE grant_vw_cursor
DEALLOCATE grant_vw_cursor
January 26, 2011 at 1:39 pm
While rowid and sqlstr are column names in your table, you are also using variables @rowid and @sqlstr in your code without ever declaring them. Add the declaration for them with the same data types as the columns at the beginning and you shoud be covered.
January 26, 2011 at 2:21 pm
Thank you David, yes I had to declare @rowid and @sqlstr.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply