0

Hey i want to write a function in sql which insert the handover parameters. My Code:

CREATE PROCEDURE `p_add_client`(IN p_username VARCHAR(45),IN p_reach VARCHAR(45),IN p_purchase VARCHAR(45))
BEGIN
set @str = ('INSERT INTO t_', p_username,'(Purchase,Reach) VALUES (p_purchase,p_reach)');
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;

but it doesnt work. I get the error Code 1064. Can anyone help me?

1 Answer 1

1

You are missing CONCAT():

set @str = CONCAT('INSERT INTO t_', p_username, '(Purchase, Reach) VALUES (p_purchase, p_reach)');

However, you will then get an error when you execute, because the variables are not known. So, you could write:

set @str = CONCAT('INSERT INTO t_', p_username, '(Purchase, Reach) VALUES (p_purchase, p_reach)');

prepare stmt from @str;
execute stmt using p_purchase, p_reach;
deallocate prepare stmt;

Of course, this all suggests a pretty bad database architecture. Why would you have a separate table per user, rather than rows in a single table for all users? There are some arcane reasons why this might be a solution to a real problem, but such an approach much more commonly suggests a problem with the solution architecture.

Sign up to request clarification or add additional context in comments.

2 Comments

actually i had only one table. but for 100 clients the table has 1*10^7 rows. so i thougt it is better for the perfomance if every one has his own table. so i have to do check only some thousend rows for a client. is it wrong? on table would be very easyier
@nicoschuck . . . You want one table with an appropriate index. If you have ten million rows, you might also want some sort of partitioning scheme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.