{"id":217,"date":"2011-06-27T17:19:19","date_gmt":"2011-06-27T16:19:19","guid":{"rendered":"http:\/\/csopro.de\/biblog\/2011\/06\/top-n-in-sql-2000\/"},"modified":"2011-06-27T17:19:19","modified_gmt":"2011-06-27T16:19:19","slug":"top-n-in-sql-2000","status":"publish","type":"post","link":"https:\/\/www.csopro.de\/biblog\/2011\/06\/top-n-in-sql-2000\/","title":{"rendered":"Dynamisches Top n in SQL 2000"},"content":{"rendered":"<p>Ab SQL Server 2005 kann man TOP n \u00fcber SELECT TOP (@n) \u2026 erledigen:<\/p>\n<blockquote>\n<p>declare @n integer     <br \/>set @n = 2<\/p>\n<p>select top (@n) a     <br \/>from      <br \/>(select 1 as a      <br \/>union all      <br \/>select 2      <br \/>union all      <br \/>select 3      <br \/>union all      <br \/>select 4      <br \/>union all      <br \/>select 5      <br \/>union all      <br \/>select 6      <br \/>) as t      <br \/>order by a desc      <\/p>\n<\/blockquote>\n<p>Das liefert als Ergebnis<\/p>\n<p>a   <br \/>&#8212;&#8212;&#8212;&#8211;    <br \/>6    <br \/>5<\/p>\n<p>(2 row(s) affected)   <\/p>\n<p>In fr\u00fcheren Versionen geht das leider nicht. Da hilft die Verwendung von set rowcount, das die Anzahl der zur\u00fcckgegebenen Zeilen definiert:<\/p>\n<blockquote>\n<p>declare @n integer      <br \/>set @n = 2<\/p>\n<p>set rowcount @n<\/p>\n<p>select distinct&#160; a       <br \/>from       <br \/>(select 1 as a       <br \/>union all       <br \/>select 2       <br \/>union all       <br \/>select 3       <br \/>union all       <br \/>select 4       <br \/>union all       <br \/>select 5       <br \/>union all       <br \/>select 6       <br \/>) as t       <br \/>order by a desc<\/p>\n<\/blockquote>\n<p>Das liefert das gleiche Ergebnis wie oben.<\/p>\n<p>Allerdings muss man danach wieder <\/p>\n<blockquote>\n<p><font color=\"#333333\">set rowcount 0<\/font><\/p>\n<\/blockquote>\n<p>absetzen, damit alle nachfolgenden SQL-Statements in dieser Session wieder alle Datens\u00e4tze zur\u00fcckliefern. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ab SQL Server 2005 kann man TOP n \u00fcber SELECT TOP (@n) \u2026 erledigen: declare @n integer set @n = 2 select top (@n) a from (select 1 as a union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 ) as t order by &hellip; <a href=\"https:\/\/www.csopro.de\/biblog\/2011\/06\/top-n-in-sql-2000\/\" class=\"more-link\"><span class=\"screen-reader-text\">Dynamisches Top n in SQL 2000<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-sqlserver"],"_links":{"self":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":0,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}