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

  • SEARCH
  • Home
  • 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 8923583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:09:51+00:00 2026-06-15T07:09:51+00:00

I understand that prepared statements aren’t allowed within a trigger, but I can’t see

  • 0

I understand that prepared statements aren’t allowed within a trigger, but I can’t see how else I can do what I want, so any alternatives/workarounds would be greatly appreciated.

DELIMITER $$

CREATE TRIGGER trigger1 after INSERT on table1
FOR EACH ROW
begin
DECLARE a varchar(30);
DECLARE b varchar(30);
DECLARE c varchar(30);
DECLARE d varchar(30);
SET a = new.Col1;
SET b = new.Col2;
SET c = (select Col4 from table2 where Col3=new.Col3);
SET d = concat('select ',a,' from table3 where yq=',c,' and xz=',b);
insert into table4 values (d);
end
$$

DELIMITER;

So what is happening: a row is inserted and 3 values are taken from that row, Col1,Col2, and new.Col3.
new.Col3 is used to get a row from a different table, which is then used along with Col2 to search the 3rd table where the value I need is.
Col1 is the problem because it is supposed to be a dynamically generated column name for d but this doesn’t work.
So obviously at the moment all I am getting back is a string of the select statement in d which is what I expect, but I cant execute the statement because that is not allowed.
So how can I get that dynamically generated column name in to the select statement?

Table 1

Col1 | Col2 | Col3
c    | d    | x
a    | e    | y
b    | e    | z
b    | f    | x

Table 2

Col3 | Col4
x    | 1
y    | 2 
z    | 3

Table3

a    | b    | c    | xz    | yq
1a   | 1b   | 1c   |  d    | 1
2a   | 2b   | 2c   |  d    | 2
3a   | 3b   | 3c   |  d    | 3
4a   | 4b   | 4c   |  e    | 1
5a   | 5b   | 5c   |  e    | 2
6a   | 6b   | 6c   |  e    | 3
7a   | 7b   | 7c   |  f    | 1
8a   | 8b   | 8c   |  f    | 2
9a   | 9b   | 9c   |  f    | 3

The result I want is from 1a-9c.

I have considered changing the table schema so that the column name in the trigger can be fixed, but that would be a last resort since it would cause issues for other bits of code.

  • 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-06-15T07:09:53+00:00Added an answer on June 15, 2026 at 7:09 am

    You will have to abandon the idea of prepared statements and dynamic SQL altogether and just painfully do some IF..THEN statements:

    DELIMITER $$
    
    CREATE TRIGGER trigger1 after INSERT on table1
    FOR EACH ROW
    begin
    DECLARE a varchar(30);
    DECLARE b varchar(30);
    DECLARE local_c varchar(30);
    DECLARE d varchar(30);
    SET a = new.Col1;
    SET b = new.Col2;
    SET local_c = (select Col4 from table2 where Col3=new.Col3);
    IF local_c = 'a' THEN
        select a into d from table3 where yq=local_c and xz=b;
    END IF;
    IF local_c = 'b' THEN
        select b into d from table3 where yq=local_c and xz=b;
    END IF;
    IF local_c = 'c' THEN
        select c into d from table3 where yq=local_c and xz=b;
    END IF;
    IF ISNULL(d) = 0 THEN
        insert into table4 values (d);
    END IF;
    end
    $$
    
    DELIMITER;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm into a problem that I can't really understand. I want to include directly
I do understand that the prepared statements is the ultimate way to seek protection
On an older server I'm using that I can't use prepared statements on I
I understand that applications under the same domain name can talk to each other
I understand that we can set the various style attributes from code behind using
I am trying understand how multi queries work in mysqli. But I confess that
Can someone put more light on the functions: sqlite3_reset(); sqlite3_clear_bindings() I understand that I
I was told today that I should really be using PDO and prepared statements
I dont understand those few statements that I read: because accessing a volatile variable
I understand that: '\n' // literally the backslash character followed by the character for

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.