Senaste inläggen 

Taggar 

package load     connect     Techdays     SQL Server 2012     filter     T-SQL     Trace Flag     HADR     transactions     sql 2008     Activity Monitor     connection     SQL2008     sp_MSForEachDB     function     Reports     Cluster     page splits     performance     SSRS 2008     clean up     resource governor     parameters     SSAS     access denied     login error     2005     DTA     Logins     0xC0202009     create index     Business Intelligence     2000     undocumented procedures     sp1     SSIS     feedback     error     profile     Säkerhet     sql 2005     sql browser     features     constraint     AcquireConnection     2008     Microsoft     Page life expectancy     CU3     security     gratis verktyg     dbmail     BOL     temp table     #am_get_querystats     CU1     reorganize index     Extended Event     history     CMS     SQL server codename Denali     SQL Denali     Datawarehouse     SQL Server     data warehouse     CTE     0xC0010014     virtuell     CTP1     platsannons SQL utvecklare     rebuild     parallelism     concatenation     bugs     DECIMAL     central management server     HEAP     SSRS     improve     XP_cmdshell     2011

Just a brief demo of dynamic use of OFFSET through a loop

Skrivet den 09 augusti 2012 i Mattias Lind, T-SQL, SQL Server 2012, Level 300, sv, en
DECLARE -- Let us declare variables:
  @count int = 0, -- Start
  @to int, -- End
  @rows int = 10; -- Rows per set
SELECT -- Now we're gonna
  @to = count(*) / @rows -- determe the number of sets
FROM sys.columns -- from this table
WHILE @count <= @to -- This loops all through to the last set
BEGIN  
  SELECT -- Returning these
    * -- columns
  FROM sys.columns -- from this table
  ORDER BY 1 ASC -- in this order (here just based on first column).
  OFFSET (0 + (@count * @rows)) -- Based on loop we get the starting point of the offset.
  ROW FETCH NEXT (@rows) ROWS ONLY -- The number of rows we're fetching!
  SET @count += 1 -- Next step!
END  

Skriv en kommentar