p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > SQL Server > SQL Server 2000 > SQL Server 2000
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 13th, 2003, 10:24 AM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to oidhche
Default Grant SELECT on multiple tables

Hi,

I have created a server role in a database which needs SELECT permissions on some, but not all, tables in the database. There are almost a hundred tables that need permissions set on, so I have been trying to put together a SQL script that will do this for me. In the standard GRANT Select on... script you can name only one table to grant permissions on. Someone recommended using dynamic SQL but I have only just got to grips with the basics of SQL. This is my current attempt at using the sp_executesql stored procedure:

************************************************** ************
DECLARE @SQLString VarChar(1000)
DECLARE @TableName VarChar(100)

SET @SQLString = 'Grant Select on @TableName To "ServerRole"'

Set @Tablename='Table1'
EXEC sp_executesql @SQLString

Set @Tablename='Table2'
EXEC sp_executesql @SQLString
************************************************** ************

This generates the following error message:

"Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'."

There is probably something very obvious about my code that jumps out at an experienced (or not-so experienced) SQL person. Can anyone suggest how to improve this code, or an altogether different way of achieving the same end?

All help greatly appreciated.

Thanks,
Sharon
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 14th, 2003, 02:53 AM
Registered User
Points: 2, Level: 1
Points: 2, Level: 1 Points: 2, Level: 1 Points: 2, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , Indiana, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's how I would do it. The following code produces the output on the northwind DB. I add the like statements so it only does the tables that start with C or S. I would then run the output in another Query Window.

I hope this helps.

Tim S

SELECT 'GRANT SELECT ON ' + TABLE_NAME + ' TO ServerRole'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
   ( TABLE_NAME LIKE 'C%' OR TABLE_NAME LIKE 'S%')


-----------------------------------------------------
GRANT SELECT ON Categories TO ServerRole
GRANT SELECT ON CustomerCustomerDemo TO ServerRole
GRANT SELECT ON CustomerDemographics TO ServerRole
GRANT SELECT ON Customers TO ServerRole
GRANT SELECT ON Shippers TO ServerRole
GRANT SELECT ON Suppliers TO ServerRole
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old June 16th, 2003, 04:52 AM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to oidhche
Default

Tim

This is great - works a treat. Thanks very much!

Sharon
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old June 16th, 2003, 07:26 PM
Friend of Wrox
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Sydney, NSW, Australia.
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just a quick comment, your original code has a few problems.



@SQLString will always hold the string 'Grant Select on @TableName To "ServerRole"'. You need to explicitly concantate the variable values:
SET @SQLString = 'Grant Select on ' + @TableName + ' To "ServerRole", this is not perl or php. Also when you change the value held in @TableName, this will not update @SQLString. Finally if you declare @SQLString as a nvarchar the original message will dissappear.

What you acutally want is:
DECLARE @SQLString nVarChar(1000)
DECLARE @TableName nVarChar(100)

Set @Tablename='Table1'
SET @SQLString = 'Grant Select on ' + @TableName + ' To "ServerRole"'
EXEC sp_executesql @SQLString

Set @Tablename='Table2'
SET @SQLString = 'Grant Select on ' + @TableName + ' To "ServerRole"'
EXEC sp_executesql @SQLString

stahta01's solution is better, but I thought that this may help in writing future code.

regards
David Cameron
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Count from multiple tables aspless SQL Language 1 January 30th, 2008 10:32 AM
Select from Two Tables Problem gaurav_jain2403 SQL Server 2000 2 May 18th, 2006 08:18 AM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 09:47 AM
Multiple Recordsets from Multiple Tables TSEROOGY Classic ASP Databases 2 December 28th, 2004 12:45 PM
Select lot tables... vieritlc Classic ASP Databases 3 May 18th, 2004 08:07 AM



All times are GMT -4. The time now is 03:50 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc