Parameterize MySQL workbench statements: How to define variables

10,446

Solution 1

In MySQL, syntax for setting variable is below.

SET @var1 = '112233';

and using the variable would be as below.

select * from providers where id=@var1;

Check out MySQL documentation for more information Link to MySQL Documentation

Solution 2

Based on your tag mysql-workbench, I find it simply to be a case where the referenced documentation and use is not relevant to what you are using.

Back up the hierarchy from your link to this http://www.sql-workbench.net/

and you will read:

Please note that SQL Workbench/J has no relation to the product MySQL Workbench which is maintained and owned by Oracle. If you are looking for MySQL Workbench support please contact Oracle.

Share:
10,446
Kashyap
Author by

Kashyap

I have more than 16 years of multifaceted experience in Large Scale Software Development. Including as System Architect, Line Manager, Scrum Master and Software Developer. Have developed good understanding of various functions from Program/Project/Product Management to Senior Management to Architectural/Technical leadership. Able to understand/anticipate needs of various stakeholders and communicate in their language. Have successfully led multiple large scale architecture initiatives on enterprise grade products, with focus on improving performance, scalability, availability and maintainability. I am a Certified ScrumMaster (CSM) and have rich experience with Agile (Scrum) as well as Waterfall development processes. As manager I have received award for building an Agile Feature team from scratch. 5+ years of experience in providing technical leadership in various roles. 4+ years of experience in direct and indirect people management. 10+ years of experience working with and developing Highly Scalable and Available Platforms. 8+ years of experience with scalable backend development. 3+ years of experience in developing Data Warehousing/ETL functions in NetAct platform. Vast experience with Java, J2EE, C/C++, Linux, scripting in Shell, Python, Ruby etc. Legal Status in USA: Permanent Resident (Greencard Holder) Software Architecture, People Management, Project Management, Performance Management, Agile, Scrum, Waterfall, TDD, ATDD, Telecom, ETL, SQL, Java, J2EE, EJB, C, C++, Shell, Python, Ruby, Rational Rose, EMF, Modeling, JMeter, Subversion/SVN, Maven, Bamboo, Jenkins, Robot, Clearcase, Git, O&M, OSS, BSS, NetAct, AWS EC2, Ansible, Puppet, Chef, Zabbix, Zookeeper, Datadog, Munin, Virtualization, VMWare, BigIP, F5, Linux, Oracle, Relational Database, BigData, Cassandra, Algorithm Optimization, Scalability, High Availability, OOAD.

Updated on June 14, 2022

Comments

  • Kashyap
    Kashyap almost 2 years

    I'm trying to parameterize a set of frequently used queries in my workbench.

    This works:

    select * from providers where id='112233';
    

    This

    WbVarDef var1=112233;
    
    select * from providers where id='$[var1]';
    

    gives error

    Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from providers where id='112233'' at line 1

    My reference was this.

    Just to be clear, these are in the MySQL workbench and not a workbench script file or a mysql script file.

  • ADTC
    ADTC about 6 years
    To be clear, this has nothing to do with Workbench specifically. It's a regular MySQL syntax/feature that can be used by any application accessing the database. It is often used in triggers and procedures.
  • cyber.sh
    cyber.sh about 6 years
    yes, this is a mysql syntax. Nothing to do with workbench