Columns ordered differently from one environment to another

Chalut, Mylène - ITSLM/GCSTI 21 Reputation points
2022-12-13T15:13:19.84+00:00

Hello guys !

I wrote the following script to get the column names from a SQL table:

 DECLARE @SchemaName VARCHAR(20);  
 DECLARE @TableName VARCHAR(200);  
 DECLARE @Alias VARCHAR(20);   
 SET @SchemaName = 'dbo';  
 SET @TableName = 'tblSUFGenericAM202209';  
 SET @Alias = NULL;  
  
DECLARE @ColumnList VARCHAR(max);  
  
    IF EXISTS  
    (  
        SELECT 1  
        FROM information_schema.tables t  
        WHERE t.table_schema = @SchemaName  
              AND t.table_name = @TableName  
    )  
        BEGIN  
	SELECT @ColumnList = ISNULL(@ColumnList, ' ') + ',' +   
	   (CASE WHEN @Alias IS NULL THEN QUOTENAME(c.column_name)  
	    ELSE QUOTENAME(@Alias) + '.' + QUOTENAME(c.COLUMN_NAME) END)  
	    FROM Information_schema.columns c  
	    WHERE c.table_name = @TableName  
		   AND c.table_schema = @SchemaName  
		  
	    SELECT @ColumnList = SUBSTRING(LTRIM(@ColumnList),2,LEN(@columnlist)-1)  
  
        END;  
	ELSE  
	BEGIN  
	    SET @ColumnList = NULL;  
	END  
  
	PRINT @ColumnList;  

It does what it's suppose to. However, I just realized that the order of the columns is not the same when I run the script on our Dev and Test environments.

DEV :
[DivisionAcronymEnglish],[DivisionAcronymFrench],[OperatingEntityNumber],[MeasureEnglish],[MeasureFrench],[SizeRaw],[SizeConverted],[UnitOfMeasureEnglish],[UnitOfMeasureFrench],[DefaultUnitOfMeasureEnglish],[DefaultUnitOfMeasureFrench],[VintageDate],[ActivationDate],[CollectEnglish],[CollectFrench],[LastUpdateDate],[Userid],[AdditionalMeasuresCharacteristicsId]

TEST :
[ActivationDate],[AdditionalMeasuresCharacteristicsId],[CollectEnglish],[CollectFrench],[DefaultUnitOfMeasureEnglish],[DefaultUnitOfMeasureFrench],[DivisionAcronymEnglish],[DivisionAcronymFrench],[LastUpdateDate],[MeasureEnglish],[MeasureFrench],[OperatingEntityNumber],[SizeConverted],[SizeRaw],[UnitOfMeasureEnglish],[UnitOfMeasureFrench],[Userid],[VintageDate]

I was expecting the column order would be the same as the input table (DEV). Can somebody tell me why the result is not the same and how I can fix this ? Some of the tables that I need column names from contains tones of them !!

Thanks in advance for your help ;-)

Mylene

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-12-13T15:36:59.28+00:00

    When you need to have an order list from SELECT, you need to use ORDER BY clause,
    If your table is the same for all environments, you can add this line with column ORDINAL_POSITION in the following section:

    SELECT @ColumnList = ISNULL(@ColumnList, ' ') + ',' +   
        (CASE WHEN @Alias IS NULL THEN QUOTENAME(c.column_name)  
         ELSE QUOTENAME(@Alias) + '.' + QUOTENAME(c.COLUMN_NAME) END)  
         FROM Information_schema.columns c  
         WHERE c.table_name = @TableName  
            AND c.table_schema = @SchemaName  
    

    Order by ORDINAL_POSITION


2 additional answers

Sort by: Most helpful
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-12-13T16:30:02.187+00:00

    If you need an order list in a SELECT query, you must have an ORDER BY clause to guarantee your resultset ( one row result is an exception but does not hurt) .

    0 comments No comments

  2. Chalut, Mylène - ITSLM/GCSTI 21 Reputation points
    2022-12-13T18:22:06.75+00:00

    As simple as that !! Thank you guys !

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.