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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:13:18+00:00 2026-05-29T11:13:18+00:00

I am trying to write simple mysql stored procedure and it seems that I

  • 0

I am trying to write simple mysql stored procedure and it seems that I can’t get it right, so far I have

    delimiter //
    create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50))
    begin
    declare dbArtist varchar(50);
    delcare dbRecord varchar(50);

    set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist));

    set dbRecord=(select recordname from records where lower(recordname)=lower(_record));
    if not exists (select * from Artists where lower(artistname)=lower(_artist)) then
    begin
      INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
      set dbArtist=_artist;
    end

    if not exists (select * from Records as R inner join Artists as A on R.ArtistId=A.ArtistId where lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist) then
    begin
      INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ((select artistid from artists where artistname=dbArtist),_record);
      set dbRecord=_record;
    end

    end

but I get syntax error in line 4:

#1064 - 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 'dbRecord varchar(50);
set dbArtist = (select artistname from artists where lowe' at line 4

this message error was returned to me by phpMyAdmin, can anyone tell me why do I get an error?

edit: modified version, still not good

delimiter //
create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50))
begin
declare dbArtist varchar(50);
declare dbRecord varchar(50);

set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist));
set dbRecord=(select recordname from records where lower(recordname)=lower(_record));
if not exists (select * from Artists where lower(artistname)=lower(_artist)) then
begin
  INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
  set dbArtist=_artist;
end

if not exists 
(select * from Records as R inner join Artists as A on R.ArtistId = A.ArtistId where     lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist) 
then
begin
  INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ( (select artistid from artists where artistname=dbArtist) ,_record);
  set dbRecord=_record;
end

end

now error in line 14 and message:

#1064 - 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 'if not exists (select * from Records as R inner join Artists as A on R.ArtistId' at line 14
  • 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-29T11:13:19+00:00Added an answer on May 29, 2026 at 11:13 am

    The problem is that you misspelled DECLARE:

    delcare dbRecord varchar(50);
    

    UPDATE: For your next error, the problem is your illegal use of NOT EXISTS.

    Within a stored procedure the proper approach is to count the existing rows, and then conditionally insert a value if the count is 0.

    Something like this:

    SELECT COUNT(*)
    INTO @v_row_count
    FROM Artists 
    WHERE LOWER(artistname)=LOWER(_artist);
    
    IF (@v_row_count = 0)
    THEN
      INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
      set dbArtist=_artist;
    END IF;
    

    P.S. To avoid poor performance on on your select query, you should consider using a collation that is not case-sensitive so you don’t need to apply the LOWER() function to the artistname column.

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

Sidebar

Related Questions

I am trying to write a stored procedure in MySQL which will perform a
I'm trying to write a simple WPF app that has two ellipses, joined by
I'm trying to write a simple ruby script that will copy a file to
I have a strange issue. I'm trying to write a simple php webpage on
I'm currently trying to write a simple ORM with PHP and mysql. I want
I'm trying to write a simple script for C to get values from a
I know that with mysql you can write SQL statements into a .sql file
I am having some difficulty getting a pretty simple stored procedure right. Consider the
I am trying to write a simple stream, streaming everything from a mysql field.
I'm trying to write a simple Java game. I have a NPC class, with

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.