All Rows value into comma seprated values into one single variable

Posted in Sql server 2005, SQL Server 2008, Uncategorized on March 23, 2010 by bimal4u

declare @SQLCol nvarchar(max), @TableName nvarchar(max)
select @SQLCol = COALESCE(@SQLCol+’,’,”)+ sc.COLUMN_NAME FROM information_schema.columns sc
where sc.COLUMN_NAME like ‘%ColumnName%’ and sc.TABLE_NAME = @TableName
order by sc.COLUMN_NAME asc

Print @SQLCol

Note:
COALESCE() : This function returns single value of collect all the values with user defined format. Here for each records i used ‘,’ but you can use anything you want for seperation. This is very good function provided by SQL server.

Get All tables rows from database with specific/All columns

Posted in Sql server 2005, SQL Server 2008 on March 23, 2010 by bimal4u

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getAllTablesRows]’) AND type in (N’P’))
DROP procedure [dbo].[getAllTablesRows]
Go

create procedure getAllTablesRows
As
Begin
DECLARE @TableName sysname ,
@SQL nvarchar(max),
@SQLCol nvarchar(max),
@num int

IF OBJECT_ID(‘#Temp’) IS NOT NULL
drop table #Temp

create TABLE #Temp

( TableName nvarchar(max) NULL DEFAULT(”),
ColumnCount nvarchar(max) NULL DEFAULT(”),
CreatedBY nvarchar(max) NULL DEFAULT(”),
CreatedByName nvarchar(max) NULL DEFAULT(”),
CreatedOn nvarchar(max) NULL DEFAULT(”),
ModifiedBy nvarchar(max) NULL DEFAULT(”),
ModifiedOn nvarchar(max) NULL DEFAULT(”)
)

–set @SQL = ‘insert into #Temp ‘
DECLARE tables_cursor CURSOR FAST_FORWARD
FOR
SELECT so.name from
sysobjects so
where so.xtype =’U’

order by so.name

OPEN tables_cursor

FETCH NEXT FROM tables_cursor INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN

select @num=count(TABLE_NAME) FROM information_schema.columns sc
where sc.COLUMN_NAME like ‘%ColumnName%’ and sc.TABLE_NAME = @TableName
group by TABLE_NAME

IF @num > 0
Begin
set @SQL = ‘ SELECT Top 5 ”’ + @TableName + ”’as TableName, ‘ + convert (nvarchar(10), @num ) + ‘ As ColumnCount ‘
set @SQLCol = ”
select @SQLCol = COALESCE(@SQLCol+’,’,”)+ sc.COLUMN_NAME FROM information_schema.columns sc
where sc.COLUMN_NAME like ‘%ColumnName%’ and sc.TABLE_NAME = @TableName
order by sc.COLUMN_NAME asc

if @num=1
Begin
set @SQLCol = @SQLCol + ‘,NULL,NULL,NULL,NULL’
End
else if @num=2
Begin
set @SQLCol = @SQLCol + ‘,NULL,NULL,NULL’
End
else if @num=3
Begin
set @SQLCol = @SQLCol + ‘,NULL,NULL’
End
else if @num=4
Begin
set @SQLCol = @SQLCol + ‘,NULL’
End

set @SQL = ‘insert into #Temp ‘+ @SQL + @SQLCol + ‘ FROM [‘ + @TableName + ‘] ‘
exec @SQL
set @num=0

End

FETCH NEXT FROM tables_cursor INTO @TableName
END

CLOSE tables_cursor
DEALLOCATE tables_cursor

select * from #Temp

End

Give all dependecy related table from the database. Its not included reference key.

Posted in Sql Server 2000, Sql server 2005 on March 23, 2010 by bimal4u

SELECT DISTINCT OBJECT_NAME(DEPID) DEPENDENT_ON_OBJECT, OBJECT_NAME (ID) USED_IN_OBJECT FROM SYS.SYSDEPENDS
order by OBJECT_NAME(DEPID),OBJECT_NAME (ID)

Get All tables infromation from the database

Posted in Uncategorized on March 23, 2010 by bimal4u

SELECT * from sysobjects where xtype =’U’

Get Record all table name with specific column and its all information from the database

Posted in Sql Server 2000, Sql server 2005 on March 23, 2010 by bimal4u

SELECT so.name , column_name ‘Column Name’,
data_type ‘Data Type’,
CHARacter_maximum_length ‘Maximum Length’
FROM information_schema.columns sc
inner join sysobjects so on sc.TABLE_NAME = so.name
where so.xtype =’U’ and (sc.COLUMN_NAME like ‘%columnName%’)
order by so.name,sc.COLUMN_NAME asc