ORPHAN USERS FIND AND FIX SCRIPT in SQL Server

Orphan Users find and fix: after the migration need to find and if is there any orphan users need to fix

EXEC sp_change_users_login 'Report'

--Use below code to fix the Orphan User issue

DECLARE @username varchar(25)
DECLARE fixusers CURSOR
FOR
SELECT UserName = name FROM sysusers
WHERE issqluser = 1 and (sid is not null and sid <> 0x0)
and suser_sname(sid) is null
ORDER BY name
OPEN fixusers
FETCH NEXT FROM fixusers
INTO @username
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_change_users_login 'update_one', @username, @username
FETCH NEXT FROM fixusers
INTO @username
END
CLOSE fixusers
DEALLOCATE fixusers

Unable to Restore the backup files in SQL Server

1. Check whether the Backup is valid or not Restore verifyonly from disk = ‘Backup path’

2. We will check the Base Backup is restored first or not

3. Check the LSN sequence 4. Check user has required permissions

5. Check the disk space

6. We will check basing upon the error message.

Services Failed to start in windows

1. SCM – start instance (run: sqlservermanager.msc)

2. OS services (run: services.msc)

3. SAC – Surface Area Configuration (2005)

4. On the command line: Run – net start service name Find the service name in services.msc

5. Using object explorer (Right click on the instance and try to start)

6. Startup parameters have incorrect file path locations. Right click the instance -> Properties -> General Tab -> startup parameters

7. Need to check whether Services are disabled or not Services.msc

8. We will check basing upon the error message 

Log File full-Troubleshooting

1. Check the log file size by using DBCC sqlperf(logspace), we can increase log size up to 2TB.

2. Purging the old files.

3. Take a Log Backup if database in full recovery model  i. Backup log dbname to disk =’path’

4. Run the checkpoint if database in simple recovery model.

5. Increase the LOG file size or Enable Auto Growth (Make sure it’s not set to Maximum).

6. Add one more T-Log file from  another drive.

7. Shrink the file Use dbname Go DBCC shrinkfile (<transactionlogname>,1).

8. Request windows team to add more space.

9. If the recovery model is Full change to Simple 10. Find the long running transactions and kill after approval.  

DBA Regular usage scripts-SQL DBA


1. On which drive the  database files(mdf & ldf) are located?

use master
select *from sysaltfiles where filename like 'd%'
                      (OR)
select *from master..sysaltfiles where filename like 'd%'

2.How to shrinklog file if it is full?

backup log dbname with no_log
dbcc shrinkfile(2)

3. how to see open transations in a database?
  dbcc opentran

4. HOW TO see if perticlular process is blocked?

Use master
select * from sysprocessess where blocked>0 and spid=57

5. why buffer cache hit ratio <90?

 if the bottlenecks are there
 then the number of pages read in to the cache will vary
 and the ratio of finding the data by the server in the cache will decrease

6 how to find attribute of perticula file?

  Execute spFileDetails 'c:\autoexec.bat'

7. how to see the status of database?

  dbcc showfilestats

8.How to see the  Cpu usage?(if buffer cache hit raio<90)

 select *from sys.sysprocesses order by cpu asc

9.How to find out the info of a specific table on a database?

 select * from sysobjects where name like '%table_name%'

10. What is Undo File? Why it is required?

 Undo file is needed in standby state because while restoring the log backup, uncommited transactions will be recoreded to the undo file and only commited transactions will be written to disk there by making users to read the database. When you restore next tlog backup SQL server will fetch the uncommited transactions from undo file and check with the new tlog backup whether the same is commited or not. If its commited the transactions will be written to disk else it will be stored in undo file until it gets commited or rolledback.

11.How to get the data the from linked server?

 Select * from LinkedServer.DBName.SchemaName.TableName

12.How to find the service pack version?

SELECT SERVERPROPERTY('PRODUCTLEVEL')

13.How to see the collation setting for server level?

SP_HELPSORT

14.How to see log usage info in a database?

select * from  sys.databases where name like '%log_reuse_wait_desc%'

15.How to see the connection info by using DMV'S?

Select * from  sys.dm_exec_sessions

16. How to see the transaction isolation level?

DBCC USEROPTIONS

17.SQL Server 2000 database compatible level to SQL Server 2005

EXEC sp_dbcmptlevel AdventureWorks, 90;

18.How to see the memory ocupation of eache query running under Query Analyzer

select * from sys.dm_exec_query_memory_grants

19.How to clear the data from buffer cashe

DBCC DROPCLEANBUFFERS

20.How to see how much space tempdb is allocated for perticulas task

Select * from sys.dm_db_session_space_usage          
                  OR
sys.dm_db_task_space_usage 

21.The following DMV query can be used to get useful information about the index usage for all objects in all databases

Select * from from sys.dm_db_index_usage_stats 
order by object_id, index_id

22.The following query to list all the schedulers and look at the number of runnable tasks.

 Select * from from 
    sys.dm_os_schedulers where   scheduler_id < 255

23.Which Query is taking more cpu time 

select * from sys.dm_exec_query_stats 

24.How to find whether any active requests are running in parallel for a given session by using the following query.

select * from sys.dm_exec_requests 

25. if sql server installation failed in sql2000 whrere can i see tha failed info?
 sqlstp.log

26. How to see the lock info?

 select * from sys.dm_tran_locks

27. How to open cluadmin from windows 2008 server?

   cluadmin.msc

28. How to backup by using T-sql Query?

backup database DB name to disk = ‘path\full.bak’ with compression,stats=1
backup database DB name to disk = ‘path\diff.bak’ with compression,stats=1
backup log DB name to disk = ‘path\log_trn’ with compression,stats=1

29. How to Restore by using T-sql Query?

Restore database dbname from disk =’path\full.bak’ with move logical name to ‘path.mdf’,move  logical name to ‘path log.ldf’,norecovery,stats=1
Restore database DB name from disk =’path\diff.bak’ with norecovery,stats=1
Restore log DB name from disk = ‘path\log.trn’ with norecovery,stats=1

29.How to find Logical names?

Restore filelistonly from disk = ‘path’