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 4111772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:06:34+00:00 2026-05-20T22:06:34+00:00

In my previous question .I have got the solution to check out which data

  • 0

In my previous question.I have got the solution to check out which data was duplicated before insert data. This is the MySQL stored procedure to check data:

DROP PROCEDURE IF EXISTS check_user_files;
DELIMITER \\  
 CREATE PROCEDURE check_user_files(IN p_user_id INT,IN p_file_id CHAR(10))
   BEGIN
        IF EXISTS(SELECT 1 FROM myTable WHERE user_id = p_user_id AND file_id= p_file_id) THEN
            UPDATE myTable SET `file_status`=0 WHERE user_id=p_user_id AND file_id=p_file_id;
        ELSE
            INSERT INTO myTable(`user_id`,`file_status`,`file_id`) 
            VALUES (p_user_id,0,p_file_id);
        END IF; 
    END \\
DELIMITER ;

To call the stored procedure:

CALL check_user_files('1','12');

The table [myTable] structure:

auto_id  user_id  file_id file_status
1        1        12        1
2        3        12        0
3        1        17        1
4        4        31        1
5        1        41        0
6        4        31        0
7        1        18        1
8        5        11        0
9        1        10        0

And my problem is, I have multi-data to check and insert at the same time.E.g:

('1','12'),('2','14'),('3','16'),...

But I can’t call the stored procedure at the same time to check all records at one time.E.g:

CALL check_user_files('2','12');
CALL check_user_files('3','12');
...

It only call the first line of stored procedure.

So, how can I check all records in one query at the same time!>

Thank you very much!!

  • 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-20T22:06:34+00:00Added an answer on May 20, 2026 at 10:06 pm

    Can be done using a single call stored procedure but due to mysql not supporting array parameters it’s going to a little ugly as we have to pass in a comma separated string of user_id;file_id tokens. The only other alternative is to call the stored procedure multiple times.

    Full script here : http://pastie.org/1722362

    mysql> select * from user_files;
    +---------+---------+-------------+
    | user_id | file_id | file_status |
    +---------+---------+-------------+
    |       1 |       2 |           0 |
    |       3 |       6 |           0 |
    +---------+---------+-------------+
    2 rows in set (0.00 sec)
    
    call check_user_files('user_id;file_id, user_id;file_id...');
    
    call check_user_files('1;2, 2;4, 3;6, 4;10, 50;50, 1000;1, 1;10001');
    
    mysql> select * from user_files;
    +---------+---------+-------------+
    | user_id | file_id | file_status |
    +---------+---------+-------------+
    |       1 |       2 |           0 |
    |       1 |   10001 |           0 |
    |       2 |       4 |           0 |
    |       3 |       6 |           0 |
    |       4 |      10 |           0 |
    |      50 |      50 |           0 |
    |    1000 |       1 |           0 |
    +---------+---------+-------------+
    7 rows in set (0.00 sec)
    

    Stored procedure

    drop procedure if exists check_user_files;
    
    delimiter #
    
    create procedure check_user_files
    (
    in p_users_files_csv varchar(4096)
    )
    proc_main:begin
    
    declare v_token varchar(255);
    
    declare v_done tinyint unsigned default 0;
    declare v_token_idx int unsigned default 1;
    
    declare v_user_id int unsigned default 0;
    declare v_file_id int unsigned default 0;
    
        set p_users_files_csv = replace(p_users_files_csv, ' ','');
    
        if p_users_files_csv is null or length(p_users_files_csv) <= 0 then
            leave proc_main;
        end if;
    
        -- the real ugly bit
    
        -- split the string into user_id/file_id tokens and put into an in-memory table...
    
        create temporary table tmp(
            user_id int unsigned not null,
            file_id int unsigned not null
        )engine = memory;   
    
        while not v_done do
    
          set v_token = trim(substring(p_users_files_csv, v_token_idx, 
            if(locate(',', p_users_files_csv, v_token_idx) > 0, 
              locate(',', p_users_files_csv, v_token_idx) - v_token_idx, length(p_users_files_csv))));
    
          if length(v_token) > 0 then
    
         set v_token_idx = v_token_idx + length(v_token) + 1;
    
             set v_user_id = mid(v_token, 1, instr(v_token, ';')-1);
             set v_file_id = mid(v_token, instr(v_token, ';')+1, length(v_token));
    
             insert into tmp (user_id, file_id) values(v_user_id, v_file_id);
          else
            set v_done = 1;
          end if;
    
      end while;
    
      -- the nice bit - insert the new values
    
      insert into user_files (user_id, file_id, file_status)
      select
       t.user_id, t.file_id, 0 as file_status
      from
       tmp t
      left outer join user_files uf on t.user_id = uf.user_id and t.file_id = uf.file_id
      where 
       uf.user_id is null and uf.file_id is null;
    
      drop temporary table if exists tmp;
    
    end proc_main #
    
    delimiter ;
    

    Hope this helps

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

Sidebar

Related Questions

No related questions found

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.