1

I have a mysql stored procedure like this:

CREATE DEFINER=`root`@`localhost` PROCEDURE `accounts_summary_by`(
    IN **created_by** int(10)
)
BEGIN
    SET group_concat_max_len=2048;
    SET @sql = NULL;
    SELECT
    GROUP_CONCAT(
    DISTINCT CONCAT(
    'MAX(IF(fiscal_year = ''',
    fiscal_year,
    ''', amount, 0)) AS ',
    CONCAT("'",fiscal_year,"'")
    )
    ) INTO @sql
    FROM
    net_savings;
    SET @sql = CONCAT('SELECT   accounts.id,
                                accounts.account, 
                                accounts.region, 
                                accounts.cluster, 
                                accounts.industry, ,'
                                ,@sql,
                                'FROM net_savings join accounts 
                                on  accounts.id = net_savings.account_id
                                Where accounts.user_id = **created_by**
                                GROUP BY accounts.account,net_savings.account_id 
                                ORDER BY accounts.id');

    PREPARE statement FROM @sql;
    EXECUTE statement;
    DEALLOCATE PREPARE statement;
    END

But upon calling the procedure like these:

CALL accounts_summary_by(2)

2 is a user_id reference to another table called users.

It gave me an error. Please help as I can't find any fixed to my problem.

0   72  23:41:12    CALL `buckets`.`accounts_summary_by`(1) Error Code: 1054. Unknown column 'created_by' in 'where clause' 0.000 sec
3
  • 2
    created_by is a field/table identifier, @created_by would be your parameter. Commented Aug 22, 2016 at 15:47
  • Is that double comma a typo? accounts.industry, ,' Commented Aug 22, 2016 at 15:47
  • ' is not a symbol used to denote field identifiers (such as aliases), use ` (the one with the ~ key.) Commented Aug 22, 2016 at 17:25

1 Answer 1

1

MySQL's internal programming language is not php, it is not going to resolve variables within a text, so you need to concat it properly to the middle of the prepared statement:

SET @sql = CONCAT('SELECT   accounts.id,
                            accounts.account, 
                            accounts.region, 
                            accounts.cluster, 
                            accounts.industry,'
                            ,@sql,
                            'FROM net_savings join accounts 
                            on  accounts.id = net_savings.account_id
                            Where accounts.user_id ='
                            ,created_by
                            ,'GROUP BY accounts.account,net_savings.account_id 
                            ORDER BY accounts.id');

Since created_by is a parameter of the procedure, you do not need to preposition it with @.

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

Comments

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.