Skip to content

Commit aa64f2d

Browse files
authored
format
1 parent 0b98e04 commit aa64f2d

1 file changed

Lines changed: 92 additions & 15 deletions

File tree

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

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

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ Last updated: 2025-01-24
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+
- [Demo](#demo)
20+
- [Set Up a Dedicated SQL Pool](#set-up-a-dedicated-sql-pool)
21+
- [Create a Dedicated SQL Pool](#create-a-dedicated-sql-pool)
22+
- [Create Tables with Spaces in Names and Columns](#create-tables-with-spaces-in-names-and-columns)
23+
- [Create Views with Modified Tables/Column Names](#create-views-with-modified-tablescolumn-names)
2624

2725
</details>
2826

@@ -130,7 +128,88 @@ Last updated: 2025-01-24
130128
1. **Create a Stored Procedure to Remove Spaces from Column Names**: Use the following script to create a stored procedure that removes spaces from column names and creates views. Click [here to see the .sql file]().
131129

132130
```sql
133-
131+
CREATE PROCEDURE RemoveSpacesFromColumnNames
132+
AS
133+
BEGIN
134+
DECLARE @tableName NVARCHAR(255)
135+
DECLARE @columnName NVARCHAR(255)
136+
DECLARE @sql NVARCHAR(MAX)
137+
138+
-- Temporary table to store table names
139+
CREATE TABLE #TableNames (TABLE_NAME NVARCHAR(255))
140+
INSERT INTO #TableNames
141+
SELECT TABLE_NAME
142+
FROM INFORMATION_SCHEMA.TABLES
143+
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo'
144+
145+
-- Loop through each table
146+
WHILE EXISTS (SELECT 1 FROM #TableNames)
147+
BEGIN
148+
SELECT TOP 1 @tableName = TABLE_NAME FROM #TableNames
149+
150+
-- Print the table name for debugging
151+
PRINT 'Processing table: ' + @tableName
152+
153+
SET @sql = 'CREATE VIEW dbo.vw' + REPLACE(@tableName, ' ', '') + ' AS SELECT '
154+
155+
-- Drop the temporary table if it exists
156+
IF OBJECT_ID('tempdb..#ColumnNames') IS NOT NULL
157+
DROP TABLE #ColumnNames
158+
159+
-- Temporary table to store column names
160+
CREATE TABLE #ColumnNames (COLUMN_NAME NVARCHAR(255))
161+
INSERT INTO #ColumnNames
162+
SELECT COLUMN_NAME
163+
FROM INFORMATION_SCHEMA.COLUMNS
164+
WHERE TABLE_NAME = @tableName
165+
166+
-- Loop through each column
167+
WHILE EXISTS (SELECT 1 FROM #ColumnNames)
168+
BEGIN
169+
SELECT TOP 1 @columnName = COLUMN_NAME FROM #ColumnNames
170+
171+
-- Print the column name for debugging
172+
PRINT 'Processing column: ' + @columnName
173+
174+
-- Remove all spaces from column names
175+
IF (SELECT COUNT(*) FROM #ColumnNames) = 1
176+
BEGIN
177+
SET @sql = @sql + 'REPLACE([' + @columnName + '], '' '', '''') AS [' + REPLACE(@columnName, ' ', '') + '] '
178+
END
179+
ELSE
180+
BEGIN
181+
SET @sql = @sql + 'REPLACE([' + @columnName + '], '' '', '''') AS [' + REPLACE(@columnName, ' ', '') + '], '
182+
END
183+
184+
DELETE FROM #ColumnNames WHERE COLUMN_NAME = @columnName
185+
END
186+
187+
-- Remove the trailing comma and space if any
188+
IF RIGHT(@sql, 2) = ', '
189+
BEGIN
190+
SET @sql = LEFT(@sql, LEN(@sql) - 2)
191+
END
192+
193+
SET @sql = @sql + ' FROM [' + @tableName + '];'
194+
195+
-- Print the dynamic SQL for debugging
196+
PRINT 'Generated SQL: ' + @sql
197+
198+
-- Execute the dynamic SQL
199+
BEGIN TRY
200+
EXEC sp_executesql @sql
201+
END TRY
202+
BEGIN CATCH
203+
PRINT 'Error: ' + ERROR_MESSAGE()
204+
END CATCH
205+
206+
DELETE FROM #TableNames WHERE TABLE_NAME = @tableName
207+
END
208+
209+
-- Clean up temporary tables
210+
DROP TABLE #TableNames
211+
DROP TABLE #ColumnNames
212+
END
134213
```
135214

136215
<img width="550" alt="image" src="https://github.com/user-attachments/assets/13b3b5f2-3142-448f-b03a-3eeae00a1509" />
@@ -157,16 +236,14 @@ Last updated: 2025-01-24
157236

158237
<img width="550" alt="image" src="https://github.com/user-attachments/assets/1ccf430e-1dff-4968-980c-a4f3913a8369" />
159238

160-
> [!NOTE]
161-
> Once you refresh, the views will be visible:
162-
163239
| Before | After |
164240
| --- | --- |
165-
<img width="360" alt="image" src="https://github.com/user-attachments/assets/b0de5118-bf67-4f75-9dd7-2ae5ba33cb28" />
166-
167-
241+
| <img width="360" alt="image" src="https://github.com/user-attachments/assets/b0de5118-bf67-4f75-9dd7-2ae5ba33cb28" /> | <img width="360" alt="image" src="https://github.com/user-attachments/assets/ef9c6d3a-1d45-4d37-9977-431cf8b774d0" /> |
168242

243+
> [!NOTE]
244+
> Once you refresh, the views will be visible:
169245

246+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/e0191419-29cc-448a-a14f-a76da221b015" />
170247

171248

172249
<div align="center">

0 commit comments

Comments
 (0)