Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:07:06+00:00 2026-05-24T01:07:06+00:00

I try to make a clean trigger with user-defined variables, so this is my

  • 0

I try to make a clean trigger with user-defined variables, so this is my code :


SET @target_bdd = 'trigger_second';
SET @trigger_name = 'account_copy';
SET @table_name = 'account';
SET @primary_key = 'id';

DROP TRIGGER IF EXISTS `@trigger_name`;

DELIMITER $$

CREATE TRIGGER `@trigger_name` AFTER INSERT
    ON `@table_name`
FOR EACH ROW BEGIN
    INSERT INTO `@target_bdd`.`@table_name`
    SELECT * FROM `@table_name` 
    WHERE `@primary_key` = NEW.`@primary_key`; 
END $$
DELIMITER ;

But I have this error :

ERROR 1064 (42000): 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 '@table_name
FOR EACH ROW BEGIN INSERT INTO `@target_bdd`.`@table_name` SELECT * ' at line 2

Why ? I tried with others quotes (‘”) but there’s same error 🙁

With no quote :

ERROR 1064 (42000): 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 ‘@trigger_name AFTER INSERT ON @table_name FOR EACH
ROW BEGIN INSERT INTO @target’ at line 1

I try to declare an user-defined variable with SQL instructions like :

SET @sql = CONCAT(
    'CREATE TRIGGER ',
    @trigger_name,
    ' AFTER INSERT ON ',
    @table_name,
    ' FOR EACH ROW BEGIN INSERT INTO ',
    @target_bdd,
    '.',
    @table_name,
    ' SELECT * FROM ',
    @table_name, 
    ' WHERE ',
    @primary_key,
    ' = NEW.',
    @primary_key, 
    '; END'
    );

PREPARE stmt FROM @sql;
EXECUTE stmt;

But I have an error 1295 (See in MySQL Documentation)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:07:06+00:00Added an answer on May 24, 2026 at 1:07 am

    You can’t use user variables for table names, columns etc in plain SQL. You can’t also create a trigger in prepared statement (just like the error told you). Seems like there is currently no way to do what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will try to make this as clear as I can, but if you
Ok, I'm going to try to make this more clear because my last question
I try to make a registration form. When user post the fields, i check
I'll try to make this as straight forward as possible. Currently our team has
I am starting this question to try and make a central point developers can
What will be the code when I try to make a combo box read
I'm fairly new to C# but I will try to make this quick! ;)
I have below variables try to make an auto generating SQL statement in python
I'll try to make this as simple a posible. I just started working with
When I try to make a very large boolean array using Java, such as:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.