help with backup and restore sql logins

  • Hi,

    I'm trying to write this script that backup and restore a test sql login.

    first script backup my test login

    select sid,dbname,password,language,loginname

    into mylogintest

    from sys.syslogins where loginname = 'test'

    then i delete this test login and try to restore it with this syntax

    declare @password sysname

    declare @sid varbinary (85)

    declare @restorelogin nvarchar (1000)

    set @password = (select password from mylogintest)

    set @sid = (select sid from mylogintest)

    set @restorelogin = 'CREATE LOGIN [test] WITH PASSWORD=''' + @password + ''' HASHED,SID =' + @sid + ', DEFAULT_DATABASE=[system], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'

    exec (@restorelogin)

    and i get this error message

    The data types nvarchar and varbinary are incompatible in the add operator.

    why is that?

    THX

  • you are trying to insert a varbinary value into a dynamic sql string.

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • so what are my options to resolve this error?

    THX

  • Here is some code from Microsoft to transfer logins and passwords from one server to the other.

    http://support.microsoft.com/kb/918992

    Kind regards,
    Gift Peddie

  • try casting your @sid to a varchar when you make set your query variable.

  • I agree with the previous post that pointed you to the following link:

    http://support.microsoft.com/kb/918992

    You can use sp_help_revlogin to script your logins to a file, and have this output backed up with the rest of your DR necessary files (backups, encryption keys, build documenation, ...).

  • hi,

    i cannot casting it.

    i found the answer to it.

    i use the master.dbo.fn_varbintohexstr and it's works good.

    THX

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply