MySQL Error: 1305. Stored Procedures - mysql

I'm getting
Error Code: 1305. FUNCTION acdb_extended.player does not exist
When I attempt to call a stored procedure. The odd thing about the error is that "player" isn't the name of the stored procedure.
Here's the stored procedure code, there is probably a number of things wrong with it but it's correct as far as I can tell:
CREATE PROCEDURE `acdb_extended`.`addAllianceMember` (IN accountNumber VARCHAR(255),
IN userName VARCHAR(255), IN serverInitial CHAR(1), IN galaxy TINYINT(2),
IN region TINYINT(2), IN system TINYINT(2), IN astro TINYINT(2), IN level TINYINT(2))
BEGIN
IF player (account_number) = accountNumber
THEN REPLACE INTO player (username)
VALUES (userName);
ELSE INSERT INTO player (account_number, username)
VALUES (accountNumber, userName);
END IF;
IF coordinates (server_initial) = serverInitial AND
coordinates (galaxy) = galaxy AND
coordinates (region) = region AND
coordinates (system) = system AND
coordinates (astro) = astro
THEN REPLACE INTO coordinates (player_ID)
VALUES ((SELECT player_ID FROM player WHERE username = userName));
ELSE INSERT INTO coordinates (player_ID, server_initial, galaxy, region, system, astro)
VALUES ((SELECT player_ID FROM player WHERE username = userName), serverInitial,
galaxy, region, system, astro);
END IF;
IF jumpgate (player_ID) = (SELECT player_ID FROM player WHERE username = userName) AND
jumpgate (coordinates_ID) = (SELECT c.coordinates_ID FROM coordinates c, player p WHERE c.player_ID = p.player_ID
AND p.username = userName)
THEN REPLACE INTO jumpgate (level)
VALUES (level);
ELSE INSERT INTO jumpgate (player_ID, coordinates_ID, level, usable)
VALUES ((SELECT player_ID FROM player WHERE username = userName),
(SELECT c.coordinates_ID FROM coordinates c, player p WHERE c.player_ID = p.player_ID
AND p.username = userName), level, TRUE);
END IF;
END
And here is the SQL statement I'm using to test it:
CALL addAllianceMember(8494618, 'Carl', 'G', 29, 08, 10, 01, 04);
Any hints on where this is going wrong?

in this line
IF player (account_number) = accountNumber
it's calling player like a function, it's look like player doesn't exists as a function.

Here you have used Player as function just after BEGIN.
BEGIN
IF player (account_number) = accountNumber

Related

Mysql integrity error 1452 on insert

select * from memory_games_game; gives me following table:
select * from memory_games_game_state;gives me following table:
I have a stored proc as bellow:
delimiter //
CREATE PROCEDURE get_game_by_user_id(
p_user_id int, p_game_id int
)
BEGIN
insert into memory_games_game_state(user_id, game_id, level, progress)
SELECT 24 as user_id,
game.game_type as game_id,
1 as level,
0 as progress
FROM memory_games_game game
left outer join memory_games_game_state gameState on
game.game_type=gameState.game_id and
gameState.user_id=24
where game.level=1 and gameState.user_id is null;
if p_game_id = -1 then
SELECT gameState.level, game_type, `current_date`
FROM memory_games_game game join memory_games_game_state gameState on
game.game_type=gameState.game_id and
gameState.user_id=24 and
game.level=gameState.level;
else
SELECT gameState.level, game_type, `current_date`
FROM memory_games_game game join memory_games_game_state gameState on
game.game_type=gameState.game_id and
gameState.user_id=p_user_id and
game.level=gameState.level
WHERE game_type=12;
end if;
END
//
The first insert inserts the records into memory_games_game_statetable.
This insert is successful for game_id from 8 to 11 however, it fails for 12 with following error:
I am able to insert records in table memory_games_gamefor game_type 12 which is nothing but game_id in the other table i.e. memory_games_game_state
What's going wrong?
UPDATE:
My django models:
class Game(models.Model):
#Field for storing game type
GAME_TYPE_CHOICES = (
(8, 'Simon Game'),
(9, 'Pinpoint reaction'),
(10, 'Loslassen'),
(11, 'Word pair'),
(12, 'Wortschatz')
)
game_type = models.IntegerField(choices=GAME_TYPE_CHOICES)
level = models.IntegerField(default='1')
#This helps to print in admin interface
def __str__(self):
return u"%s level %s" % (self.get_game_type_display(), self.level)
class Game_state(models.Model):
game = models.ForeignKey(Game, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
level = models.IntegerField(default='1')
progress = models.IntegerField(default='0')
current_date = models.DateField(auto_now=True)
class Game_state_ui_model(models.Model):
GAME_TYPE_CHOICES = (
(8, 'Simon Game'),
(9, 'Pinpoint reaction'),
(10, 'Loslassen'),
(11, 'Word pair'),
(12, 'Wortschatz')
)
game_type = models.IntegerField(choices=GAME_TYPE_CHOICES)
level = models.IntegerField()
user_id = models.IntegerField(default='0')
current_date = models.DateField(auto_now=True)
# static method to fetch games for a paricular user
#staticmethod
def fetch_games(user_id, game_id):
print("Fetching games in model")
# create a cursor
cur = connection.cursor()
# execute the stored procedure passing in
# search_string as a parameter
cur.callproc('get_game_by_user_id', [user_id, game_id,])
# grab the results
results = cur.fetchall()
cur.close()
Game_state_list=[]
for row in results:
print("Get game", row)
Gs = Game_state_ui_model()
Gs.level=row[0]
Gs.game_type=row[1]
Gs.current_date=row[2]
Game_state_list.append(Gs)
return Game_state_list
As the error states, game_id references memory_games_game.id; NOT memory_games_game.game_type.
The thinking that "game_type 12 which is nothing but game_id in the other table i.e. memory_games_game_state" is incorrect.
You need a row in memory_games_game with id = 12.
I dropped all the tables and did migration again, which somehow solved the problem. I didn't change anything else.

How to optionally select multiple values for the same column in SQL?

So I am using Kentico CMS Desk 7 to generate reports for my company. In Kentico you create parameters and then create a table using sql and those parameters with the # symbol so whatever the user enters into that parameter, it will be the value of a parameter variable like #Status. I am wanting to add the ability for the user to either enter in one value, multiple values, or no values into the parameters, but I do not know how to implement the multiple values. I am a little new to SQL so bear with me. This is the SQL code I have right now:
select
ClaimNumber as 'Claim Number',
CustomerName as 'Customer Name',
DollarAmount as 'Dollar Amount',
[ReasonCode] as 'Reason code',
rt.[ReasonTypeName] as 'Reason type',
PlantNumber as 'Selling Company',
Status as 'Status'
from TABLE1 as c
join TABLE2 as u on u.UserID = c.DocumentCreatedByUserID
left join TABLE3 as rt on rt.ItemId = c.ReasonType
where ClaimDate between #FromDate and #ToDate
and ReasonCode like #ReasonCode
and ReasonType like #ReasonType
and (#SellingCompany = '' or PlantNumber = #SellingCompany)
and Status like #Status
order by ClaimNumber;
The parameter that I am trying to do this with is the selling company parameter denoted as #SellingCompany. Right now, this works for users not entering in any value and users entering in only one value, but I would like for users to have the ability to input multiple values separated by commas. I feel like an IN operator might work, but I am inexperienced in SQL and I don't know how I would implement this. I can't publish the data obviously because there is customer information, but this statement works as it is and I just need to know how to implement what I'm wanting to do. Thanks guys!
Have you tried this?
select
ClaimNumber as 'Claim Number',
CustomerName as 'Customer Name',
DollarAmount as 'Dollar Amount',
[ReasonCode] as 'Reason code',
rt.[ReasonTypeName] as 'Reason type',
PlantNumber as 'Selling Company',
Status as 'Status'
from TABLE1 as c
join TABLE2 as u on u.UserID = c.DocumentCreatedByUserID
left join TABLE3 as rt on rt.ItemId = c.ReasonType
where ClaimDate between #FromDate and #ToDate
and ReasonCode like #ReasonCode
and ReasonType like #ReasonType
and (#SellingCompany = '' or PlantNumber IN (#SellingCompany))
and Status like #Status
order by ClaimNumber;
I use this SQL function specifically when I need to cast a delimited string to a table value to use with the IN operator.
CREATE FUNCTION [dbo].[ParseIDListToTable]
(#vc_Ids nvarchar(MAX))
RETURNS #Id_table TABLE
(ID nvarchar(15))
BEGIN
DECLARE #in_Index1 AS INT, --Used to store ID delimiter(',') position in string
#vc_ID AS NVARCHAR(15)
/* initialize working variables */
SET #in_Index1 = CHARINDEX(',',#vc_Ids)
/* loop through ids in delimited string */
WHILE (#in_Index1 > 0 OR LEN(#vc_Ids) > 0)
BEGIN
/* parse out single id for processing */
IF #in_Index1 > 0
BEGIN
SET #vc_ID = Left(#vc_Ids,#in_Index1 - 1)
SET #vc_Ids = Right(#vc_Ids,Len(#vc_Ids) - #in_Index1)
END
ELSE
BEGIN
SET #vc_ID = #vc_Ids
SET #vc_Ids = ''
END
INSERT #Id_table (ID)
VALUES(#vc_ID)
/* prepare to loop */
SET #in_Index1 = CHARINDEX(',',#vc_Ids)
END
/* return the ids */
RETURN
END
Then I use it in my SELECT statement like so
WHERE PlantNumber IN (SELECT * FROM dbo.ParseIDListToTable('Microsoft,Apple,Dell'))
This should return the results you're looking for.

MySQL any keyword usage

I am having the following issue:
When I execute the following statement, I get an error for it returning more than one row
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow'),
(SELECT musicgenreid
FROM musicgenre
WHERE musicgenreid = 4),
( totallikes = 328374 ) );
I am getting the error on the (select pesonID from person...) statement, and I am trying to use the 'any' keyword to just grab any row, but I cannot seem to get it to work. I have tried just about any permutation I can think of of the current query and 'any', but it does not work. Is there another solution I should be trying or am I just missing the mark for some reason?
It seems you're trying to do something like this:
INSERT INTO artist (personid, musicgenreid, totallikes)
VALUES (
(SELECT personid FROM person
WHERE firstname = 'Joe' AND middlename = '' AND lastname = 'blow'
ORDER BY RAND()
LIMIT 1
),
4,
328374
);
This will get you a random personid that matches the given criteria.
The musicgenreid in your query would be either null or 4. I am forcing it to 4 as it seems that you're manually adding them and you know they already exist.
The total likes field is also fixed but your syntax was incorrect.
try with this sql statement
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow' LIMIT 1 ),
4,
328374);

mysql stored procedure recieving cannot be null errors from valid query

Here is my stored Procedure
CREATE PROCEDURE `tblCarGarage_Insert`(
IN URL varchar(700),
IN Price int(7),
IN Make varchar(60),
IN Model varchar(60),
IN TrimLevel varchar(60),
IN Miles int(6),
IN ZipCode int(5),
IN Description varchar(80),
IN Color varchar(30),
IN Owner varchar(100),
IN VIN varchar(20),
IN VINKeyCode varchar(12),
IN CarYear int(4),
IN ListingID int(14))
INSERT INTO tblcargarage
SET URL = #URL,
Price = #Price,
rBit = b'1',
DateDiscovered = NOW(),
LastProcessDate = NOW(),
CarYear = #CarYear,
Make =
(SELECT uniqueID
FROM tbltranslationmake
WHERE make = 'Honda'),
Model =
(SELECT uniqueID
FROM tbltranslationmodel
WHERE model = #Model),
CarTrim =
(SELECT uniqueID
FROM tbltranslationtrim
WHERE trimlevel = #TrimLevel),
Miles = #Miles,
ZipCode = #ZipCode,
Description = #Description,
Color = #Color,
Owner = #Owner,
VIN = #VIN,
VINKeyCode = #VinkeyCode,
ListingID = #ListingID;
Here is my execution
SET #CarYear = '';
SET #URL = 'asdas';
SET #Price = 20112;
SET #Make = 'Honda';
SET #Model = 'Civic';
SET #TrimLevel = 'Ex';
SET #Miles = 20112;
SET #Description = 'asdasdasd';
SET #Color = 'yellow';
SET #Owner = 'asdasdas';
SET #VIN = 'qeqweqweqw2e';
SET #VinkeyCode = 'asd23sd';
SET #ListingID = 1231231;
SET #ZipCode = 12331;
CALL tblCarGarage_Insert(#CarYear,
#URL,
#Price,
#Make,
#Model,
#TrimLevel,
#Miles,
#Description,
#Color,
#Owner,
#VIN,
#VinkeyCode,
#ListingID,
#ZipCode)
SELECT uniqueID
FROM tbltranslationmake
WHERE make = 'Honda' returns 11 (as an int)
Model =
(SELECT uniqueID
FROM tbltranslationmodel
WHERE model = #Model),
Returns 712 as an int
CarTrim =
(SELECT uniqueID
FROM tbltranslationtrim
WHERE trimlevel = #TrimLevel),
returns 12334 as an int.
Yes all 3 of those columns are INT in my DB,
I keep getting 'Column 'Make' cannot be null, if i run the query as a non SP just as a query Insert into asd set x = 'x' ect... it works just fine with the same values. Any idea?
Thanks!
This must have to do something with passing in a varchar and the table expecting an int. Maybe it's not returning the correct error?
Looking at your code, the first problem I notice is that in your CALL statement you are passing the parameters in the wrong order. For example, in the stored procedure definition the first parameter is URL, but in your call to the procedure the first parameter you pass in is CarYear.
Try passing the parameters in the correct order and see if that works for you:
CALL tblCarGarage_Insert(
#URL,
#Price,
#Make,
#Model,
#TrimLevel,
#Miles,
#ZipCode,
#Description,
#Color,
#Owner,
#VIN,
#VinkeyCode,
#CarYear,
#ListingID
)

Linq to sql causing foreign key constraint errors

Here is the revelant code.
TournamentTeam newTeam = new TournamentTeam();
TournamentTeams.InsertOnSubmit(newTeam);
SubmitChanges();
TournamentParticipant newSignup = new TournamentParticipant
{
CheckedIn = false,
TournamentID = tournamentId,
UserID = participant.UserID,
TeamID = newTeam.TeamId
};
TournamentParticipants.InsertOnSubmit(newSignup);
SubmitChanges();
TournamentParticipants.TeamId has a fk relationship on TournamentTeam.TeamID, TeamID is an identity column
What I don't understand is that when TournamentTeam gets inserts, it grabs the new identity value. Even when I debug the code new Signup is recieving the new team id. But when it comes to generating the insert, it avoids this value completely and does the insert before the select statement where it grabs the new identity column.
exec sp_executesql N'INSERT INTO [dbo].[TournamentParticipants]([UserID], [TournamentID], [CheckedIn]) VALUES (#p0, #p1, #p2)
SELECT [t0].[TeamID] FROM [dbo].[TournamentParticipants] AS [t0] WHERE ([t0].[UserID] = #p3) AND ([t0].[TournamentID] = #p4)',N'#p0 int,#p1 int,#p2 bit,#p3 int,#p4 int',#p0=29805,#p1=247,#p2=0,#p3=29805,#p4=247
How can I either make linq to sql use the value for team id that I have specified or make the select statement be generated before the insert statement?
Instead of setting the TeamID, set the Team entity to the one you just created. Delay the SubmitChanges to insert both, then it will fix up the ids when the insert is done.
TournamentTeam newTeam = new TournamentTeam();
TournamentTeams.InsertOnSubmit(newTeam);
TournamentParticipant newSignup = new TournamentParticipant
{
CheckedIn = false,
TournamentID = tournamentId,
UserID = participant.UserID,
Team = newTeam
};
TournamentParticipants.InsertOnSubmit(newSignup);
SubmitChanges();