Link to home
Start Free TrialLog in
Avatar of 7of9121098
7of9121098

asked on

MS SQL Stotred procedure....

I get an error on cursor, can't see the problem. Help!

ALTER PROCEDURE [dbo].[WOW_SETLOGINS]
 (
    --Designed to set userlogins in for a given database/tables.

    --Retrieves a string of table names based on a given database.
    --Input parameters...  
    @DBNAME VARCHAR(45),
    @LOGINNAME VARCHAR(45)  
 )

AS

DECLARE @sSelectSql NVARCHAR(1000)
DECLARE @sGrantSql NVARCHAR(1000)
 
--The cursor that will be filled by the dynamic SQL
DECLARE @outputCursor CURSOR

--Grab all of the tables from the given database.
SET @sSelectSql= 'SELECT Name ' + @DBNAME + '..sysobjects WHERE type = ''U'''

--Create the dynamic SQL to fill a CURSOR instead, Note the OPEN keyword.
SET @sSelectSql = 'SET @outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @sSelectSql + ' ; OPEN @outputCursor'

--Execute dynamic SQL
--The parameter list for the sproc: OUTPUT CURSOR
--The parameter to pass to the sproc: the CURSOR
EXEC sp_executesql @sSelectSql, N' @outputCursor CURSOR OUTPUT', @outputCursor OUTPUT        

--Code that will just output the values from the cursor
DECLARE @TABLENAME VARCHAR(45) --The name of the table.

--Grap All of the fields and insert them into the variables
FETCH NEXT FROM @outputCursor INTO @TABLENAME

WHILE (@@FETCH_STATUS=0)
 BEGIN
   SET @sGrantSql= 'GRANT SELECT, INSERT, DELETE, UPDATE ON ' + @DBNAME + '..'
   SET @sGrantSql= @sGrantSql + @TABLENAME + ' TO [' + @LOGINNAME + ']'  
 
   --Grant the table the given permissions...
   EXEC sp_executesql @sGrantSql, N' @DBNAME VARCHAR(45), @TextFieldName varchar(25), @TABLENAME VARCHAR(45), @LOGINNAME VARCHAR(45)',
   @DBNAME,@TABLENAME,@LOGINNAME
   
   FETCH NEXT FROM @outputCursor INTO @TABLENAME
 END
 
 CLOSE @outputCursor
 DEALLOCATE @outputCursor
Avatar of GSQL
GSQL
Flag of United States of America image

SET @sSelectSql= 'SELECT Name from ' + @DBNAME + '.sys.objects WHERE type = ''U'''

ALTER PROCEDURE [dbo].[WOW_SETLOGINS]
 (
    --Designed to set userlogins in for a given database/tables.
 
    --Retrieves a string of table names based on a given database. 
    --Input parameters...   
    @DBNAME VARCHAR(45), 
    @LOGINNAME VARCHAR(45)   
 )
 
AS 
 
DECLARE @sSelectSql NVARCHAR(1000)
DECLARE @sGrantSql NVARCHAR(1000)
 
--The cursor that will be filled by the dynamic SQL
DECLARE @outputCursor CURSOR DYNAMIC
 
--Grab all of the tables from the given database.
SET @sSelectSql= 'SELECT Name from' + @DBNAME + '..sysobjects WHERE type = ''U'''
 
--Create the dynamic SQL to fill a CURSOR instead, Note the OPEN keyword.
SET @sSelectSql = 'SET @outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @sSelectSql + ' ; OPEN @outputCursor'
 
--Execute dynamic SQL
--The parameter list for the sproc: OUTPUT CURSOR
--The parameter to pass to the sproc: the CURSOR
EXEC sp_executesql @sSelectSql, N' @outputCursor CURSOR OUTPUT', @outputCursor OUTPUT        
 
--Code that will just output the values from the cursor
DECLARE @TABLENAME VARCHAR(45) --The name of the table.
 
--Grap All of the fields and insert them into the variables
FETCH NEXT FROM @outputCursor INTO @TABLENAME
 
WHILE (@@FETCH_STATUS=0)
 BEGIN
   SET @sGrantSql= 'GRANT SELECT, INSERT, DELETE, UPDATE ON ' + @DBNAME + '..' 
   SET @sGrantSql= @sGrantSql + @TABLENAME + ' TO [' + @LOGINNAME + ']'  
  
   --Grant the table the given permissions...
   EXEC sp_executesql @sGrantSql, N' @DBNAME VARCHAR(45), @TextFieldName varchar(25), @TABLENAME VARCHAR(45), @LOGINNAME VARCHAR(45)', 
   @DBNAME,@TABLENAME,@LOGINNAME
   
   FETCH NEXT FROM @outputCursor INTO @TABLENAME
 END
 
 CLOSE @outputCursor
 DEALLOCATE @outputCursor

Open in new window

Avatar of 7of9121098
7of9121098

ASKER

I get an following error when I run it....it doesn't like the keyword dymanic.

Incorrect syntax near 'dynamic'.
Msg 137, Level 15, State 2, Procedure SETLOGINS, Line 29
Must declare the scalar variable "@outputCursor".
Msg 13
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

 I solved the issue I had to remove the dynamic and put a space in the 'SELECT Name from'  to 'Select Name From '. Thanks
Thanks...RiteshShah!