Advertisement

09.17.2008 at 01:54AM PDT, ID: 23737847 | Points: 500
[x]
Attachment Details

inline mysql mult-line query using c#

Asked by simonhurley in SQL Query Syntax, MySQL Server, Programming for ASP.NET

Tags: ,

Hi,

I've attached a function as a code snippet.  the function makes an call to mysql,  the aim of the function is to return an 'account id'.

i pass an order number and telephone number into the function, the mysql query then tries to find a matching "accountid" field value in the "Account" table, if it does then it returns that id, else it inserts the order# tel# as a new record and return the new identity id for that table.

Can anybody assist on how to correct the query itself as it throws an error...

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE temp_id INTEGER;
                   SET temp_id = (SELECT ID FROM Acco' at line 2"Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
/// <summary>
        /// Return an account ID based on the ordernumber and telephone;
        /// If no account already exists then generate a new account id and return its number.
        /// </summary>
        /// <param name="orderNumber"></param>
        /// <param name="telephoneNumber"></param>
        /// <returns></returns>
        public int GetAccountId(string orderNumber, string telephoneNumber)
        {
            #region Instrumentation
 
            if (log.IsInfoEnabled)
            {
                log.InfoFormat("{0}", MethodInfo.GetCurrentMethod().Name);
            }
 
            #endregion
 
            // Get the id of the account based on the order number and tel, if no id then generate a new account
            const string query = @"
                BEGIN
                    DECLARE temp_id INTEGER;
                    SET temp_id = (SELECT ID FROM Account WHERE OrderNumber=@OrderNumber AND Telephone=@Telephone LIMIT 1);
 
                    IF temp_id = 0 THEN
                      BEGIN
                        INSERT INTO Account (OrderNumber,Telephone) VALUES (@OrderNumber,@Telephone);
                        SELECT last_insert_id();
                      END;
                    ELSE
                      BEGIN
                        SELECT temp_id;
                      END;
                    END IF;
                END
                    ";
 
            MySqlDataAdapter msda = null;
 
            try
            {
               msda = new MySqlDataAdapter(query, connectionString);
               msda.SelectCommand.CommandType = CommandType.Text;
               msda.SelectCommand.Parameters.AddWithValue("@OrderNumber", orderNumber);
               msda.SelectCommand.Parameters.AddWithValue("@Telephone", orderNumber);
 
               DataTable dt = null;
 
               try
               {
                   dt = new DataTable();
                   msda.Fill(dt);
                   if (dt.Rows.Count != 0)
                   {
                       dt.Locale = CultureInfo.InvariantCulture;
 
                       return Convert.ToInt32(dt.Rows[0][0]);
                   }
                   else
                   {
                       return 0;
                   }
               }
               catch (Exception ex)
               {
                   dt.Dispose();
                   throw ex;
               }
           }
           finally
           {
               if (msda != null) msda.Dispose();
           }
        }
[+][-]09.17.2008 at 03:00AM PDT, ID: 22496943

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 03:22AM PDT, ID: 22497014

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 03:44AM PDT, ID: 22497097

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 03:48AM PDT, ID: 22497116

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 05:41AM PDT, ID: 22497750

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 05:45AM PDT, ID: 22497787

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 06:09AM PDT, ID: 22498016

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.14.2008 at 04:26PM PST, ID: 22965080

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 14-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628