Showing posts with label SQLSERVER. Show all posts
Showing posts with label SQLSERVER. Show all posts

Monday, 19 July 2021

Reset IsIdentity of table to particular Value in SQL SERVER

 Syntax for setting to particular value is

 

DBCC CHECKIDENT ('table_name', RESEED, 100)  

-- Supply table name, here 100 is identity seed value

To Know the current seed value of the table use the below query


 DBCC CHECKIDENT ('table_name') -- Displays the current seed value

Thursday, 15 April 2021

SQL Server Error User Group or Role Already Exists in the Current Database Error Number 15023

 Whenever user mapping done is doing in SQL Server there comes an error such as 




Microsoft SQL Server, Error: 15023

To resolve this connect to the sql server using system administrator account and give the below commands

use [database_name]  -- give the database name exec sp_change_users_login 'AUTO_FIX', '[login_name]' -- give the login name 
go

Now you can check the database access using the login given above.

If everything is OK you will see the following output:

The row for user  will be fixed by updating its login link to a login already in existence.The number of orphaned users fixed by updating users was 1.The number of orphaned users fixed by adding new logins and then updating users was 0.

 This saved my day!! Hope it helps you too... 

Sunday, 21 February 2021

To Know Foreign Keys on Table in SQL SERVER

Msg 3726, Level 16, State 1, Line 26

Could not drop object 'Table_name' because it is referenced by a FOREIGN KEY constraint.


To get the list of Foreign Keys

 SELECT * 

FROM sys.foreign_keys

WHERE referenced_object_id = object_id('Table_name')


To Remove the constraints, the given output to be executed to remove Foreign Keys

SELECT 

    'ALTER TABLE [' +  OBJECT_SCHEMA_NAME(parent_object_id) +

    '].[' + OBJECT_NAME(parent_object_id) + 

    '] DROP CONSTRAINT [' + name + ']'

FROM sys.foreign_keys

WHERE referenced_object_id = object_id('Table_name')



Wednesday, 22 July 2020

Error For users in SQL SERVER , the User neither deleting nor modifying

ERROR:

An exception occurred while executing a Transact-SQL statement or batch (Microsoft SQL Server, Error: 15023)

The complate Error is
Microsoft SQL Server Management Studio ------------------------------  Create failed for User 'apuser'.  (Microsoft.SqlServer.Smo)  For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=15.0.18206.0+((SSMS_Rel).191029-2112)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Create+User&LinkId=20476  ------------------------------ ADDITIONAL INFORMATION:  An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)  ------------------------------  User, group, or role 'apuser' already exists in the current database. (Microsoft SQL Server, Error: 15023)

Many times the situation occurred as the SQL SERVER USER neither can be created nor modified throwing the above error. Even the user deleted and created again giving the same error again and again . To rectify this..

sp_change_users_login 'AUTO_FIX', 'someuser'   -- here the user is 'apuser' so the command is

sp_change_users_login 'AUTO_FIX', 'apuser'

The output of the command should be

The row for user ‘apuser’ will be fixed by updating its login link to a login already in existence.
The number of orphaned users fixed by updating users was 1.
The number of orphaned users fixed by adding new logins and then updating users was 0.

The output may different depending on issues, if the issues not there then it will be shown as 0 each.. the command will solve most of the issues , After executing the above test the user creation or modification once again.. if problem still persisting

First of all run following T-SQL Query in Query Analyzer. This will return all the existing users in database in result pan.
USE YourDB
GO
EXEC sp_change_users_login 'Report'GO


Run following T-SQL Query in Query Analyzer to associate login with the username. ‘Auto_Fix’ attribute will create the user in SQL Server instance if it does not exist. In following example ‘ColdFusion’ is UserName, ‘cf’ is Password. Auto-Fix links a user entry in the sysusers table in the current database to a login of the same name in sysxlogins.

USE YourDB
GO
EXEC sp_change_users_login 'Auto_Fix''apuser', NULL, 'cf'GO


After proceeding the above steps check once again
If the problem persisting delete the user and create again by using the following command

USE YourDB
GO
EXEC sp_dropuser 'apuser'GO

Delete the user by selecting the assigned database only