如何使用select查询在不使用where子句和游标的情况下获取特定记录。 [英] How to get a specific record without using where clause and cursor in sql using select query.

查看:122
本文介绍了如何使用select查询在不使用where子句和游标的情况下获取特定记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张这样的桌子。



EmpCode
101
102
103
104




假设用户想要提取第4行或第2行等任何记录那么如何使用select语句编写一个查询,该语句在不使用Where子句的情况下给出了确切的结果。



我们有什么方法可以找到确切的记录。数组的一个例子然后在数组中如果我们想获得第三条记录,那么我们可以直接使用ECode [3]并得到103作为结果。我想使用相同而不使用任何游标值值而且也没有where子句。

解决方案





查看这个



http://howtoideas.net/how-to- select-nth-row-from-a-table-in-sql-server [ ^ ]


今天!我发现了与OP提到的相同的问题,并且在GOOGLING之后,我在CP上找到了这个已解决的帖子,但看到回答的链接无法正常工作感到震惊和恐惧...... :(

无论如何稍晚一点点后,找到了我的查询答案,



并想到在这里分享它,只是为了其他天真就像我一样对未来。





好​​吧。让我们来谈谈。如果用户想要搜索表的第二行不使用WHERE子句。



下面的块进入游戏。:)



  SELECT   TOP   1  T. *  FROM  
SELECT TOP 2 * FROM TableName ORDER BY ID ASC AS T
ORDER BY T.ID DESC ;





逻辑非常简单,内部查询得到第n行,即在这种情况下为2行,而外部查询则是结果并获得最高分。 :)



希望它会有所帮助。 :)


I have a table like this.

EmpCode
101
102
103
104


Suppose a user want to extract any record like 4th row or 2nd row then how to write a query using select statement which gives the exact result without using Where clause.

Is there any way through which we can find the exact record.lets take an example of array then In array if we want to get 3rd record then we can directly use ECode[3] and we get 103 as result.I want to use the same without using any cursor value value and no where clause also.

解决方案

Hi,

check this out

http://howtoideas.net/how-to-select-nth-row-from-a-table-in-sql-server[^]


Today! I found the same issue as mentioned by the OP, and after GOOGLING, I landed on this SOLVED POST at CP, but shocked and terrified to see the answered link not working... :(

anyway a bit later after bit more GOOGLING , found an answer to my query,

and thought of sharing it here, just for the sake of other naive's like me for future.


Well. Let comes to the point. if a user wants to search 2nd row of a table without using WHERE clause.

the following chunk comes into the play. :)

SELECT TOP 1 T.* FROM
(SELECT TOP 2 * FROM TableName ORDER BY ID ASC) AS T
ORDER BY T.ID DESC;



The logic is pretty simple, Inner Query gets the nth number of rows i.e. 2 top rows in this case, and than outer query descended that result and get the top of that. :)

Hope it will help. :)


这篇关于如何使用select查询在不使用where子句和游标的情况下获取特定记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆