Serverless SQL Pool: Dynamically Remove Spaces¶
Table of contents (click to expand)
Overview¶
Important
Serverless SQL pools do not support internal tables. They primarily support external and temporary tables. Use a dedicated SQL pool when you need internal tables with local storage.
| Table Type | Description | Use Cases |
|---|---|---|
| External Tables | - Reference data stored in external sources like Azure Data Lake Storage, Azure Blob Storage, Azure Cosmos DB, etc. - Data is not physically stored in the SQL pool. - Useful for querying large datasets without loading them into the SQL pool. |
- Querying large datasets stored externally. - Performing data analysis on data stored in various formats. |
| Temporary Tables | - Created and used within the scope of a session. - Data is stored temporarily and is dropped when the session ends. - Useful for intermediate data processing and transformations. |
- Storing intermediate results during complex queries. - Performing temporary data transformations and aggregations. |
Demo¶
Set Up a Synapse Workspace¶
- Sign in to the Azure Portal: Go to the Azure Portal and sign in with your Azure account.
-
Navigate to Your Synapse Workspace: In the Azure Portal, search for your Synapse workspace or create a new one if you don't have one.
-
Launch Synapse Studio: From the Synapse workspace overview, click on the
Open Synapse Studiobutton.
Upload Sample Data to Storage Account¶
Important
This demo uses the storage account created with the Synapse workspace. CREATE EXTERNAL DATA SOURCE is not supported in the serverless SQL pool's master database, so create a user database first and perform the remaining operations there.
- Create a Container in the Storage Account:
- Go to the Azure portal and navigate to your storage account.
- In the left-hand menu, select
Containers. - Click on
+ Containerto create a new container. - Enter a name for the container, such as
sample-tables-container. - Set the
Public access levelto your preference (e.g., Private). -
Click
Create. -
Navigate to the Container:
- In the Azure portal, go to your storage account.
- Select
Containersfrom the left-hand menu. - Click on the container you created (e.g.,
sample-tables-container). - Upload the Sample CSV File:
- Click on the
Uploadbutton. - In the upload blade, click on
Browseto select the sample CSV file from your local machine. -
Choose the file, click
Uploadto upload the file to the container.
Create User Database¶
-
First, create a new database in your Synapse workspace.
CREATE DATABASE {User Database Name}; -
Switch to the User Database: Use the newly created database.
Create an External Data Source and File Format¶
-
Integrate this task with the previous step by establishing the external data source within the user database.
USE {User Database Name}; CREATE EXTERNAL DATA SOURCE {Data Source Name} WITH ( LOCATION = 'https://<your-storage-account>.dfs.core.windows.net/<your-container>/' ); -
Create an External File Format: As part of the same flow, define the format of the CSV file.
CREATE EXTERNAL FILE FORMAT {File Format Name} WITH ( FORMAT_TYPE = DELIMITEDTEXT, FORMAT_OPTIONS ( FIELD_TERMINATOR = ',', STRING_DELIMITER = '"', FIRST_ROW = 2 ) );
Create an External Table¶
-
Create an external table that references the sample data.
CREATE EXTERNAL TABLE [Table Name] ( [Employee ID] INT, [Employee Name] NVARCHAR(50), [Hire Date] DATE ) WITH ( LOCATION = 'employee data with spaces.csv', DATA_SOURCE = {Data Source Name}, FILE_FORMAT = {File Format Name} ); CREATE EXTERNAL TABLE [Table Name] ( [Product ID] INT, [Product Name] NVARCHAR(50), [Release Date] DATE ) WITH ( LOCATION = 'product data with spaces.csv', DATA_SOURCE = {Data Source Name}, FILE_FORMAT = {File Format Name} ); -
Confirm the existence of the tables
SELECT * FROM sys.tables WHERE name IN ('Product Data', 'Employee Data'); -
Query the External Table: You can now query the external table to see the sample data.
SELECT * FROM {Table Name};
Create Views with Modified Tables/Column Names¶
This script is designed to dynamically create views for each table in a database, renaming columns to remove spaces. It starts by creating a temporary table to store the SQL statements and assigns a unique row number to each statement. The script then loops through these statements, executing each one in turn. Finally, it cleans up by dropping the temporary table.
- Temporary Table Creation: A temporary table
#CreateViewStatementsis created to store the dynamic SQL statements and their corresponding row numbers.- Inserting SQL Statements: The script generates SQL statements to create views for each table in the database. It uses the
INFORMATION_SCHEMA.COLUMNSto get the table and column names, renaming columns to remove spaces. These statements, along with a row number, are inserted into the temporary table.- Variable Declaration: Variables are declared to hold the current SQL statement, the current row number, and the maximum row number.
- Getting Maximum Row Number: The script retrieves the maximum row number from the temporary table to determine how many statements need to be executed.
- Executing SQL Statements: A loop iterates through each row in the temporary table, retrieves the SQL statement, executes it, and increments the row number until all statements are executed.
- Cleanup: The temporary table is dropped to clean up after the script has finished executing.
-- Create a temporary table to store the dynamic SQL statements
CREATE TABLE #CreateViewStatements (SQLStatement NVARCHAR(MAX), RowNum INT);
-- Insert dynamic SQL statements for each table with a row number
INSERT INTO #CreateViewStatements (SQLStatement, RowNum)
SELECT
'CREATE VIEW ' + QUOTENAME(REPLACE(TABLE_NAME, ' ', '_')) + ' AS SELECT ' +
STRING_AGG('[' + COLUMN_NAME + '] AS [' + REPLACE(COLUMN_NAME, ' ', '') + ']', ', ') +
' FROM ' + QUOTENAME(TABLE_NAME),
ROW_NUMBER() OVER (ORDER BY TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME;
-- Declare variables to hold the SQL statement and row number
DECLARE @sql NVARCHAR(MAX);
DECLARE @rowNum INT = 1;
DECLARE @maxRowNum INT;
-- Get the maximum row number
SELECT @maxRowNum = MAX(RowNum) FROM #CreateViewStatements;
-- Loop through the temporary table and execute each SQL statement
WHILE @rowNum <= @maxRowNum
BEGIN
-- Get the next SQL statement
SELECT @sql = SQLStatement FROM #CreateViewStatements WHERE RowNum = @rowNum;
-- Execute the SQL statement
EXEC sp_executesql @sql;
-- Increment the row number
SET @rowNum = @rowNum + 1;
END;
-- Drop the temporary table
DROP TABLE #CreateViewStatements;