sqlschool.gr logo

articles

Articles of SQLschool.gr Team

How to clone / move logins to another server

Antonios Chatzipavlis
Saturday 20 July 2019

Δεν είναι λίγες οι φορές που χρειάζεται να μεταφέρεις logins από ένα SQL Server σε ένα άλλον. Οι λόγοι είναι αρκετοί για να θέλεις να κάνεις κάτι τέτοιο όπως Database Replication, Always On Availability groups, Log Shipping etc.

Σε κάθε περίπτωση θέλεις να πάρεις από τον primary SQL Server αυτά τα logins που σου χρειάζονται, και στον άλλον, ώστε να μπορούν και από εκεί αυτά να χρησιμοποιούνται.

Αντί να καταφύγουμε σε κάτι περίπλοκο όπως μερικά που είμαι σίγουρος ότι θα έχει βρει στο Internet μπορείτε να εκτελέσετε το παρακάτω query, στον primary, και να πάρετε το αποτέλεσμα του, που δεν είναι άλλο από τα CREATE LOGIN statements για κάθε login έχει o primary, και αυτά απλά να τα εκτελέσετε στο άλλον ή στους άλλους. Φυσικά και μπορείτε να εκτελέσετε μόνο όσα χρειάζεστε. Aν κατά λάθος τα εκτελέσετε όλα μαζί όσα υπάρχουν δεν θα δημιουργηθούν ξανά απλά θα πάρετε πολλά μηνύματα λάθους που θα σας λένε αυτό. Η επιλογή είναι δική σας.

Μερικές τεχνικές λεπτομέρειες καθώς ποτέ δε μου αρέσει να δίνω απλά ένα query ή tip χωρίς να εξηγώ πως αυτό δουλεύει.

Το συγκεκριμένο κάνει χρήση των sys.server_principals, sys_sql_logins, sys.credentials. Δεν θα σας γράψω τι κάνει το κάθε ένα από αυτά καθώς είναι εύκολο να τα βρείτε στα BOL. Θα δείτε ότι υπάρχουν δύο βασικά conditional statements με τα οποία ξεχωρίζω τα windows και sql logins ώστε να διαμορφωθεί κατάλληλα το create login statement. Επειδή δεν με ενδιαφέρουν όλα τα logins στο where φιλτράρω αυτά που με ενδιαφέρουν βγάζοντας εκτός όλα τα systemic logins.

Τέλος θα δείτε ότι στα SQL Logins στο password χρησιμοποιώ το όποιον HASHED καθώς με αυτό μπορώ να έχω το ίδιο password. Δείτε για αυτό στα BOL το εξηγεί εξαιρετικά.

select 'create login ' + QUOTENAME( p.name ) 
        + iif ( p.type IN ('U','G'), ' from windows ', ' '  )  
        + 'with '  
        + iif ( p.type = 'S', 'password = ' + CONVERT(VARCHAR(8000),l.password_hash,1) + ' hashed, '  
        + 'sid = ' + CONVERT(VARCHAR(8000),l.sid,1) 
        + ', check_expiration = ' + iif ( l.is_expiration_checked > 0, 'ON', 'OFF' )  
        + ', check_policy = ' + iif ( l.is_policy_checked > 0 ,'ON, ', 'OFF, ' )
        +  iif (l.credential_id > 0, 'credential = ' + c.name +',',  '' ) , '' ) 
        + 'default_database = ' + p.default_database_name 
        + iif ( len(p.default_language_name) > 0 , ', default_language = ' + p.default_language_name,  '' )
        +';'
from sys.server_principals as p
left join sys.sql_logins as l on p.principal_id = l.principal_id
left join sys.credentials as c on l.credential_id = c.credential_id
where p.type in ('S','U','G')
              and p.name <> 'sa'
              and p.name not like 'NT Authority%'
              and p.name not like 'NT Service%'
              and p.name not like '##%'

//antonch

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.