Windows 10: How to: Sync SQL users from one server to another

Discus and support How to: Sync SQL users from one server to another in Windows 10 Tutorials to solve the problem; When you have SQL Clustering with Always On, the databases are sycnhronised and the failover works like a charm. The only problem is that the logins... Discussion in 'Windows 10 Tutorials' started by Noel, Jun 21, 2018.

  1. Noel New Member

    How to: Sync SQL users from one server to another


    When you have SQL Clustering with Always On, the databases are sycnhronised and the failover works like a charm. The only problem is that the logins are not synchronized from one server to another. To export the current users and import to another server, on the main server execute the below T-SQL.

    USE master
    GO
    IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL
    DROP PROCEDURE sp_hexadecimal
    GO
    CREATE PROCEDURE sp_hexadecimal
    @binvalue varbinary(256),
    @hexvalue varchar (514) OUTPUT
    AS
    DECLARE @charvalue varchar (514)
    DECLARE @i int
    DECLARE @length int
    DECLARE @hexstring char(16)
    SELECT @charvalue = '0x'
    SELECT @i = 1
    SELECT @length = DATALENGTH (@binvalue)
    SELECT @hexstring = '0123456789ABCDEF'
    WHILE (@i 'sa'
    ELSE
    DECLARE login_curs CURSOR FOR

    SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
    sys.server_principals p LEFT JOIN sys.syslogins l
    ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name
    OPEN login_curs

    FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
    IF (@@fetch_status = -1)
    BEGIN
    PRINT 'No login(s) found.'
    CLOSE login_curs
    DEALLOCATE login_curs
    RETURN -1
    END
    SET @tmpstr = '/* sp_help_revlogin script '
    PRINT @tmpstr
    SET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */'
    PRINT @tmpstr
    PRINT ''
    WHILE (@@fetch_status <> -1)
    BEGIN
    IF (@@fetch_status <> -2)
    BEGIN
    PRINT ''
    SET @tmpstr = '-- Login: ' + @name
    PRINT @tmpstr
    IF (@type IN ( 'G', 'U'))
    BEGIN -- NT authenticated account/group

    SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']'
    END
    ELSE BEGIN -- SQL Server authentication
    -- obtain password and sid
    SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) )
    EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT
    EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT

    -- obtain password policy state
    SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
    SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name

    SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']'

    IF ( @is_policy_checked IS NOT NULL )
    BEGIN
    SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked
    END
    IF ( @is_expiration_checked IS NOT NULL )
    BEGIN
    SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked
    END
    END
    IF (@denylogin = 1)
    BEGIN -- login is denied access
    SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name )
    END
    ELSE IF (@hasaccess = 0)
    BEGIN -- login exists but does not have access
    SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name )
    END
    IF (@is_disabled = 1)
    BEGIN -- login is disabled
    SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE'
    END
    PRINT @tmpstr
    END

    FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
    END
    CLOSE login_curs
    DEALLOCATE login_curs
    RETURN 0
    GO

    Once this is done, run the below T-SQL on the main SQL server

    EXEC sp_help_revlogin

    This will output the script to be used on the replica server. Copy the output and paste it on the connection to the replica server. After executing you will find the users with the same password from the main server.

    (18)

    read more...
     
  2. Brink Win User

    SQL Server + Python — What’s new


    Source: SQL Server + Python s new | SQL Server Blog
     
    Brink, Jun 21, 2018
    #2
  3. Dyan Rey Win User
    SQL server 2014

    Hello,

    Incorrect user permissions is one of the possible reasons why you're having challenges in downloading SQL server 2014. We suggest that you check this
    link. This page contains information on how to obtain the latest service pack for SQL
    Server 2014.

    Let us know how else we may help you.  
     
    Dyan Rey, Jun 21, 2018
    #3
  4. Brink Win User

    How to: Sync SQL users from one server to another

    SQL Server + PHP – What’s new


    Source: SQL Server + PHP s new | SQL Server Blog
     
    Brink, Jun 21, 2018
    #4
Thema:

How to: Sync SQL users from one server to another

Loading...
  1. How to: Sync SQL users from one server to another - Similar Threads - Sync SQL users

  2. How to copy a file from one server to another ?

    in Windows 10 Network and Sharing
    How to copy a file from one server to another ?: Hello, I have a basic question of how to copy a file in one server to another server that I need your help?For example , I have this file in my server : test-myserver-sql1C:\Program Files\Microsoft SQL Server\130\DTS\Binn\dtutil.exeAnd I want to copy it to my second server :...
  3. How to copy a file from one server to another ?

    in Windows 10 Gaming
    How to copy a file from one server to another ?: Hello, I have a basic question of how to copy a file in one server to another server that I need your help?For example , I have this file in my server : test-myserver-sql1C:\Program Files\Microsoft SQL Server\130\DTS\Binn\dtutil.exeAnd I want to copy it to my second server :...
  4. How to copy a file from one server to another ?

    in Windows 10 Software and Apps
    How to copy a file from one server to another ?: Hello, I have a basic question of how to copy a file in one server to another server that I need your help?For example , I have this file in my server : test-myserver-sql1C:\Program Files\Microsoft SQL Server\130\DTS\Binn\dtutil.exeAnd I want to copy it to my second server :...
  5. SQL server

    in Windows 10 Software and Apps
    SQL server: After upgrading the OS from Win 10 to Win 11 home edition the SQL doesn't work any more. https://answers.microsoft.com/en-us/windows/forum/all/sql-server/1c360899-de1f-4c76-ae53-cd415ef46ac1
  6. SQL server

    in Windows 10 Gaming
    SQL server: After upgrading the OS from Win 10 to Win 11 home edition the SQL doesn't work any more. https://answers.microsoft.com/en-us/windows/forum/all/sql-server/1c360899-de1f-4c76-ae53-cd415ef46ac1
  7. Transferring files from one user to another

    in Windows 10 Customization
    Transferring files from one user to another: I original set up windows under an account/email that has since be closed. I want to transfer all my files currently on the old user account to my new user account but can't seem to figure this out. Is there a way I can move my files, apps, and setting that are on the old...
  8. How to move data from one user account to another?

    in Windows 10 Ask Insider
    How to move data from one user account to another?: How do I move all the programs and files from one user to another user on the same system? submitted by /u/samerium [link] [comments] https://www.reddit.com/r/Windows10/comments/ijpcft/how_to_move_data_from_one_user_account_to_another/
  9. How to move an app from a user to another one

    in Windows 10 Ask Insider
    How to move an app from a user to another one: Hi, I created a new user for me on the same laptop my mom uses. I play Township (a game app) on her user and I want to transfer it to my personal user. Any idea how to do it? submitted by /u/Retrograde-Motion [link] [comments]...
  10. Sync calendar from one PC to another

    in Windows 10 Software and Apps
    Sync calendar from one PC to another: Hi there !! I really really need help!! My HP model 14BS153OD is dying a slow and painful death and will be having it's death rattle soon. Before it is dead as a door nail and in the trash heap, the only thing I am concerned about at all is my calendar....

Users found this page by searching for:

  1. t-sql sync users to another server