本文介绍了如何在sql server中获取表的第n行和第n列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨专家
我有一张桌子,我需要在sql server2008中访问该表的第n行和第n列怎么可能??????
Hi Experts
I have a table and i need to acess the nth row and nth column of that table in sql server2008 How is it possible??????
推荐答案
CREATE TABLE TempTable
(
Sno INT IDENTITY(1,1),
UserId INT,
DeptId INT,
FirstName varchar(100),
Lastname varchar(100),
Email varchar(100),
IsDeleted bit
)
INSERT INTO TempTable VALUES(1,2,'John','Smith','[email protected]',0)
INSERT INTO TempTable VALUES(1,2,'Tom','Cruse','[email protected]',0)
INSERT INTO TempTable VALUES(1,2,'Sunny','Leone','[email protected]',0)
INSERT INTO TempTable VALUES(1,2,'Kat','Wins','[email protected]',0)
INSERT INTO TempTable VALUES(1,2,'Amit','Kappa','[email protected]',0)
--SELECT * FROM TempTable
DECLARE @SQLQuery NVARCHAR(2000)
DECLARE @TableName AS NVARCHAR(200)
DECLARE @NthColumn AS INT
DECLARE @NthRow AS NVARCHAR(1000)
DECLARE @ColName AS VARCHAR(100)
SET @TableName ='TempTable'
SET @NthColumn=6
SET @NthRow = '5'
SELECT @ColName = COL_NAME(OBJECT_ID(@TableName),@NthColumn)
SET @SQLQuery = 'SELECT ' + @ColName + ' FROM ' + '(
SELECT *,ROW_NUMBER() OVER (ORDER BY ' + @ColName +' DESC) R FROM '+@TableName+ ' ) Temp
WHERE R='+ @NthRow
PRINT @SQLQuery
Exec(@SQLQuery)
DROP TABLE TempTable
这篇关于如何在sql server中获取表的第n行和第n列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!