简体   繁体   中英

How can I add a parameter for mysql variable?

I have query in mysql which contains one variable. The query is

SELECT
  GROUP_CONCAT(
    CONCAT(
      ' MAX(IF(Property = ''',
      t.Property,
      ''', Value, NULL)) AS ',
      t.Property
    )
  ) INTO @PivotQuery
FROM
  (SELECT
     Property
   FROM
     ProductOld
   GROUP BY
     Property) t;

SET @PivotQuery = CONCAT('SELECT ProductID,', @PivotQuery, ' FROM ProductOld GROUP BY ProductID'); 

PREPARE statement FROM @PivotQuery;
EXECUTE statement;
DEALLOCATE PREPARE statement;

But, when I run using mysqlcommand in my vb.net application, it throws an error stating that Parameter @PivotQuery must be defined..

将此代码与您现有的代码交换:SET @PivotQuery = CONCAT('SELECT ProductID',@PivotQuery,'FROM ProductOld GROUP BY ProductID');

Private Function saveCustomer()
Dim command As MySqlCommand = Nothing
Dim query As String = "INSERT INTO contacts (first_name, surname, house_number, street, suburb, state, phone, mobile, work, email, notes) VALUES (@first_name, @surname, @housenumber, @street, @suburb, @state, @phone, @mobile, @work, @email, @notes)"
Try
    If connection.State = ConnectionState.Closed Then
        connection.Open()

    End If
    command = New MySqlCommand(query, connection)
    command.Parameters.AddWithValue("@first_name", txtFirstName.Text)
    command.Parameters.AddWithValue("@surname", txtSurname.Text)
    command.Parameters.AddWithValue("@housenumber", txtHouseNo.Text)
    command.Parameters.AddWithValue("@street", txtStreet.Text)
    command.Parameters.AddWithValue("@suburb", txtSuburb.Text)
    command.Parameters.AddWithValue("@state", cboState.Text)
    command.Parameters.AddWithValue("@phone", txtPhone.Text)
    command.Parameters.AddWithValue("@mobile", txtMobile.Text)
    command.Parameters.AddWithValue("@work", txtWork.Text)
    command.Parameters.AddWithValue("@email", txtEmail.Text)
    command.Parameters.AddWithValue("@notes", txtNotes.Text)
    command.ExecuteNonQuery()
    MessageBox.Show("Contact Saved Sucessfully")
    Return True
Catch ex As MySqlException
    Return False
Finally
    connection.Close()
    command.Dispose()
End Try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM