From the question, it looks like what you have written quasi-resembles what Oracle calls an anonymous code block. MySQL does not facilitate such a mechanism.
With regard to what you want to accomplish, you do not need a stored procedure.
Try assembling the SQL statement like this:
@stmt = 'SELECT sid, ifnull(pricelist,"BASE"), count(*) AS recs FROM buyerList AS b ';
@stmt = CONCAT(@stmt,'LEFT JOIN sellerList AS s ON s.sid = b.sid AND s.pass = b.pass ');
@stmt = CONCAT(@stmt,'LEFT JOIN pricelists p ON ');
SELECT GROUP_CONCAT(CONCAT('(p.sid = a.sid AND p.preisliste = ',sid,' AND p.ean = a.ean AND p.iln = ',pricelist',)') SEPARATOR ' OR ')
INTO @LeftJoinClause FROM preislisten;
@stmt = CONCAT(@stmt,@LeftJoinClause,' WHERE b.bid = ?');
SELECT @stmt\G
This will print out the desired query
If it is the desired query, execute it
PREPARE sql FROM @stmt;
EXECUTE sql using @param_iln;
DEALLOCATE PREPARE sql;
Give it a Try !!!
If you actually want the query to be small without hardcoding every value just write the code with a more straightforward LEFT JOIN setup
SELECT p.sid, ifnull(p.pricelist,"BASE"), count(*) AS recs FROM buyerList AS b
LEFT JOIN sellerList AS s ON s.sid = b.sid AND s.pass = b.pass
LEFT JOIN pricelists p ON
(p.sid = a.sid AND p.preisliste = a.sid AND p.ean = a.ean)
WHERE b.bid = ?
GROUP BY p.sid,p.pricelist;
I also just noticed you have a.sid and a.ean, what table has an alias of a
?
Deadlocking by SELECTs can be done in a variety of ways. I have written posts about them
- You can have SELECTs get deadlocked by UPDATEs and DELETEs
- You can have UPDATEs and DELETEs blocked by SELECTs
- You can just lock the table before the SELECT and unlock it afterwards
LOCK TABLES table READ;
SELECT ... ;
UNLOCK TABLES;
- You could perform a large UPDATE in a transaction with the SERIALIZEABLE isoaltion level. The MySQL Documentation on
SERIALIZEABLE isoaltion level
says:
This level is like REPEATABLE READ, but InnoDB implicitly converts all plain SELECT statements to SELECT ... LOCK IN SHARE MODE if autocommit is disabled. If autocommit is enabled, the SELECT is its own transaction. It therefore is known to be read only and can be serialized if performed as a consistent (nonlocking) read and need not block for other transactions. (To force a plain SELECT to block if other transactions have modified the selected rows, disable autocommit.)
Cutting right to the chase, you could just use
- SELECT ... FOR UPDATE, then try
SELECT
against those rows
- SELECT ... LOCK IN SHARE MODE, then try to
UPDATE
or DELETE
those rows
- See the MySQL Documentation
Give it a Try !!!
Best Answer
Some RDBMS (SQL Server) allow you to get this information easily. Especially for a direct DB connection from code running in the user's session on Windows, using Windows Auth to connect to SQL Server.
It can be done if the user connects to a website, which then connects to SQL Server too.
However, you're on MySQL.
Your simplest option is to have the client code send it in, derived from the OS. Ypu can then pass this in to the trigger as per this SO answer https://stackoverflow.com/questions/324605/mysql-trigger-storing-a-select-in-a-variable