create external table with specific field order

rajanisqldev-42 221 Reputation points
2023-04-03T11:48:01.67+00:00

Hi,

I have a csv file in datalake. I have created

  1. database scoped credential
  2. external data source
  3. external file format

The csv file has fields in the order

  1. Segment
  2. Product
  3. Qty
  4. Sales
  5. Month
  6. Year

my Create external table DML is as below

CREATE EXTERNAL TABLE testfile

(

Segment varchar(20),

Product varchar(20),

Sales varchar(20)

)

WITH

(

Location = "xxxxxx",

data_source=demods,

FILE_FORMAT=csvfiles

)

When I query the table, I am getting strange results

SELECT * FROM testfile

Result:

Segment Product Sales


SEG12 PRD123 154 -- Which is actually Qty not Sales

Is there field order is mandatory?

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,375 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. ShaikMaheer-MSFT 38,546 Reputation points Microsoft Employee Moderator
    2023-04-05T04:18:53.61+00:00

    Hi rajanisqldev-42, Thank you for posting query in Microsoft Q&A Platform.

    Yes, order should be same. But you can consider using CETAS also in your case.

    
    -- use CETAS to export select statement with OPENROWSET result to  storage
    CREATE EXTERNAL TABLE population_by_year_state
    WITH (
        LOCATION = 'aggregated_data/',
        DATA_SOURCE = population_ds,  
        FILE_FORMAT = census_file_format
    )  
    AS
    SELECT decennialTime, stateName, SUM(population) AS population
    FROM
        OPENROWSET(BULK 'https://azureopendatastorage.dfs.core.windows.net/censusdatacontainer/release/us_population_county/year=*/*.parquet',
        FORMAT='PARQUET') AS [r]
    GROUP BY decennialTime, stateName
    GO
    
    -- you can query the newly created external table
    SELECT * FROM population_by_year_state
    
    

    Please consider hitting Accept Answer button. Accepted Answers help community as well.

    1 person found this answer helpful.

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.