SQL Query to display the columns and data types in all the tables

August 6, 2012

SQL Query to display the columns and data types in all the tables

The below mentioned script will display the details in the order of TableName, ColPosition, ColumnName, DataType and Length:

SELECT
IT.TABLE_NAME AS ‘TableName’,
ORDINAL_POSITION AS ‘ColPosition’,
COLUMN_NAME AS ‘ColumnName’,
DATA_TYPE AS ‘DataType’,
CHARACTER_MAXIMUM_LENGTH AS ‘Length’
from INFORMATION_SCHEMA.COLUMNS as IC
inner join INFORMATION_SCHEMA.TABLES as IT on  IT.TABLE_NAME=IC.TABLE_NAME and IT.TABLE_TYPE=’BASE TABLE’
ORDER BY
TableName,
ColPosition


Query to get the SQL text of a particular process (SPID)

July 18, 2012

Query to get the SQL text of a particular process (SPID)

To know the process id run sp_who2. You will get the process id.

After getting the process id run the below batch to get the SQL text:

DECLARE @sqlhandle binary(20)
SELECT @sqlhandle=sql_handle FROM sysprocesses WHERE spid=90 — here I use the process id as 90
SELECT text FROM ::fn_get_sql(@sqlhandle)


How to find the SQL Server Edition, Version and Product Level?

July 3, 2012

The following Query will show us the SQL Server Edition, Product Version and level:

SELECT   SERVERPROPERTY(‘productversion’) as ProductVersion, SERVERPROPERTY(‘productlevel’) as ProductLevel, SERVERPROPERTY(‘edition’) as Edition


How to find the duplicate rows in a table

July 3, 2012

To find the duplicate rows in a table we can use the following SQL query:

SELECT
Column1,Column2,COUNT(PK_Column)
FROM
<Table Name>
GROUP BY
Column1,Column2
HAVING
COUNT(ColumnWithDuplicateValues) > 1

 


SQL Query to find the column name in all the tables in a database

June 28, 2012

The below query will display the list of all the tables with same column name:

USE <DatabaseBase>
GO
SELECT T.name AS Table_Name,
SCHEMA_NAME(schema_id) AS Schema_Name,
C.name AS Column_Name
FROM sys.tables AS T
INNER JOIN sys.columns AS C ON T.OBJECT_ID = C.OBJECT_ID
WHERE C.name LIKE ‘%<ColumnName>%’
ORDER BY schema_name, Table_Name;


%d bloggers like this: