Wednesday, April 15, 2015

How to find all the dependencies of a table in sql server

Following are the way can check the depedencies


Method 1: Using sp_depends

 sp_depends 'dbo.First'  
 GO  


Method 2: Using information_schema.routines
 SELECT *   
  FROM information_schema.routines ISR   
  WHERE CHARINDEX('dbo.First', ISR.ROUTINE_DEFINITION) > 0   
  GO  


Method 3: Using DMV sys.dm_sql_referencing_entities

 SELECT referencing_schema_name, referencing_entity_name, referencing_id, 
 referencing_class_desc, is_caller_dependent FROM sys.dm_sql_referencing_entities
 ('dbo.First', 'OBJECT'); GO referencing_id, referencing_class_desc, is_caller_dependent  
 FROM sys.dm_sql_referencing_entities ('dbo.First', 'OBJECT');  
 GO  

Tuesday, April 14, 2015

Insert value from one table to another in SQL Server

Example

 INSERT INTO new_table (Col1, Col2, Col3, Coln)  
 SELECT Value1, Value2, Value3, Valuen
 FROM initial_table  
 -- optionally WHERE / JOINS ...  

Update one table based on another table using JOIN - SQL Server

Example

  UPDATE t1 SET   
     t1.status = 1  
  FROM  table1 t1   
     INNER JOIN table t2   
           ON t1.Id = t2.ID  
  WHERE t2.num = 15   

Sunday, April 12, 2015

When restoring a backup, how do I disconnect all active connections? SQL Server

SQL Server Management Studio 2008

The interface has changed for SQL Server Management studio 2008, here are the steps 

  1. Right-click the server in Object Explorer and select 'Activity Monitor'.
  2. When this opens, expand the Processes group.
  3. Now use the drop-down to filter the results by database name.
  4. Kill off the server connections by selecting the right-click 'Kill Process' option.