Tuesday, July 27, 2004

Retrive records without column name

Hi,

Create a Stored Procedure or Function using following script, and pass
tablename and NoOfColumns as Input Parameters. You problem is solved

Declare @TableName nvarchar(1000)
Declare @NoOfColumns Int
Set @NoOfColumns = 3
Set @TableName = 'Stores'

Declare @ID INT
Declare @SqlString nvarchar(3000), @SqlString1 nvarchar(3000)
Set @SQLString = 'Select '
Select @ID = id From SYSOBJECTS WHERE xtype = 'U' and Name = @TableName
Declare @SQlParameters nvarchar(1000)
Set @SqlParameters = N'@SQLString nvarchar(3000)
OUTPUT, @ID Int'
Set @SqlString1 = 'Select Top ' + Cast(@NoOfColumns as varchar(5)) + '
@SQLString = @SQLString + '' '' + name + '','' From SYSCOLUMNS WHERE id =
@ID order by colid'
PRINT @SQLString1
Exec sp_executesql @SqlString1, @SqlParameters, @SqlString OUTPUT, @ID
SeT @SQLString = Left(@SqlString, Len(@SqlString) - 1) + ' From ' +
@tablename
Exec sp_executesql @SqlString




No comments: