SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[area](
[area_id] [bigint] NOT NULL,
[area_code] [varchar](10) NULL,
[area_name] [varchar](50) NOT NULL,
[latitude] [float] NOT NULL,
[longitude] [float] NOT NULL,
CONSTRAINT [PK_area] PRIMARY KEY CLUSTERED
(
[area_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[area] ([area_id], [area_code], [area_name], [latitude], [longitude]) VALUES (1, N'EKM', N'Ernakulam', 9.97768, 76.29684)
INSERT [dbo].[area] ([area_id], [area_code], [area_name], [latitude], [longitude]) VALUES (2, N'TCR', N'Thrissur', 10.5243, 76.2125)
declare @inpXAxis INT
declare @inpYAxis INT
declare @reqColXAxis as varchar(100)
declare @reqColYAxis as varchar(100)
declare @sq1Query as varchar(MAX)
declare @tableName as varchar(100)
SET @tableName='area'
SET @inpXAxis=3
SET @inpYAxis=2
select @reqColXAxis = column_name from information_schema.columns where table_name = @tableName
and ordinal_position = @inpXAxis
IF OBJECT_ID('tempdb..#temp1') IS NOT NULL
BEGIN
drop table #temp1
END
set @sq1Query = 'select IDENTITY(int,1,1) as myRow, ' + @reqColXAxis+' INTO #temp1 from '+@tableName +' ; SELECT '+@reqColXAxis +' FROM #temp1 WHERE myRow = '+CAST ( @inpYAxis as varchar)
exec(@sq1Query)
good luck ;-)