SQL Server Login with view to only one database

SSinhg 316 Reputation points
2024-11-08T10:01:41.47+00:00

Hi

I have a request to create a SQL account on my Instance of SQL 2014

  1. I want the user to connect to instance - Done
  2. And have read write access to only 1 schema - Done
  3. view only 1 database - Not working out

How i can i restrict user to see only 1 database and not all.

Below code is generated from CHATGPT and it works for step 1 & 2

SQL

USE master;
GO
-- Step 1: Create a login for the user if not already created
CREATE LOGIN [NewUserLogin] WITH PASSWORD = '';
GO
-- Step 2: Create a user in the ISFAL database for this login
USE mydatabse;
GO
CREATE USER [NewUser] FOR LOGIN [NewUserLogin];
GO
-- Step 3: Grant CONNECT permission to make visible and accessible
GRANT CONNECT TO [NewUser];
GO
-- Step 4: Grant SELECT permission on a specific schema (e.g., 'SchemaName') in mydatabase
GRANT SELECT ON SCHEMA::SchemaName TO [NewUser];
GO
-- Step 5: Deny VIEW ANY DATABASE permission at the server level for the login
USE master;
GO
DENY VIEW ANY DATABASE TO [NewUserLogin];
GO
Developer technologies | Transact-SQL
SQL Server | Other
{count} vote

Accepted answer
  1. LiHongMSFT-4306 31,566 Reputation points
    2024-11-11T08:21:49.1233333+00:00

    Hi @SSinhg

    See this similar thread: Hide SQL database from Management Studio.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2024-11-08T10:47:41.2866667+00:00
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.