Skip to content

Commit 1d0c132

Browse files
tduguidtduguid
authored andcommitted
Fixed syntax of PL/SQL merge and union scripts and updated the comment header format
1 parent e56fb6a commit 1d0c132

File tree

4 files changed

+89
-36
lines changed

4 files changed

+89
-36
lines changed

CS/Scripts/Formula.cs

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,18 +1319,15 @@ public static void PlSqlMergeValues()
13191319
{
13201320
ErrorHandler.CreateLogRecord();
13211321
if (ErrorHandler.IsValidListObject(true) == false) { return; };
1322-
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
13231322
string lastColumnName = Properties.Settings.Default.Table_ColumnTableAlias;
1323+
string sqlColName = string.Empty;
13241324
string tableAlias = Properties.Settings.Default.Table_ColumnTableAlias;
13251325
string tableAliasTemp = tableAlias + "_source";
1326-
string sqlColName = string.Empty;
1327-
1328-
sqlColName = "SELECT " + tableAliasTemp + ".*" + " FROM (VALUES";
13291326

1327+
sqlColName = Properties.Settings.Default.Table_ColumnName;
13301328
tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
13311329
int lastColumnIndex = tbl.Range.Columns.Count;
13321330
sqlCol = tbl.ListColumns[lastColumnIndex];
1333-
13341331
if (sqlCol.Name == sqlColName)
13351332
{
13361333
lastColumnName = sqlCol.Name;
@@ -1342,7 +1339,6 @@ public static void PlSqlMergeValues()
13421339
lastColumnIndex = tbl.Range.Columns.Count;
13431340
}
13441341

1345-
// Columns formatted as text will not work as formulas and the added column will copy the formatting from the previous column so ensure that the added column never has Text format...
13461342
sqlCol.DataBodyRange.NumberFormat = "General";
13471343
string formula = string.Empty;
13481344
string qt = string.Empty;
@@ -1355,7 +1351,6 @@ public static void PlSqlMergeValues()
13551351
return;
13561352
}
13571353
if (col.Name == lastColumnName | col.Range.EntireColumn.Hidden)
1358-
//if (col.Name != lastColumnName | col.Range.EntireColumn.Hidden == false)
13591354
{
13601355
//DO NOTHING - because the column is hidden or the last column with the sql script
13611356
}
@@ -1370,28 +1365,51 @@ public static void PlSqlMergeValues()
13701365
colRef = colRef.Replace("'", "''");
13711366
colRef = colRef.Replace("#", "'#");
13721367
colRef = "SUBSTITUTE(" + colRef + ", " + "\"" + qt + "\", \"" + qt + qt + "\")";
1373-
formula += "\"" + qt + "\" & " + colRef + " & \"" + qt + "\"";
1368+
string dqt = "\"\"";
1369+
string valuePlSuffix = "& \" AS " + dqt + col.Name + dqt + "\"";
1370+
formula += "\"" + qt + "\" & " + colRef + " & \"" + qt + "\"" + valuePlSuffix;
13741371
}
13751372
}
13761373
string nullValue = Properties.Settings.Default.Table_ColumnScriptNull;
13771374
formula = "SUBSTITUTE(" + formula + ", \"'" + nullValue + "'\", \"" + nullValue + "\")";
13781375
int firstRowNbr = tbl.Range[1, 1].Row + 1; // must use the offset for the first row number
1379-
formula = "=IF(" + (firstRowNbr).ToString() + "-ROW() = 0, \" \", \",\") & " + "\" ( \" & " + formula + " & \")\"";
1376+
formula = "=IF(" + (firstRowNbr).ToString() + "-ROW() = 0, \"\", \"UNION \") & " + "\"SELECT \" & " + formula + " & \" FROM DUAL\"";
1377+
tbl.ShowTotals = false;
13801378
lastColumnName = sqlColName; // maximum header characters are 255
13811379
tbl.HeaderRowRange[lastColumnIndex].Value2 = lastColumnName;
1382-
tbl.ShowTotals = true;
1383-
string totalsColumnValue = ") " + tableAliasTemp + " (" + Ribbon.ConcatenateColumnNames(tbl.Range, "", "[", "]") + ") ";
1384-
tbl.TotalsRowRange[lastColumnIndex].Value2 = totalsColumnValue; // totals row has a maximum limit of 32,767 characters
13851380
try
13861381
{
13871382
sqlCol.DataBodyRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible).Rows.Formula = formula;
13881383
sqlCol.Range.Columns.AutoFit();
13891384
sqlCol.Range.HorizontalAlignment = Excel.Constants.xlLeft;
1390-
sqlCol.Range.Copy();
1385+
sqlCol.DataBodyRange.Copy();
13911386
Ribbon.AppVariables.FileType = "SQL";
1392-
Ribbon.AppVariables.ScriptRange = (string)Clipboard.GetData(DataFormats.Text);
1393-
Ribbon.AppVariables.ScriptRange = Ribbon.AppVariables.ScriptRange.Replace(@"""", String.Empty);
1394-
Ribbon.AppVariables.ScriptRange = "SET XACT_ABORT ON" + Environment.NewLine + "BEGIN TRANSACTION;" + Environment.NewLine + Environment.NewLine + ";WITH " + Environment.NewLine + tableAliasTemp + Environment.NewLine + "AS " + Environment.NewLine + "(" + Environment.NewLine + Ribbon.AppVariables.ScriptRange + ") " + Environment.NewLine + "MERGE " + tableAlias + " AS T" + Environment.NewLine + "USING " + tableAliasTemp + " AS S" + Environment.NewLine + "ON " + Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "WHEN NOT MATCHED BY TARGET" + Environment.NewLine + "THEN INSERT" + Environment.NewLine + "(" + Environment.NewLine + Ribbon.ConcatenateColumnNames(tbl.Range, "", "[", "]") + Environment.NewLine + ")" + Environment.NewLine + "VALUES" + Environment.NewLine + "(" + Environment.NewLine + Ribbon.ConcatenateColumnNames(tbl.Range, "S", "[", "]") + Environment.NewLine + ")" + Environment.NewLine + "WHEN MATCHED" + Environment.NewLine + "THEN UPDATE SET" + Environment.NewLine + Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "--WHEN NOT MATCHED BY SOURCE AND 'ADD WHERE CLAUSE HERE'" + Environment.NewLine + "--THEN DELETE" + Environment.NewLine + "OUTPUT $action, inserted.*, deleted.*;" + Environment.NewLine + Environment.NewLine + "ROLLBACK TRANSACTION;" + Environment.NewLine + "--COMMIT TRANSACTION;" + Environment.NewLine + "GO";
1387+
Ribbon.AppVariables.ScriptRange = Ribbon.GetCommentHeader("To update, insert & delete rows");
1388+
Ribbon.AppVariables.ScriptRange += "BEGIN" + Environment.NewLine + Environment.NewLine;
1389+
Ribbon.AppVariables.ScriptRange += "WITH " + Environment.NewLine;
1390+
Ribbon.AppVariables.ScriptRange += tableAliasTemp + Environment.NewLine;
1391+
Ribbon.AppVariables.ScriptRange += "AS " + Environment.NewLine;
1392+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
1393+
Ribbon.AppVariables.ScriptRange += (string)Clipboard.GetData(DataFormats.Text);
1394+
Ribbon.AppVariables.ScriptRange += ") " + Environment.NewLine;
1395+
Ribbon.AppVariables.ScriptRange += "MERGE INTO " + tableAlias + " AS T" + Environment.NewLine;
1396+
Ribbon.AppVariables.ScriptRange += "USING " + tableAliasTemp + " AS S" + Environment.NewLine;
1397+
Ribbon.AppVariables.ScriptRange += "ON --< Update the join to the correct unique key for the table" + Environment.NewLine;
1398+
Ribbon.AppVariables.ScriptRange += Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "WHEN NOT MATCHED" + Environment.NewLine;
1399+
Ribbon.AppVariables.ScriptRange += "THEN INSERT" + Environment.NewLine;
1400+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
1401+
Ribbon.AppVariables.ScriptRange += Ribbon.ConcatenateColumnNames(tbl.Range, "", "[", "]") + Environment.NewLine;
1402+
Ribbon.AppVariables.ScriptRange += ")" + Environment.NewLine;
1403+
Ribbon.AppVariables.ScriptRange += "VALUES" + Environment.NewLine;
1404+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
1405+
Ribbon.AppVariables.ScriptRange += Ribbon.ConcatenateColumnNames(tbl.Range, "S", "[", "]") + Environment.NewLine;
1406+
Ribbon.AppVariables.ScriptRange += ")" + Environment.NewLine;
1407+
Ribbon.AppVariables.ScriptRange += "WHEN MATCHED" + Environment.NewLine;
1408+
Ribbon.AppVariables.ScriptRange += "THEN UPDATE SET" + Environment.NewLine;
1409+
Ribbon.AppVariables.ScriptRange += Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "--WHEN NOT MATCHED THEN DELETE WHERE --< You may want to add a WHERE clause here" + Environment.NewLine + Environment.NewLine;
1410+
Ribbon.AppVariables.ScriptRange += "ROLLBACK;" + Environment.NewLine;
1411+
Ribbon.AppVariables.ScriptRange += "--COMMIT;" + Environment.NewLine + Environment.NewLine;
1412+
Ribbon.AppVariables.ScriptRange += "END;";
13951413
}
13961414
catch (System.Runtime.InteropServices.COMException)
13971415
{
@@ -1584,7 +1602,7 @@ public static void PlSqlSelectUnion()
15841602
colRef = colRef.Replace("#", "'#");
15851603
colRef = "SUBSTITUTE(" + colRef + ", " + "\"" + qt + "\", \"" + qt + qt + "\")";
15861604
string dqt = "\"\"";
1587-
string valuePlSuffix = "& \" AS " + dqt + col.Name + dqt + " \"";
1605+
string valuePlSuffix = "& \" AS " + dqt + col.Name + dqt + "\"";
15881606
formula += "\"" + qt + "\" & " + colRef + " & \"" + qt + "\"" + valuePlSuffix;
15891607
}
15901608
}
@@ -1602,8 +1620,9 @@ public static void PlSqlSelectUnion()
16021620
sqlCol.Range.HorizontalAlignment = Excel.Constants.xlLeft;
16031621
sqlCol.DataBodyRange.Copy();
16041622
Ribbon.AppVariables.FileType = "SQL";
1605-
Ribbon.AppVariables.ScriptRange = (string)Clipboard.GetData(DataFormats.Text);
1606-
Ribbon.AppVariables.ScriptRange = Ribbon.AppVariables.ScriptRange.Replace(@"""", String.Empty);
1623+
Ribbon.AppVariables.ScriptRange = Ribbon.GetCommentHeader("To select values with a union operator");
1624+
Ribbon.AppVariables.ScriptRange += (string)Clipboard.GetData(DataFormats.Text);
1625+
16071626
}
16081627
catch (System.Runtime.InteropServices.COMException)
16091628
{
@@ -1842,21 +1861,28 @@ public static void TSqlCreateTable()
18421861
string nullValue = Properties.Settings.Default.Table_ColumnScriptNull;
18431862
formula = "SUBSTITUTE(" + formula + ", \"'" + nullValue + "'\", \"" + nullValue + "\")";
18441863
string tableAlias = Properties.Settings.Default.Table_ColumnTableAlias;
1845-
string insertPrefix = "INSERT INTO " + tableAlias + " (" + Ribbon.ConcatenateColumnNames(tbl.Range) + ") VALUES(";
1864+
string insertPrefix = "INSERT INTO " + tableAlias + " (" + Ribbon.ConcatenateColumnNames(tbl.Range, "", "[", "]") + ") VALUES(";
18461865
formula = "=\"" + insertPrefix + "\" & " + formula + " & \");\"";
18471866
tbl.ShowTotals = false;
18481867
lastColumnName = sqlColName; // maximum header characters are 255
18491868
tbl.HeaderRowRange[lastColumnIndex].Value2 = lastColumnName;
1850-
string createTable = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + tableAlias + "') AND type in (N'U'))" + Environment.NewLine + "DROP TABLE " + tableAlias + Environment.NewLine + "; " + Environment.NewLine + "CREATE TABLE " + tableAlias + " (" + tableAlias + "_ID [int] PRIMARY KEY IDENTITY(1,1) NOT NULL, " + Ribbon.ConcatenateColumnNames(tbl.Range, "", Environment.NewLine + "[", "] [varchar](max) NULL") + Environment.NewLine + ");" + Environment.NewLine;
18511869
try
18521870
{
18531871
sqlCol.DataBodyRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible).Rows.Formula = formula;
18541872
sqlCol.Range.Columns.AutoFit();
18551873
sqlCol.Range.HorizontalAlignment = Excel.Constants.xlLeft;
18561874
sqlCol.DataBodyRange.Copy();
18571875
Ribbon.AppVariables.FileType = "SQL";
1858-
Ribbon.AppVariables.ScriptRange = createTable + (string)Clipboard.GetData(DataFormats.Text);
1859-
Ribbon.AppVariables.ScriptRange = Ribbon.GetCommentHeader("To create and insert records") + Ribbon.AppVariables.ScriptRange.Replace(@"""", String.Empty);
1876+
Ribbon.AppVariables.ScriptRange = Ribbon.GetCommentHeader("To create and insert records");
1877+
Ribbon.AppVariables.ScriptRange += "IF EXISTS(SELECT * FROM [sys].[objects] WHERE [object_id]=OBJECT_ID(N'" + tableAlias + "') AND [TYPE]=N'U')" + Environment.NewLine;
1878+
Ribbon.AppVariables.ScriptRange += "DROP TABLE " + tableAlias + Environment.NewLine;
1879+
Ribbon.AppVariables.ScriptRange += "; " + Environment.NewLine + Environment.NewLine;
1880+
Ribbon.AppVariables.ScriptRange += "CREATE TABLE " + tableAlias + Environment.NewLine;
1881+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
1882+
Ribbon.AppVariables.ScriptRange += " [" + tableAlias + "_ID] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL" + Ribbon.ConcatenateColumnNames(tbl.Range, "", Environment.NewLine + ", [", "] [varchar](max) NULL", "") + Environment.NewLine;
1883+
Ribbon.AppVariables.ScriptRange += ");" + Environment.NewLine + Environment.NewLine;
1884+
Ribbon.AppVariables.ScriptRange += (string)Clipboard.GetData(DataFormats.Text);
1885+
Ribbon.AppVariables.ScriptRange = Ribbon.AppVariables.ScriptRange.Replace(@"""", String.Empty);
18601886
}
18611887
catch (System.Runtime.InteropServices.COMException)
18621888
{
@@ -2067,9 +2093,37 @@ public static void TSqlMergeValues()
20672093
sqlCol.Range.HorizontalAlignment = Excel.Constants.xlLeft;
20682094
sqlCol.Range.Copy();
20692095
Ribbon.AppVariables.FileType = "SQL";
2070-
Ribbon.AppVariables.ScriptRange = (string)Clipboard.GetData(DataFormats.Text);
2096+
Ribbon.AppVariables.ScriptRange = Ribbon.GetCommentHeader("To update, insert & delete rows");
2097+
Ribbon.AppVariables.ScriptRange += "SET XACT_ABORT ON" + Environment.NewLine;
2098+
Ribbon.AppVariables.ScriptRange += "BEGIN TRANSACTION;" + Environment.NewLine + Environment.NewLine;
2099+
Ribbon.AppVariables.ScriptRange += ";WITH " + Environment.NewLine;
2100+
Ribbon.AppVariables.ScriptRange += tableAliasTemp + Environment.NewLine;
2101+
Ribbon.AppVariables.ScriptRange += "AS " + Environment.NewLine;
2102+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
2103+
Ribbon.AppVariables.ScriptRange += (string)Clipboard.GetData(DataFormats.Text);
2104+
Ribbon.AppVariables.ScriptRange += ") " + Environment.NewLine;
2105+
Ribbon.AppVariables.ScriptRange += "MERGE " + tableAlias + " AS T" + Environment.NewLine;
2106+
Ribbon.AppVariables.ScriptRange += "USING " + tableAliasTemp + " AS S" + Environment.NewLine;
2107+
Ribbon.AppVariables.ScriptRange += "ON --< Update the join to the correct unique key for the table" + Environment.NewLine;
2108+
Ribbon.AppVariables.ScriptRange += Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "WHEN NOT MATCHED BY TARGET" + Environment.NewLine;
2109+
Ribbon.AppVariables.ScriptRange += "THEN INSERT" + Environment.NewLine;
2110+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
2111+
Ribbon.AppVariables.ScriptRange += " " + Ribbon.ConcatenateColumnNames(tbl.Range, "", "[", "]") + Environment.NewLine;
2112+
Ribbon.AppVariables.ScriptRange += ")" + Environment.NewLine;
2113+
Ribbon.AppVariables.ScriptRange += "VALUES" + Environment.NewLine;
2114+
Ribbon.AppVariables.ScriptRange += "(" + Environment.NewLine;
2115+
Ribbon.AppVariables.ScriptRange += " " + Ribbon.ConcatenateColumnNames(tbl.Range, "S", "[", "]") + Environment.NewLine;
2116+
Ribbon.AppVariables.ScriptRange += ")" + Environment.NewLine;
2117+
Ribbon.AppVariables.ScriptRange += "WHEN MATCHED" + Environment.NewLine;
2118+
Ribbon.AppVariables.ScriptRange += "THEN UPDATE SET" + Environment.NewLine;
2119+
Ribbon.AppVariables.ScriptRange += Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "--WHEN NOT MATCHED BY SOURCE --< You may want to add a WHERE clause here" + Environment.NewLine;
2120+
Ribbon.AppVariables.ScriptRange += "--THEN DELETE" + Environment.NewLine;
2121+
Ribbon.AppVariables.ScriptRange += "OUTPUT @@SERVERNAME AS [Server Name], DB_NAME() AS [Database Name], $action, inserted.*, deleted.*;" + Environment.NewLine + Environment.NewLine;
2122+
Ribbon.AppVariables.ScriptRange += "ROLLBACK TRANSACTION;" + Environment.NewLine;
2123+
Ribbon.AppVariables.ScriptRange += "--COMMIT TRANSACTION;" + Environment.NewLine + Environment.NewLine;
2124+
Ribbon.AppVariables.ScriptRange += "GO";
20712125
Ribbon.AppVariables.ScriptRange = Ribbon.AppVariables.ScriptRange.Replace(@"""", String.Empty);
2072-
Ribbon.AppVariables.ScriptRange = Ribbon.GetCommentHeader("To update, insert & delete rows") + "SET XACT_ABORT ON" + Environment.NewLine + "BEGIN TRANSACTION;" + Environment.NewLine + Environment.NewLine + ";WITH " + Environment.NewLine + tableAliasTemp + Environment.NewLine + "AS " + Environment.NewLine + "(" + Environment.NewLine + Ribbon.AppVariables.ScriptRange + ") " + Environment.NewLine + "MERGE " + tableAlias + " AS T" + Environment.NewLine + "USING " + tableAliasTemp + " AS S" + Environment.NewLine + "ON " + Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "WHEN NOT MATCHED BY TARGET" + Environment.NewLine + "THEN INSERT" + Environment.NewLine + "(" + Environment.NewLine + Ribbon.ConcatenateColumnNames(tbl.Range, "", "[", "]") + Environment.NewLine + ")" + Environment.NewLine + "VALUES" + Environment.NewLine + "(" + Environment.NewLine + Ribbon.ConcatenateColumnNames(tbl.Range, "S", "[", "]") + Environment.NewLine + ")" + Environment.NewLine + "WHEN MATCHED" + Environment.NewLine + "THEN UPDATE SET" + Environment.NewLine + Ribbon.ConcatenateColumnNamesJoin(tbl.Range, "T", "S") + "--WHEN NOT MATCHED BY SOURCE AND 'ADD WHERE CLAUSE HERE'" + Environment.NewLine + "--THEN DELETE" + Environment.NewLine + "OUTPUT $action, inserted.*, deleted.*;" + Environment.NewLine + Environment.NewLine + "ROLLBACK TRANSACTION;" + Environment.NewLine + "--COMMIT TRANSACTION;" + Environment.NewLine + "GO";
2126+
20732127
}
20742128
catch (System.Runtime.InteropServices.COMException)
20752129
{

0 commit comments

Comments
 (0)