SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Granting select permissions on all Views, SQL error (declare scalar variable)


Granting select permissions on all Views, SQL error (declare scalar variable)

Author
Message
chrisph
chrisph
SSC Eights!
SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)

Group: General Forum Members
Points: 901 Visits: 623
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

David Webb-CDS
David Webb-CDS
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3974 Visits: 8586
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.



And then again, I might be wrong ...
David Webb
chrisph
chrisph
SSC Eights!
SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)SSC Eights! (901 reputation)

Group: General Forum Members
Points: 901 Visits: 623
Thank you David, yes I had to declare @rowid and @sqlstr.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search