0

MySQL Gurus,

I'm converting some reports from a MSSQL database for usage on a MySQL Database, and don't seem to understand how the DECLARE works in MySQL. Below is the SQL code for the report, as works in MSSQL. I read that DECLARE can only be use in a nested function, I belive, but that just does not sound right to me.

Current Report SQL: (I parse & replace the values of Current & Pending from my app code)

DECLARE @Current int;
DECLARE @Pending int;

SET @Current = [1];
SET @Pending = [3];

Select Ticket.TIcketID,
ISNULL((Select LocationName from Location where LocationID = Ticket.SiteCurrentLocation), 'Invalid Location') as [Current Location],
ISNULL((Select LocationName from Location where LocationID = Ticket.SitePendingLocation), 'Invalid Location') as [Pending Location]
from Ticket

where 
(SitePendingLocation > 0 AND SitePendingLocation <> SiteCurrentLocation) AND
(SiteCurrentLocation = @Current OR @Current = 0) AND
(SitePendingLocation = @Pending OR @Current = 0)

Any insight?

Thanks - Andrew

EDIT

Working, converted script - that it may help others:

SET @Current = '1';
SET @Pending = '1';

Select Ticket.TIcketID,
IFNULL((Select LocationName from Location where LocationID = Ticket.SiteCurrentLocation), 'Invalid Location') as `Current Location`,
IFNULL((Select LocationName from Location where LocationID = Ticket.SitePendingLocation), 'Invalid Location') as `Pending Location`
from Ticket

where 
(SitePendingLocation > 0 AND SitePendingLocation <> SiteCurrentLocation) AND
(SiteCurrentLocation = @Current OR @Current = 0) AND
(SitePendingLocation = @Pending OR @Current = 0)
Fuginator
  • 229
  • 3
  • 5
  • 15
  • 1
    You can either use SET by itself (no DECLARE) or replace @ with _ (or no prefix). See http://stackoverflow.com/questions/763718/whats-wrong-with-this-mysql-statement-declare-id-int for a similar question. – dash Dec 14 '11 at 00:24
  • Thanks @dash - no need for the `DECLARE` at all then, great! Now off to figure out what it does not like about `ISNULL`... – Fuginator Dec 14 '11 at 00:27
  • It's IFNULL ;-) - http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull – dash Dec 14 '11 at 00:30
  • @dash, please 'answer' the question below with your first comment so I can accept it, thanks :) – Fuginator Dec 14 '11 at 00:34

1 Answers1

1

You can either use SET by itself (no DECLARE) or replace @ with _ or similar (or even no prefix).

I generally prefix mine with _

See What's wrong with this MySQL statement: DECLARE @ID INT for a similar question

As regards your other comment, it's IFNULL in MySql - See http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull

It's always the little things... :-)

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71