1

I want to write like this code:

select col1,col2,col3,col4
from x
where ----
order by all 

More details: I get my columns dynamically from database using Sp_executesql to execute my SQL statement and I don't know what the number of returned columns is, and I want to sort these columns by all these dynamically columns.

This is my code:

ALTER PROCEDURE [dbo].[SP_Get_Trx_Log_details] 
  -- Add the parameters for the stored procedure here   
  @thread_id INT, 
  @frd_id_BU VARCHAR(max), 
  @and_cond  VARCHAR(max)

AS 
  BEGIN 
      DECLARE @tbl_name NVARCHAR(300) 
      DECLARE @type NVARCHAR(10) 
      SELECT @type = Upper(thread_type) 
      FROM   dbo.tbl_frd_threads 
      WHERE  thread_id = @thread_id 

      IF( ( @type = 'M' 
             OR @type = 'Q' 
             OR @thread_id = 50 ) 
          AND @thread_id <> 49 ) 
        BEGIN 

            SET @tbl_name = 'dbo.tbl_frd_details_member_qa det LEFT JOIN dbo.tbl_frd_actions act  ON det.frd_det_id = act.frd_id AND det.thread_id = act.thread_id AND act.action_type = ''C''  '
        END 
      ELSE IF ( ( @type = 'P' 
              OR @thread_id = 49 ) 
           AND @thread_id <> 50 ) 
        BEGIN 

            SET @tbl_name = 'dbo.tbl_frd_details_provider det LEFT JOIN dbo.tbl_frd_actions act  ON det.frd_det_id = act.frd_id AND det.thread_id = act.thread_id AND act.action_type = ''C''  '
        END 

      IF ( @thread_id = 1000 ) -- JOIN details tbl with action tbl 
        BEGIN 
            SET @tbl_name = 'dbo.tbl_frd_details_member_qa det LEFT JOIN dbo.tbl_frd_actions act  ON det.frd_det_id = act.frd_id AND det.thread_id = act.thread_id AND det.action_type = act.actions  AND act.action_type = ''C''    '--  AND convert(datetime,act.action_date)  = '''+Convert(varchar(50),@action_date)+''''
        END 

    DECLARE @SelectStmt AS NVARCHAR(max) 
    DECLARE @SelectColumns AS NVARCHAR(max) 

    SELECT @SelectColumns = trx_log_cols 
    FROM   tbl_frd_threads 
    WHERE  thread_id = @thread_id 
           AND Isnull(trx_log_cols, '-1') != '-1' 
           AND trx_log_cols != '' 

    SET @SelectStmt = 'SELECT * FROM ( SELECT  Distinct' + isnull(@SelectColumns ,'-1')+ ' FROM ' 
                      + @tbl_name + ' WHERE   det.frd_id =''' 
                      + @frd_id_BU + '''    ' + @and_cond +') A'
                      + ' ORDER BY A.[Action Date] , 2,3,5,7'
    -- Here I don't know the number of returned columns BUT I need to sort the returned columns by all columns or by odded number 

    EXEC Sp_executesql  @SelectStmt 
END 

Clear ?

Thanks

1 Answers1

1

I still don't know, if I understand what you are asking or not. If you want to order your rows in the order of your columns, i.e. first by first column, then by second column, and so on, then haytem has given you the answer an hour ago in the comments section: Build the ORDER BY clause exactly the same way as you build your SELECT clause.

SET @SelectStmt = 'SELECT * FROM ( SELECT  Distinct' + isnull(@SelectColumns ,'-1')
                  + ' FROM ' + @tbl_name 
                  + ' WHERE   det.frd_id =''' + @frd_id_BU + '''    ' + @and_cond +') A'
                  + ' ORDER BY ' + isnull(@SelectColumns ,'1')

In case there are no columns in @SelectColumns, you select -1 and order by 1, i.e. the first and only column.

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
  • But the @SelectColumns contain aliases!! – Mohamed Nasr Apr 25 '16 at 14:27
  • Ah, okay, there are aliases. That was not to be expected, as the columns are retrieved from a table. Can you change the table so it contains column names and alias names separated? That would be the best option I guess. An alternative would be to generate a list of 1,2,3,4, ... according to the number of commas in @SelectColumns plus 1. I don't know if this is easily achievable in your DBMS, however. – Thorsten Kettner Apr 25 '16 at 14:35
  • your answer shows me you understand my question well..but as i said the @selectColumns contain aliases!! how can i solve this issue?? – Mohamed Nasr Apr 25 '16 at 14:37