Book a Demo

Author Topic: Recursive SQL statements using WITH (CTE)  (Read 8523 times)

Mads Kirstan

  • EA Novice
  • *
  • Posts: 14
  • Karma: +1/-0
    • View Profile
Recursive SQL statements using WITH (CTE)
« on: May 30, 2017, 09:00:18 pm »
Hi,

What is the status on EA not supporting the WITH clause in SQL?

Kind regards
Mads

PeterHeintz

  • EA Practitioner
  • ***
  • Posts: 1001
  • Karma: +59/-18
    • View Profile
Re: Recursive SQL statements using WITH (CTE)
« Reply #1 on: May 30, 2017, 10:22:51 pm »
EA only process sql select statements and I assume that this will not change in the near future.
However you could add views on you sql server, having "With" clauses and call these views by EA select statements. At least that is what we do.
Best regards,

Peter Heintz

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Recursive SQL statements using WITH (CTE)
« Reply #2 on: May 31, 2017, 09:09:35 pm »
EA only process sql select statements and I assume that this will not change in the near future.
Well, in Repository.SqlQuery() that's true, but the undocumented Repository.Execute() doesn't have the same restrictions. Might be worth a try.

/Uffe
My theories are always correct, just apply them to the right reality.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Recursive SQL statements using WITH (CTE)
« Reply #3 on: May 31, 2017, 09:14:02 pm »
Unfortunately Repository.Execute does not deliver any query results.

q.

PeterHeintz

  • EA Practitioner
  • ***
  • Posts: 1001
  • Karma: +59/-18
    • View Profile
Re: Recursive SQL statements using WITH (CTE)
« Reply #4 on: June 01, 2017, 08:49:56 pm »
Yes, we also tried Repository.SQLQurery and got no results. The only way we found to use "With" was to have the "with" stored on the server and call/select it from EA.
Best regards,

Peter Heintz

McMannus

  • EA User
  • **
  • Posts: 108
  • Karma: +4/-1
    • View Profile
Re: Recursive SQL statements using WITH (CTE)
« Reply #5 on: August 01, 2017, 09:09:15 pm »
What you can also try is directly working on the EAP project with C# DB access mechanisms. I did that once for updating absolute paths for toolbox profile icons to the current version control checkout path (helps enormously if different work on MDG development). I believe with this method, the syntax you are allowed to use is just the database technology itself, so you could try the with statement with this method:

Code: [Select]
                var profilesProject =  "profiles.eap";
                const string iconFolder = "\\icons";
                try
                {

                    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;"
                                                               + "Data Source=" + profilesProject);
                    conn.Open();

                    using (DataTable dt = new DataTable())
                    {
                        using (OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT [Default],ID FROM t_attribute WHERE Name='Icon'", conn))
                        {
                            adapter.Fill(dt);
                        }
                        List<string> queries = new List<string>();
                        foreach (DataRow row in dt.Rows)
                        {
                            string toolboxIconPath = row["Default"].ToString();
                            if (!toolboxIconPath.StartsWith(executingFolder))
                            {
                                int index = toolboxIconPath.IndexOf(iconFolder);
                                if (index > 0)
                                {
                                    string newPath = executingFolder + toolboxIconPath.Substring(index);
                                    Console.WriteLine("\tReplacement: " + toolboxIconPath + " -> " + newPath);
                                    queries.Add("UPDATE [t_attribute] SET [Default]='"+newPath+"' WHERE [ID]="+row["ID"]);
                                }
                            }
                        }

                        if (queries.Any())
                        {
                            Console.WriteLine("\nAre the replaced paths correct? Go on with 'y'");
                            ConsoleKeyInfo pressedChar = Console.ReadKey();
                            if (pressedChar.KeyChar == 'y')
                            {
                                foreach (string query in queries)
                                {
                                    using (OleDbCommand cmd = new OleDbCommand(query, conn))
                                    {
                                        cmd.ExecuteNonQuery();
                                    }
                                }
                                Console.WriteLine("\nToolbox Icon Paths updated successfully :-)");
                            }
                        }
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine("\nAn error occured during updating the toolbox icon image paths. Are you sure that you have the SVN lock on the profiles.eap file?");
                }
                Console.WriteLine("\n\nPress any key to exit...");
                Console.ReadKey();