Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cursor with Insert and Function

Author  Topic 

Big_tim
Starting Member

22 Posts

Posted - 2010-08-11 : 12:00:19
Hi Guys,
I have a bit of a problem with a script that I’m trying to write which will move the GUID and Username from one table (tblUser) in a database (DB1) to another (UserCredential) on a different DB (DB2) (Both DB’s are on the same server). Now, that bit is simple, but as well as those 2 fields, ‘tblUser’ also has a ‘password’ column. The trickier bit is that I have a function which is going to change the regular password into a secure Salt and hashed password and gives me an output of 2 columns.
Now, to go through the database and run the function for each user is going to need to use a cursor (I believe) so I have so far come up with the following script. However, I’ve not really used cursors before and am hitting a brick wall.
How do I get the cursor to read from one table then insert the results into another?
Any help would be hugely appreciated.

DECLARE
@UserId uniqueidentifier,
@UserName nvarchar(50),
@Salt nvarchar(50),
@PasswordSalt nvarchar(80),
@IsLocked int,
@IsDeadLocked int,
@LoginFailedAttempts int,
@SecurityQuestionFailedAttempts int,
@CreationDate datetime,
@LastActivityDate datetime

--Declare the Cursor
DECLARE c1 CURSOR FOR

SELECT
U.UserId
,U.UserName
,U.Password
,(SELECT Salt
FROM msdb.dbo.GetSaltAndPassword(@Password)) Salt
,(SELECT HashedPassword
FROM msdb.dbo.GetSaltAndPassword(@Password)) HashedPassword

FROM
DB1.ids.tblUser U
WHERE
UserId = @UserId

-- Start the cursor
OPEN c1
FETCH NEXT FROM c1 INTO @UserId, @UserName, @Salt, @PasswordSalt, @IsLocked, @IsDeadLocked, @LoginFailedAttempts, @SecurityQuestionFailedAttempts, @CreationDate, @LastActivityDate

WHILE @@fetch_status = 0
BEGIN

--Reset the variable to nothing each time round the loop

SET @UserId = null
BEGIN

--Insert details into StandardUserCredential table

INSERT INTO DB2.ids.UserCredential
(
UserId
,UserName
,PasswordSalt
,[Password]
,IsLocked
,IsDeadLocked
,LoginFailedAttempts
,SecurityQuestionFailedAttempts
,CreationDate
,LastActivityDate
)

VALUES
(@UserId
,@UserName
,@Salt
,@PasswordSalt
,0
,0
,0
,0
,GETDATE()
,GETDATE()
)
END

FETCH NEXT FROM c1 INTO @UserId, @UserName, @Salt, @PasswordSalt, @IsLocked, @IsDeadLocked, @LoginFailedAttempts, @SecurityQuestionFailedAttempts, @CreationDate, @LastActivityDate

END
CLOSE c1
DEALLOCATE c1

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-11 : 12:09:48
are you using sql 2005?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Big_tim
Starting Member

22 Posts

Posted - 2010-08-12 : 04:13:27
Yes, SQL 2005...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-12 : 11:12:58
then use the method 4 in below link

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -