Skip to content

Commit 5d18721

Browse files
authored
src samples
1 parent f3d7473 commit 5d18721

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
-- Create sample tables with spaces in names
2+
CREATE TABLE [Employee Records] (
3+
[Employee ID] INT,
4+
[Employee Name] NVARCHAR(255),
5+
[Employee Address] NVARCHAR(255)
6+
);
7+
8+
CREATE TABLE [Sales Data] (
9+
[Sale ID] INT,
10+
[Sale Date] DATE,
11+
[Employee ID] INT,
12+
[Sale Amount] DECIMAL(10, 2)
13+
);
14+
15+
CREATE TABLE [Inventory Details] (
16+
[Item ID] INT,
17+
[Item Name] NVARCHAR(255),
18+
[Item Category] NVARCHAR(255),
19+
[Item Price] DECIMAL(10, 2)
20+
);
21+
22+
-- Insert sample data into the tables
23+
INSERT INTO [Employee Records] ([Employee ID], [Employee Name], [Employee Address])
24+
VALUES (1, 'Alice Johnson', '789 Pine St');
25+
26+
INSERT INTO [Employee Records] ([Employee ID], [Employee Name], [Employee Address])
27+
VALUES (2, 'Bob Brown', '101 Maple St');
28+
29+
INSERT INTO [Sales Data] ([Sale ID], [Sale Date], [Employee ID], [Sale Amount])
30+
VALUES (1, '2023-02-01', 1, 200.00);
31+
32+
INSERT INTO [Sales Data] ([Sale ID], [Sale Date], [Employee ID], [Sale Amount])
33+
VALUES (2, '2023-02-02', 2, 250.00);
34+
35+
INSERT INTO [Inventory Details] ([Item ID], [Item Name], [Item Category], [Item Price])
36+
VALUES (1, 'Gadget', 'Electronics', 49.99);
37+
38+
INSERT INTO [Inventory Details] ([Item ID], [Item Name], [Item Category], [Item Price])
39+
VALUES (2, 'Tool', 'Hardware', 29.99);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXEC RemoveSpacesFromColumnNames
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
CREATE PROCEDURE RemoveSpacesFromColumnNames
2+
AS
3+
BEGIN
4+
DECLARE @tableName NVARCHAR(255)
5+
DECLARE @columnName NVARCHAR(255)
6+
DECLARE @sql NVARCHAR(MAX)
7+
8+
-- Temporary table to store table names
9+
CREATE TABLE #TableNames (TABLE_NAME NVARCHAR(255))
10+
INSERT INTO #TableNames
11+
SELECT TABLE_NAME
12+
FROM INFORMATION_SCHEMA.TABLES
13+
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo'
14+
15+
-- Loop through each table
16+
WHILE EXISTS (SELECT 1 FROM #TableNames)
17+
BEGIN
18+
SELECT TOP 1 @tableName = TABLE_NAME FROM #TableNames
19+
20+
-- Print the table name for debugging
21+
PRINT 'Processing table: ' + @tableName
22+
23+
SET @sql = 'CREATE VIEW dbo.vw' + REPLACE(@tableName, ' ', '') + ' AS SELECT '
24+
25+
-- Drop the temporary table if it exists
26+
IF OBJECT_ID('tempdb..#ColumnNames') IS NOT NULL
27+
DROP TABLE #ColumnNames
28+
29+
-- Temporary table to store column names
30+
CREATE TABLE #ColumnNames (COLUMN_NAME NVARCHAR(255))
31+
INSERT INTO #ColumnNames
32+
SELECT COLUMN_NAME
33+
FROM INFORMATION_SCHEMA.COLUMNS
34+
WHERE TABLE_NAME = @tableName
35+
36+
-- Loop through each column
37+
WHILE EXISTS (SELECT 1 FROM #ColumnNames)
38+
BEGIN
39+
SELECT TOP 1 @columnName = COLUMN_NAME FROM #ColumnNames
40+
41+
-- Print the column name for debugging
42+
PRINT 'Processing column: ' + @columnName
43+
44+
-- Remove all spaces from column names
45+
IF (SELECT COUNT(*) FROM #ColumnNames) = 1
46+
BEGIN
47+
SET @sql = @sql + 'REPLACE([' + @columnName + '], '' '', '''') AS [' + REPLACE(@columnName, ' ', '') + '] '
48+
END
49+
ELSE
50+
BEGIN
51+
SET @sql = @sql + 'REPLACE([' + @columnName + '], '' '', '''') AS [' + REPLACE(@columnName, ' ', '') + '], '
52+
END
53+
54+
DELETE FROM #ColumnNames WHERE COLUMN_NAME = @columnName
55+
END
56+
57+
-- Remove the trailing comma and space if any
58+
IF RIGHT(@sql, 2) = ', '
59+
BEGIN
60+
SET @sql = LEFT(@sql, LEN(@sql) - 2)
61+
END
62+
63+
SET @sql = @sql + ' FROM [' + @tableName + '];'
64+
65+
-- Print the dynamic SQL for debugging
66+
PRINT 'Generated SQL: ' + @sql
67+
68+
-- Execute the dynamic SQL
69+
BEGIN TRY
70+
EXEC sp_executesql @sql
71+
END TRY
72+
BEGIN CATCH
73+
PRINT 'Error: ' + ERROR_MESSAGE()
74+
END CATCH
75+
76+
DELETE FROM #TableNames WHERE TABLE_NAME = @tableName
77+
END
78+
79+
-- Clean up temporary tables
80+
DROP TABLE #TableNames
81+
DROP TABLE #ColumnNames
82+
END

0 commit comments

Comments
 (0)