cookieChoices = {};

Wednesday, 4 May 2016

Get Current Language of SQL Server

Get Current Language of SQL Server



SELECT @@LANGUAGE AS Current_Language;  

Get the version name of SQL Server

Get the version name of SQL Server


SELECT @@VERSION AS Version_Name  

List of tables with number of records

List of tables with number of records


CREATE TABLE #Tab  
  
(  
  
Table_Name [varchar](max),  
  
Total_Records int  
  
);  
  
EXEC sp_MSForEachTable @command1=' Insert Into #Tab(Table_Name, Total_Records)

 SELECT ''?'', COUNT(*) FROM ?'  
  
SELECT * FROM #Tab t ORDER BY t.Total_Records DESC;  
  
DROP TABLE #Tab;

RESEED Identity of all tables

RESEED Identity of all tables


EXEC sp_MSForEachTable '  
  
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1  
  
DBCC CHECKIDENT (''?'', RESEED, 0)

List of Primary Key and Foreign Key for a particular table

List of Primary Key and Foreign Key for a particular table


SELECT  
  
DISTINCT  
  
Constraint_Name AS [Constraint],  
  
Table_Schema AS [Schema],  
  
Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE  
  
WHERE INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME='Table_Name'  
  
GO  

List of Primary Key and Foreign Key for Whole Database

List of Primary Key and Foreign Key for Whole Database


SELECT  
  
DISTINCT  
  
Constraint_Name AS [Constraint],  
  
Table_Schema AS [Schema],  
  
Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE  
  
GO  

Get all table that don’t have identity column

Get all table that don’t have identity column:


SELECT  
  
TABLE_NAME FROM INFORMATION_SCHEMA.TABLES  
  
where  
  
Table_NAME NOT IN  
  
(  
  
SELECT DISTINCT c.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS c  
  
INNER  
  
JOIN sys.identity_columns ic  
  
on  
  
(c.COLUMN_NAME=ic.NAME))  
  
AND  
  
TABLE_TYPE ='BASE TABLE'