SansSQL

Sunday, May 23, 2010

Row Not Found at the Subscriber - Replication Issue

When you find an issue in replication with the error “The row was not found at the Subscriber when applying the replicated command.”, first we have to get the Transaction sequence number and Command ID from the error.
This can be found at Distributer to Subscriber history in replication monitor.

Once we get the Transaction Sequence Number and Command ID we can easily drill down to the command which is causing the issue by using sp_browsereplcmds. Before to this, we have to also find out publisher_database_id.

For finding publisher_database_id, we need to make use of Transaction Sequence Number and Command ID.
Query to find publisher_database_id using Transaction Sequence Number and Command ID
select * from msrepl_commands
where xact_seqno = 0x000BF8FB0003411E000400000000 and command_id=6



Once we get the publisher_database_id from the above query, then we need to execute the below query to get the command which is causing the error.
Query to find the command which is causing error
exec sp_browsereplcmds @xact_seqno_start = '0x000BF8FB0003411E000400000000',
@xact_seqno_end = '0x000BF8FB0003411E000400000000', @Command_id=6, @publisher_database_id=60


Once we get the command, we can manually sync the missing data from publisher to subscriber to make the replication work fine as before.

Note: All these commands have to be run on distribution database.

This article is also available in pdf format for downloading.
Please Click here to get your copy.





Friday, April 30, 2010

T-SQL - Find which Object resides in which FileGroup

To Find which object resides in which FileGroup in a database, run the below queries.

/**************** For SQL 2000 ****************/

Select Distinct OBJECT_NAME(SI.id) AS ObjectName
        ,SO.type AS ObjectType
        ,FG.GroupName AS FileGroupName
from sysindexes SI , sysfilegroups FG ,sysobjects SO
where SI.groupid = FG.groupid and
      SI.id = SO.id
Order by ObjectName

/**************** For SQL 2005 and SQL 2008 ****************/

Select Distinct OBJECT_NAME(SI.object_id) AS ObjectName
        ,OBJECTPROPERTYEX(SI.object_id,'BaseType') AS ObjectType
        ,FG.Name AS FileGroupName
from sys.indexes SI , sys.filegroups FG
where SI.data_space_id = FG.data_space_id
Order by ObjectName

Statistics Updated Date

Statistics in SQL Server refers specifically to information that the server collects about the distribution of data in columns and indexes. More information about Statistics on How to Create, Drop and update Statistics are covered here.
In this post, we will see how to retrieve the information on when a Statistics was last updated.
To retrieve this information, run the below query on one of the database.
SELECT OBJECT_NAME(OBJECT_ID) AS TableName, name AS IndexName, Type_desc AS IndexTypeTableType=Case When OBJECTPROPERTY(OBJECT_ID,'IsUserTable')=0 Then 'SystemTable' When OBJECTPROPERTY(OBJECT_ID,'IsUserTable')=1 Then 'UserTable' ENDSTATS_DATE(OBJECT_ID, index_id) AS StatsUpdatedDate 
FROM sys.indexes 
Where OBJECTPROPERTY(OBJECT_ID,'IsUserTable')=
Order by TableName

This query will list the indexes present on all of the user tables in a particular database and when their Statistics was last updated.

Saturday, April 24, 2010

Revoke access for all Orphaned users in a server

When a database is moved or restored from one server to another server the login Id's stored in the master database do not align with the login Id's stored in the user database, which account for the orphaned users in the databases.


To get the list of Orphaned users in a database, execute the below query on the particular database.
Exec sp_change_users_login 'Report'


To cycle through all the databases in a server to find the orphaned users, execute the below query

Exec sp_MSforeachdb 'use ?;
Select db_name() as DatabaseName
Exec sp_change_users_login ''Report'''

To Revoke access for all the Orphaned Users in a server, Run the below script

Create a History table in tempdb or in any other DB as required or if required.


Create Table tempdb..OrphUsers_Histroy
(Username nvarchar(250),
userSID varbinary (85),
SQLText nvarchar(500),
Fixedon datetime)


Select * from tempdb..OrphUsers_Histroy

Exec sp_MSforeachdb 'use ?;


Create Table tempdb..OrphUsers 
(Username nvarchar(250),
userSID varbinary (85),
SQLText nvarchar(500))


insert into tempdb..OrphUsers (Username,userSID) 
Exec sp_change_users_login ''Report''


update tempdb..OrphUsers set SQLTEXT=''Exec sp_revokedbaccess ''+username+''''


while (select COUNT(*) from tempdb..OrphUsers )<>0
Begin
declare @SQLTEXT nvarchar(500)
select @SQLTEXT=SQLText from tempdb..OrphUsers
exec sp_executesql @SQLTEXT


/* Remove this insert statement if you do not want to log history */ 
insert into tempdb..OrphUsers_Histroy
select Username,userSID,SQLText,getdate() from tempdb..OrphUsers where SQLTEXT=@SQLTEXT


delete from tempdb..OrphUsers where SQLTEXT=@SQLTEXT
END


drop table tempdb..OrphUsers '

Select * from tempdb..OrphUsers_Histroy

Wednesday, April 14, 2010

Query to find Dependant Objects

Select DISTINCT (OBJECT_SCHEMA_NAME(id)+'.'+ OBJECT_NAME(id)) AS [Object Name] ,(OBJECT_SCHEMA_NAME(depid)+'.'+ OBJECT_NAME(depid)) AS [Is Dependant on], OBJECTPROPERTYEX(id,'BaseType') AS [Object Type],OBJECTPROPERTYEX(depid,'BaseType') AS [Object Type of Is Dependant on] From sys.sysdepends Where OBJECTPROPERTYEX(id,'IsMSShipped')=0 -- To get only user created object

Ads