Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Query pagination around multiple select

$
0
0
Hello all!I need to use on this query the pagination with parameter "Skip" and "Take" around multiple select table. I already have the query that return multiple table, but I need to implement the logic for the offset fetch pagination operation. You can test the query just only set the @SearchTerm with any value you want that exists in your database. Currently this query just output all tables that contains the value (with the rows count inside the table)[code="sql"]-- credit to http://stackoverflow.com/a/12306613DECLARE @Skip int = 31 -- GET FROM ROW 31DECLARE @Take int = 10 -- TAKE 10 FROM @SKIP (31) SO FROM 31 TO 41DECLARE @SearchTerm nvarchar(4000) = N'texttosearch' -- VALUE TO SEARCH IN DATABASEDECLARE @TableName sysnameDECLARE @TotalCount int = 0set @TableName = N'' -- DONT CARE NOW ABOUT THISset nocount onset @SearchTerm = N'%' + @SearchTerm + '%'declare @TabCols table ( id int not null primary key identity , table_schema sysname not null , table_name sysname not null , column_name sysname not null , data_type sysname not null)insert into @TabCols (table_schema, table_name, column_name, data_type) select t.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE from INFORMATION_SCHEMA.TABLES t join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_SCHEMA = c.TABLE_SCHEMA and t.TABLE_NAME = c.TABLE_NAME where 1 = 1 and t.TABLE_TYPE = 'base table' and c.DATA_TYPE not in ('image', 'sql_variant') and c.TABLE_NAME like case when len(@TableName) > 0 then @TableName else '%' end order by c.TABLE_NAME, c.ORDINAL_POSITIONdeclare @table_schema sysname , @table_name sysname , @column_name sysname , @data_type sysname , @exists nvarchar(4000) -- Can be max for SQL2005+ , @sql nvarchar(4000) -- Can be max for SQL2005+ , @sqlcount nvarchar(4000) -- Can be max for SQL2005+ , @where nvarchar(4000) -- Can be max for SQL2005+ , @runcount nvarchar(4000) -- Can be max for SQL2005+ , @run nvarchar(4000) -- Can be max for SQL2005+while exists (select null from @TabCols) begin select top 1 @table_schema = table_schema , @table_name = table_name , @exists = 'select null from [' + table_schema + '].[' + table_name + '] where 1 = 0' , @sql = 'select ''' + '[' + table_schema + '].[' + table_name + ']' + ''' as TABLE_NAME, * from [' + table_schema + '].[' + table_name + '] where 1 = 0' , @sqlcount = 'select @count = count(*) from [' + table_schema + '].[' + table_name + '] where 1 = 0' , @where = '' from @TabCols order by id declare @first_column nvarchar(50) = N'' while exists (select null from @TabCols where table_schema = @table_schema and table_name = @table_name) begin select top 1 @column_name = column_name , @data_type = data_type from @TabCols where table_schema = @table_schema and table_name = @table_name order by id if @first_column = '' begin set @first_column = @column_name end -- Special case for money if @data_type in ('money', 'smallmoney') begin if isnumeric(@SearchTerm) = 1 begin set @where = @where + ' or [' + @column_name + '] = cast(''' + @SearchTerm + ''' as ' + @data_type + ')' -- could also cast the column as varchar for wildcards end end -- Special case for xml else if @data_type = 'xml' begin set @where = @where + ' or cast([' + @column_name + '] as nvarchar(max)) like ''' + @SearchTerm + '''' end -- Special case for date else if @data_type in ('date', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime', 'time') begin set @where = @where + ' or convert(nvarchar(50), [' + @column_name + '], 121) like ''' + @SearchTerm + '''' end -- Search all other types else begin set @where = @where + ' or [' + @column_name + '] like ''' + @SearchTerm + '''' end delete from @TabCols where table_schema = @table_schema and table_name = @table_name and column_name = @column_name end -- Count row in table declare @count int set @runcount = @sqlcount + @where exec sp_executesql @runcount, N'@count int OUTPUT', @count OUTPUT -- PROBABLY HERE BEGIN THE LOGIC TO IMPLEMENT IF @count > 0 BEGIN --set @run = @sql + @where + ' ORDER BY [' + @first_column + '] OFFSET ' + convert(nvarchar(255), @Skip) +' ROWS FETCH NEXT ' + convert(nvarchar(255), @Take) + ' ROWS ONLY' set @run = 'TABLE WITH ' + convert(nvarchar(255), @count) + ' ROWS => OFFSET ' + convert(nvarchar(255), @Skip) +'(wrong) ROWS FETCH NEXT ' + convert(nvarchar(255), @Take) + '(wrong) ROWS ONLY' print @run --exec sp_executesql @run set @TotalCount = @TotalCount + @count END -- PROBABLY HERE EMD THE LOGIC TO IMPLEMENTendprint 'TOTAL COUNT => ' + convert(nvarchar(255), @TotalCount)set nocount off[/code]------------------------------------------------------[Example #1]I have 4 tables that match "mytexttosearchexample1":first table have 122 rows second table have 15 rowsthird table have 53 rowsfourth table have 38 rowsSo, if @Skip is 130 and @Take is 20 a correct result by the query must be:[code="other"]TABLE WITH 15 ROWS => OFFSET 8 ROWS FETCH NEXT 7 ROWS ONLYTABLE WITH 53 ROWS => OFFSET 1 ROWS FETCH NEXT 13 ROWS ONLYTOTAL COUNT => 228[/code][Example #2]I have 3 tables that match "mytexttosearchexample2":first table have 14 rows second table have 32 rowsthird table have 11 rowsSo, if @Skip is 5 and @Take is 10 a correct result by the query must be:[code="other"]TABLE WITH 14 ROWS => OFFSET 5 ROWS FETCH NEXT 9 ROWS ONLYTABLE WITH 32 ROWS => OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLYTOTAL COUNT => 57[/code]------------------------------------------------------I very near to get that solved! That's my current work, anyway i think that i'm missing some "IF"[code="sql"]-- credit to http://stackoverflow.com/a/12306613DECLARE @Skip int = 1 -- GET FROM ROW 31DECLARE @Take int = 9 -- TAKE 10 FROM @SKIP (31) SO FROM 31 TO 41DECLARE @ActualSkip int = @SkipDECLARE @ActualTake int = @TakeDECLARE @SearchTerm nvarchar(4000) = N'texttosearch' -- VALUE TO SEARCH IN DATABASEDECLARE @TableName sysnameDECLARE @TotalCount int = 0set @TableName = N'' -- DONT CARE NOW ABOUT THISset nocount onset @SearchTerm = N'%' + @SearchTerm + '%'declare @TabCols table ( id int not null primary key identity , table_schema sysname not null , table_name sysname not null , column_name sysname not null , data_type sysname not null)insert into @TabCols (table_schema, table_name, column_name, data_type) select t.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE from INFORMATION_SCHEMA.TABLES t join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_SCHEMA = c.TABLE_SCHEMA and t.TABLE_NAME = c.TABLE_NAME where 1 = 1 and t.TABLE_TYPE = 'base table' and c.DATA_TYPE not in ('image', 'sql_variant') and c.TABLE_NAME like case when len(@TableName) > 0 then @TableName else '%' end order by c.TABLE_NAME, c.ORDINAL_POSITIONdeclare @table_schema sysname , @table_name sysname , @column_name sysname , @data_type sysname , @exists nvarchar(4000) -- Can be max for SQL2005+ , @sql nvarchar(4000) -- Can be max for SQL2005+ , @sqlcount nvarchar(4000) -- Can be max for SQL2005+ , @where nvarchar(4000) -- Can be max for SQL2005+ , @runcount nvarchar(4000) -- Can be max for SQL2005+ , @run nvarchar(4000) -- Can be max for SQL2005+while exists (select null from @TabCols) begin select top 1 @table_schema = table_schema , @table_name = table_name , @exists = 'select null from [' + table_schema + '].[' + table_name + '] where 1 = 0' , @sql = 'select ''' + '[' + table_schema + '].[' + table_name + ']' + ''' as TABLE_NAME, * from [' + table_schema + '].[' + table_name + '] where 1 = 0' , @sqlcount = 'select @count = count(*) from [' + table_schema + '].[' + table_name + '] where 1 = 0' , @where = '' from @TabCols order by id declare @first_column nvarchar(50) = N'' while exists (select null from @TabCols where table_schema = @table_schema and table_name = @table_name) begin select top 1 @column_name = column_name , @data_type = data_type from @TabCols where table_schema = @table_schema and table_name = @table_name order by id if @first_column = '' begin set @first_column = @column_name end -- Special case for money if @data_type in ('money', 'smallmoney') begin if isnumeric(@SearchTerm) = 1 begin set @where = @where + ' or [' + @column_name + '] = cast(''' + @SearchTerm + ''' as ' + @data_type + ')' -- could also cast the column as varchar for wildcards end end -- Special case for xml else if @data_type = 'xml' begin set @where = @where + ' or cast([' + @column_name + '] as nvarchar(max)) like ''' + @SearchTerm + '''' end -- Special case for date else if @data_type in ('date', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime', 'time') begin set @where = @where + ' or convert(nvarchar(50), [' + @column_name + '], 121) like ''' + @SearchTerm + '''' end -- Search all other types else begin set @where = @where + ' or [' + @column_name + '] like ''' + @SearchTerm + '''' end delete from @TabCols where table_schema = @table_schema and table_name = @table_name and column_name = @column_name end -- Count row in table declare @count int set @runcount = @sqlcount + @where exec sp_executesql @runcount, N'@count int OUTPUT', @count OUTPUT -- PROBABLY HERE BEGIN THE LOGIC TO IMPLEMENT if @count > 0 begin if @count >= @ActualSkip begin if @take <= @count begin if @ActualSkip + @Take <= @count begin set @ActualTake = @Take end else begin set @ActualTake = @count - @ActualSkip end end else begin set @ActualTake = @count end end else begin set @ActualTake = 0 set @ActualSkip = @ActualSkip - @count end set @Take = @Take - @ActualTake if @ActualTake > 0 begin --set @run = @sql + @where + ' ORDER BY [' + @first_column + '] OFFSET ' + convert(nvarchar(255), @Skip) +' ROWS FETCH NEXT ' + convert(nvarchar(255), @Take) + ' ROWS ONLY' set @run = 'TABLE WITH ' + convert(nvarchar(255), @count) + ' ROWS => OFFSET ' + convert(nvarchar(255), @ActualSkip) +' ROWS FETCH NEXT ' + convert(nvarchar(255), @ActualTake) + ' ROWS ONLY' print @run --exec sp_executesql @run end set @TotalCount = @TotalCount + @count end -- PROBABLY HERE EMD THE LOGIC TO IMPLEMENTendprint 'TOTAL COUNT => ' + convert(nvarchar(255), @TotalCount)set nocount off[/code]Please to get all table with numbers of existing elements that contains the value refer to the first queryThank you in advance

Viewing all articles
Browse latest Browse all 3145

Trending Articles