Skip to main content
fixed typo
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87

from the comments of @sepupic and @Scott Hodgin, I put this together:

condicionalconditional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + NCHAR(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + NCHAR(10) + 
      N'OFFSET  @offset  ROWS ' + NCHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; '

SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;

from the comments of @sepupic and @Scott Hodgin, I put this together:

condicional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + NCHAR(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + NCHAR(10) + 
      N'OFFSET  @offset  ROWS ' + NCHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; '

SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;

from the comments of @sepupic and @Scott Hodgin, I put this together:

conditional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + NCHAR(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + NCHAR(10) + 
      N'OFFSET  @offset  ROWS ' + NCHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; '

SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;
little changes in the script
Source Link
Marcello Miorelli
  • 16.6k
  • 53
  • 170
  • 311

from the comments of @sepupic and @Scott Hodgin, I put this together:

condicional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'
declare @offset int, @limit int

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + charNCHAR(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + CHARNCHAR(10) + 
      N'OFFSET  @offset  ROWS ' + CHARNCHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; ' 

SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;

from the comments of @sepupic and @Scott Hodgin, I put this together:

condicional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'
declare @offset int, @limit int

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + char(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + CHAR(10) + 
      N'OFFSET  @offset  ROWS ' + CHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; '
SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;

from the comments of @sepupic and @Scott Hodgin, I put this together:

condicional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + NCHAR(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + NCHAR(10) + 
      N'OFFSET  @offset  ROWS ' + NCHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; ' 

SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;
Source Link
Marcello Miorelli
  • 16.6k
  • 53
  • 170
  • 311

from the comments of @sepupic and @Scott Hodgin, I put this together:

condicional order by

DECLARE @sql nvarchar(MAX);  
DECLARE @params nvarchar(100);  
declare @sort_col nvarchar(20) = 'name_voter'
declare @sort_dir nvarchar(20) = 'asc'
declare @offset int, @limit int

SET @sql =  
     N'SELECT id, name_voter, home_street_address_1,home_address_city  
       FROM dbo.base_voter 
       WITH(NOLOCK)' + char(10) +
      N'ORDER BY ' + @sort_col + SPACE(2) + @sort_dir + CHAR(10) + 
      N'OFFSET  @offset  ROWS ' + CHAR(10) + 
      N'FETCH NEXT  @limit  ROWS ONLY; '
SET @params = N' @offset int, @limit int';

--print @sql

EXECUTE sp_executesql @sql, @params, 0, 50;