Skip to content

Commit a30bca4

Browse files
authored
complete e2e demo
1 parent cfe5564 commit a30bca4

1 file changed

Lines changed: 93 additions & 33 deletions

File tree

  • 0_Azure/2_AzureAnalytics/1_SynapseAnalytics/demos/2_synapse_views_dynamically_remove_space/0_serverlessSQLPool

0_Azure/2_AzureAnalytics/1_SynapseAnalytics/demos/2_synapse_views_dynamically_remove_space/0_serverlessSQLPool/README.md

Lines changed: 93 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ Last updated: 2025-01-26
1010

1111
----------
1212

13-
## Wiki
14-
15-
<details>
16-
<summary><b>List of References </b> (Click to expand)</summary>
17-
18-
19-
</details>
20-
2113
## Content
2214

2315
<details>
2416
<summary><b>Table of Content </b> (Click to expand)</summary>
2517

18+
- [Content](#content)
19+
- [Overview](#overview)
20+
- [Demo](#demo)
21+
- [Set Up a Synapse Workspace](#set-up-a-synapse-workspace)
22+
- [Upload Sample Data to Storage Account](#upload-sample-data-to-storage-account)
23+
- [Create User Database](#create-user-database)
24+
- [Create an External Data Source and File Format](#create-an-external-data-source-and-file-format)
25+
- [Create an External Table](#create-an-external-table)
26+
- [Create Views with Modified Tables/Column Names](#create-views-with-modified-tablescolumn-names)
2627

2728
</details>
2829

@@ -95,21 +96,24 @@ Last updated: 2025-01-26
9596
<img width="884" alt="image" src="https://github.com/user-attachments/assets/f24a4d45-6bee-46ea-bccc-b5f893044c01" />
9697

9798
2. Switch to the User Database: Use the newly created database.
98-
3. Create an External Data Source: Integrate this task with the previous step by establishing the external data source within the user database.
99+
100+
### Create an External Data Source and File Format
101+
102+
1. Integrate this task with the previous step by establishing the external data source within the user database.
99103

100104
```sql
101105
USE {User Database Name};
102106
103107
CREATE EXTERNAL DATA SOURCE {Data Source Name}
104108
WITH (
105-
LOCATION = 'https://<your-storage-account>.dfs.core.windows.net/<your-container>'
109+
LOCATION = 'https://<your-storage-account>.dfs.core.windows.net/<your-container>/'
106110
);
107111
```
108112

109113
<img width="550" alt="image" src="https://github.com/user-attachments/assets/13f22e3b-7c68-4ed2-8a56-2752a5033869" />
110114

111115

112-
4. **Create an External File Format**: As part of the same flow, define the format of the CSV file.
116+
2. **Create an External File Format**: As part of the same flow, define the format of the CSV file.
113117

114118
```sql
115119
CREATE EXTERNAL FILE FORMAT {File Format Name}
@@ -126,51 +130,107 @@ Last updated: 2025-01-26
126130
<img width="550" alt="image" src="https://github.com/user-attachments/assets/40a4c6a2-bc51-49ba-9361-bec6eb0edb9e" />
127131

128132

129-
5. **Create an External Table**: Create an external table that references the sample data.
133+
### Create an External Table
134+
135+
1. Create an external table that references the sample data.
130136

131137
```sql
132-
CREATE EXTERNAL TABLE [Table With Spaces] (
133-
[ID] INT,
134-
[Name With Spaces] NVARCHAR(50),
135-
[Date With Spaces] DATE
138+
CREATE EXTERNAL TABLE [Table Name] (
139+
[Employee ID] INT,
140+
[Employee Name] NVARCHAR(50),
141+
[Hire Date] DATE
142+
)
143+
WITH (
144+
LOCATION = 'employee data with spaces.csv',
145+
DATA_SOURCE = {Data Source Name},
146+
FILE_FORMAT = {File Format Name}
147+
);
148+
149+
CREATE EXTERNAL TABLE [Table Name] (
150+
[Product ID] INT,
151+
[Product Name] NVARCHAR(50),
152+
[Release Date] DATE
136153
)
137154
WITH (
138-
LOCATION = '<file-name>.csv',
155+
LOCATION = 'product data with spaces.csv',
139156
DATA_SOURCE = {Data Source Name},
140157
FILE_FORMAT = {File Format Name}
141158
);
142159
```
160+
161+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/841cc8b7-4c2d-4c9a-975c-fec4bb3326d8" />
162+
163+
2. Confirm the existence of the tables
164+
165+
```sql
166+
SELECT * FROM sys.tables
167+
WHERE name IN ('Product Data', 'Employee Data');
168+
```
143169

170+
<img width="240" alt="image" src="https://github.com/user-attachments/assets/9eb9e379-30a7-42b8-966d-8f8c96b31395" />
144171

145172

146-
6. **Query the External Table**: You can now query the external table to see the sample data.
173+
3. **Query the External Table**: You can now query the external table to see the sample data.
174+
147175
```sql
148-
SELECT * FROM [Table With Spaces];
176+
SELECT * FROM {Table Name};
149177
```
150178

179+
### Create Views with Modified Tables/Column Names
151180

181+
> 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.
182+
> 1. **Temporary Table Creation**: A temporary table `#CreateViewStatements` is created to store the dynamic SQL statements and their corresponding row numbers. <br/>
183+
> 2. **Inserting SQL Statements**: The script generates SQL statements to create views for each table in the database. It uses the `INFORMATION_SCHEMA.COLUMNS` to get the table and column names, renaming columns to remove spaces. These statements, along with a row number, are inserted into the temporary table. <br/>
184+
> 3. **Variable Declaration**: Variables are declared to hold the current SQL statement, the current row number, and the maximum row number. <br/>
185+
> 4. **Getting Maximum Row Number**: The script retrieves the maximum row number from the temporary table to determine how many statements need to be executed. <br/>
186+
> 5. **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. <br/>
187+
> 6. **Cleanup**: The temporary table is dropped to clean up after the script has finished executing. <br/>
152188

189+
```sql
190+
-- Create a temporary table to store the dynamic SQL statements
191+
CREATE TABLE #CreateViewStatements (SQLStatement NVARCHAR(MAX), RowNum INT);
153192
193+
-- Insert dynamic SQL statements for each table with a row number
194+
INSERT INTO #CreateViewStatements (SQLStatement, RowNum)
195+
SELECT
196+
'CREATE VIEW ' + QUOTENAME(REPLACE(TABLE_NAME, ' ', '_')) + ' AS SELECT ' +
197+
STRING_AGG('[' + COLUMN_NAME + '] AS [' + REPLACE(COLUMN_NAME, ' ', '') + ']', ', ') +
198+
' FROM ' + QUOTENAME(TABLE_NAME),
199+
ROW_NUMBER() OVER (ORDER BY TABLE_NAME)
200+
FROM INFORMATION_SCHEMA.COLUMNS
201+
GROUP BY TABLE_NAME;
154202
155-
1. **Open the SQL Script Editor**:
156-
- In Synapse Studio, go to the `Develop hub` by clicking on the `Develop` icon in the left navigation pane.
157-
- Click on `+ New SQL script` to open the SQL script editor.
203+
-- Declare variables to hold the SQL statement and row number
204+
DECLARE @sql NVARCHAR(MAX);
205+
DECLARE @rowNum INT = 1;
206+
DECLARE @maxRowNum INT;
158207
159-
<img width="550" alt="image" src="https://github.com/user-attachments/assets/3382ea4a-06eb-4e32-93d9-569cef7fc2f5" />
208+
-- Get the maximum row number
209+
SELECT @maxRowNum = MAX(RowNum) FROM #CreateViewStatements;
160210
211+
-- Loop through the temporary table and execute each SQL statement
212+
WHILE @rowNum <= @maxRowNum
213+
BEGIN
214+
-- Get the next SQL statement
215+
SELECT @sql = SQLStatement FROM #CreateViewStatements WHERE RowNum = @rowNum;
161216
162-
2. Create an External Data Source: Define an external data source that points to your SQL database.
217+
-- Execute the SQL statement
218+
EXEC sp_executesql @sql;
163219
164-
```sql
165-
CREATE EXTERNAL DATA SOURCE MySQLDataSource
166-
WITH (
167-
TYPE = RDBMS,
168-
LOCATION = '<your-sql-server-name>.database.windows.net',
169-
DATABASE_NAME = '<your-database-name>',
170-
CREDENTIAL = MyCredential
171-
);
172-
```
220+
-- Increment the row number
221+
SET @rowNum = @rowNum + 1;
222+
END;
223+
224+
-- Drop the temporary table
225+
DROP TABLE #CreateViewStatements;
226+
```
227+
228+
229+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/2654dcb8-d843-4000-9b32-b36f14f2a7a8" />
230+
231+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/6f270523-adf1-4ca3-b80c-b5decd624459" />
173232

233+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/7a5a26ff-9a5f-4c98-a337-b64ce0cc1699" />
174234

175235
<div align="center">
176236
<h3 style="color: #4CAF50;">Total Visitors</h3>

0 commit comments

Comments
 (0)