Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Granting select permissions on all Views, SQL error (declare scalar variable) Expand / Collapse
Author
Message
Posted Wednesday, January 26, 2011 1:30 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 9:01 AM
Points: 100, Visits: 459
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
Post #1054192
Posted Wednesday, January 26, 2011 1:39 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 9:52 AM
Points: 863, Visits: 7,328
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
Post #1054197
Posted Wednesday, January 26, 2011 2:20 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 9:01 AM
Points: 100, Visits: 459
Thank you David, yes I had to declare @rowid and @sqlstr.
Post #1054228
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse