How to Script SQL Server Logins and Permissions

Published
Length
3 min read

Luis Felipe Dias

All posts

To understand how to script sql server logins and permissions, you export server identities and role rules. Then you run those exported statements on your target database instance. Teams do this work during server moves, disaster recovery drills, and security audits. Moving a database without its accounts breaks apps and blocks engineers. Understanding how to script sql server logins and permissions SQL Server splits access into two layers: the server level and the database level. A login li

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To understand how to script sql server logins and permissions, you export server identities and role rules. Then you run those exported statements on your target database instance.

Teams do this work during server moves, disaster recovery drills, and security audits. Moving a database without its accounts breaks apps and blocks engineers.

Understanding how to script sql server logins and permissions

SQL Server splits access into two layers: the server level and the database level. A login lives in the master database and lets a person or app log in. A database user is the identity of that login inside a single database.

When someone needs many databases on one instance, a login in master is common. For one or a few databases, you can use a contained database user instead. A contained user has a password and does not tie to a login in master. An admin must first turn on contained databases on the server and the database. SQL Server supports 13 types of database users across these models.

To script these accounts, admins query system views to build text statements. First, you export the server logins so the new instance knows the accounts. Next, you export the database users for each database on the server. Last, you script the role memberships and object grants for each database user.

Fixed database roles make permission scripting much simpler for your team. Members of the db_datareader role can read all data from all user tables and views. Members of db_datawriter can add, delete, or change data in all user tables. You can also block access with db_denydatareader or db_denydatawriter roles. The db_owner role can run all maintenance tasks and can even drop the database. You cannot change the permissions assigned to these fixed database roles.

Security tradeoffs of static SQL Server logins and roles

Scripting logins and roles helps you copy access rules across servers. Still, relying only on static logins and database roles leaves big security gaps. Static passwords sit in app files or local machines for months. If an attacker steals a login, they get every permission tied to that user.

Kubernetes API Server Access + AI Agent Permissions: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Fixed roles are also very broad and lack fine control. A user or AI agent in db_datareader can read every user table and view. That includes tables holding emails, phone numbers, card numbers, and national IDs. Database roles check who logs in, but they do not filter what rows return. A read role cannot stop a bad query from pulling millions of rows.

Moving logins between servers can also create mismatched user links. If a login and a database user do not match, access fails or maps wrong. Admins end up spending hours fixing broken scripts instead of shipping code. Teams need a way to check every query and mask sensitive fields in results.

How to keep SQL Server permissions clean across environments

Start by auditing who has the db_owner role in your production databases. Move humans and AI tools to narrower roles whenever you can. Use deny roles like db_denydatareader when a role is too open for a user.

Separate human access from app accounts so you can track every query back to a person. Do not share a single master login across your whole engineering team. Combine database roles with a layer that approves queries and masks sensitive fields in results. This setup keeps your data safe even when a role grants broad read access.

Govern SQL Server queries and responses in flight

Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev sits inline in the TDS wire protocol next to SQL Server as a sidecar for your SQL Server. On every production batch from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for approval.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused or held for review, matched sensitive fields in supported result columns are masked in memory, and each decision is logged. Check out the code on GitHub.

Frequently asked questions

What is the difference between a SQL Server login and a database user?

A login lives in the master database and lets you log in to the server. A database user is the identity of that login inside one specific database.

When should I use a contained database user in SQL Server?

Use a contained user when an account needs access to only one or a few databases. An admin must first turn on contained databases on the server and database.

Can I change the permissions inside the db_datareader role?

No, you cannot change the permissions assigned to fixed database roles in SQL Server. You must create a custom role or add a deny role like db_denydatareader.

The query runs. The emails stay hidden.