/* declare the variable for the current table and schema */ declare @CurrTable varchar(40), @CurrSchema varchar(40) DECLARE tables_cursor CURSOR FOR SELECT TABLE_NAME, table_schema FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' OPEN tables_cursor -- Perform the first fetch. FETCH NEXT FROM tables_cursor -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN -- This is executed as long as the previous fetch succeeds. -- EXEC sp_addrolemember 'db_datareader','Jon' FETCH NEXT FROM tables_cursor into @CurrTable, @CurrSchema /* grant permission, but 'grant' does not like @CurrTable */ grant select on @CurrTable to ReadOnlyUser END CLOSE tables_cursor DEALLOCATE tables_cursorThe parser fails the line
grant select on @CurrTable to ReadOnlyUserbecause it is expecting an known object/table, not a variable. How do I overcome this?