I need to check if a specific login already exists on the SQL Server, and if it doesn’t, then I need to add it. I have found the following code to actually add the login to the database, but I want to wrap this in an IF statement (somehow) to check if the login exists first.
2/15/2009 · Is there any way to check whether the login exists before creating login ? create login should be executed after check . thanks, Sreenath, Use sys.database_principals instead of sys.server_principals.. So the final query would look like this (accounting for the user filter): USE [MyDatabase] GO IF NOT EXISTS (SELECT [name] FROM [sys].[database_principals] WHERE [type] = N’S’ AND [name] = N’IIS APPPOOLMyWebApi AppPool’) Begin CREATE USER [IIS APPPOOLMyWebApi AppPool] FOR LOGIN [IIS APPPOOLMyWebApi.
Checking if a SQL Server login already exists – Stack Overflow, tsql – How to check if the USER is already created in the database or …
Overview of the T-SQL If Exists statement in a SQL Server database, IS_ROLEMEMBER (Transact-SQL) – SQL Server | Microsoft Docs, 8/22/2016 · When writing T-SQL code, we often write code to check if the database object exists first and then take some action. Is there an easier way to do this in SQL Server 2016? Solution. Microsoft SQL Server 2016 was released to manufacturing on June 1st. Please see this SQL Server Team BLOG for the full details.
Essentially combining David’s answer and marc_s’s answer, as requested by a comment from Chris.. Books Online says of sp_grantdbaccess:. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
5/22/2013 · Login . Sometimes it could be useful to check if a SQL Server login already exists . Before to attempt the creation of a login maybe you want to check if it exists . This could be achieved using one of the following codes:, The SQL EXISTS Operator . The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns true if the subquery returns one or more records.