1 Replies - 572 Views - Last Post: 03 December 2014 - 10:56 AM

#1 ricososwavay   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 113
  • Joined: 11-February 14

Cursor to Create Login from table

Posted 23 November 2014 - 07:18 AM

I am trying to create a cursor that loops through a table getting all the first and last names and then create a login and user for these names. The trouble I am having is how to set the first and last name with no space from the table to the Login and user. Here is my code so far the problem is in the begin statement.


DECLARE @FirstName varchar(50),@LastName varchar(50)
DECLARE AdminList CURSOR FOR
SELECT FirstName,LastName from Administrators
OPEN AdminList
FETCH NEXT FROM AdminList 
INTO @FirstName,@LastName	 
WHILE @@FETCH_STATUS = 0
BEGIN
CREATE LOGIN @[email protected] WITH PASSWORD = 'temp',
DEFAULT_DATABASE=MyGuitarShop
CREATE USER @[email protected] FOR LOGIN @[email protected]
ALTER ROLE OrderEntry ADD MEMBER @[email protected]
END
FETCH NEXT FROM AdminList 
INTO @FirstName,@LastName
CLOSE AdminList
DEALLOCATE AdminList


This post has been edited by ricososwavay: 23 November 2014 - 07:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cursor to Create Login from table

#2 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 409
  • View blog
  • Posts: 883
  • Joined: 27-June 09

Re: Cursor to Create Login from table

Posted 03 December 2014 - 10:56 AM

You need to use dynamic sql for this. Make a string that contains the whole command for creating user and login and then use the command EXEC @cmd.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1