Dynamic SQL for build grant

Hello,

I try to build grant statement with this stored proc, I have this error :


CREATE procedure [dbo].[create_grants2]
@Dbname varchar (30),  
@Schsource varchar (30)
 
AS  
set nocount on

declare @sql varchar(max)
declare @sql2 varchar(max)
CREATE TABLE stmt (sql varchar(max) )
set @sql = 'SELECT ''GRANT '+
      CASE c.action
            WHEN 26 THEN ''REFERENCES''
            WHEN 193 THEN ''SELECT''
            WHEN 195 THEN ''INSERT''
            WHEN 196 THEN ''DELETE''
            WHEN 197 THEN ''UPDATE''
            WHEN 198 THEN ''CREATE TABLE''
            WHEN 203 THEN ''CREATE DATABASE''
            WHEN 207 THEN ''CREATE VIEW''
            WHEN 222 THEN ''CREATE PROCEDURE''
            WHEN 224 THEN ''EXECUTE''
            WHEN 233 THEN ''CREATE DEFAULT''
            ELSE ''CREATE RULE''
      END+ ' ON '+ o.name+ ' TO ' + b.name  + CHAR(13) + ';' + CHAR(13)
FROM sys.objects o
INNER JOIN sysprotects c
      ON o.object_id = c.id
INNER JOIN sysusers b
      ON c.uid = b.uid
left join sys.schemas s
    on s.schema_id = o.schema_id
   
WHERE o.type IN ('U','P','V')
and s.name= @Schsource
and b.name not like ''X3%''
order by o.name desc

SELECT 'GRANT '+
      CASE c.action
            WHEN 26 THEN ''REFERENCES''
            WHEN 193 THEN ''SELECT''
            WHEN 195 THEN ''INSERT''
            WHEN 196 THEN ''DELETE''
            WHEN 197 THEN ''UPDATE''
            WHEN 198 THEN ''CREATE TABLE''
            WHEN 203 THEN ''CREATE DATABASE''
            WHEN 207 THEN ''CREATE VIEW''
            WHEN 222 THEN ''CREATE PROCEDURE''
            WHEN 224 THEN ''EXECUTE''
            WHEN 233 THEN ''CREATE DEFAULT''
            ELSE ''CREATE RULE''
      END+ ' ON '+ o.name+ ' TO ' + b.name  + CHAR(13) + ';' + CHAR(13)
FROM sys.objects o
INNER JOIN sysprotects c
      ON o.object_id = c.id
INNER JOIN sysusers b
      ON c.uid = b.uid
left join sys.schemas s
    on s.schema_id = o.schema_id
   
WHERE o.type IN ('U','P','V')
and s.name= @Schsource
and b.name like ''X3%'''

Msg 156, Level 15, State 1, Procedure create_grants2, Line 13
Incorrect syntax near the keyword 'REFERENCES'.
Msg 156, Level 15, State 1, Procedure create_grants2, Line 41
Incorrect syntax near the keyword 'REFERENCES'.
Msg 105, Level 15, State 1, Procedure create_grants2, Line 64
Unclosed quotation mark after the character string ''

Thanks

bibi

bibi92Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:

INSERT INTO stmt
SELECT 'GRANT '+
CASE c.action
WHEN 26 THEN 'REFERENCES'
WHEN 193 THEN 'SELECT'
WHEN 195 THEN 'INSERT'
WHEN 196 THEN 'DELETE'
WHEN 197 THEN 'UPDATE'
WHEN 198 THEN 'CREATE TABLE'
WHEN 203 THEN 'CREATE DATABASE'
WHEN 207 THEN 'CREATE VIEW'
WHEN 222 THEN 'CREATE PROCEDURE'
WHEN 224 THEN 'EXECUTE'
WHEN 233 THEN 'CREATE DEFAULT'
ELSE 'CREATE RULE'
END+ ' ON '+ o.name+ ' TO ' + b.name + CHAR(13) + ';' + CHAR(13)
FROM sys.objects o
INNER JOIN sysprotects c
ON o.object_id = c.id
INNER JOIN sysusers b
ON c.uid = b.uid
left join sys.schemas s
on s.schema_id = o.schema_id

WHERE o.type IN ('U','P','V')
and s.name= @Schsource
and b.name like 'X3%'
order by o.name desc
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bibi92Author Commented:
Thanks a lot, is it possible to update only  a part of sql statement like :

GRANT SELECT ON BCOR.CDIADSP TO F_ADX ;
GRANT SELECT ON BCOR.CDIADSP TO G_ADX ;
Thanks

bibi
0
bibi92Author Commented:
Thanks a lot regards bibi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.