MySQL Forums
Forum List  »  Microsoft SQL Server

Re: Calling stored procedures ... Dynamic SQL
Posted by: Michael Joss
Date: March 08, 2006 11:53PM

Hi Perry,

Might just be that particular example that you chose but in that case you could simply do the same thing using the CASE statement rather than going to the trouble of using dynamic SQL.

DELIMITER $$;

DROP PROCEDURE IF EXISTS `TestDynamicSQL`$$

CREATE PROCEDURE `TestDynamicSQL` (
_FromID INT,
_ToID INT)
BEGIN

SELECT *
FROM testDynamic
WHERE
systemID >= CASE WHEN @fromID IS NULL THEN systemID ELSE @fromID END
AND systemID <= CASE WHEN @toID IS NULL THEN systemID ELSE @toID END end;

END$$

DELIMITER ;$$

would return exactly the same thing (probably quicker) and certainly a lot easier to follow? In MSSQL you could even use ISNULL() instead of the whole case statement. Of course a simple > or < rather than >= or <= would make your life more difficult (though not impossible) using this method.

Hope thats helpful

Cheers

MJ

Options: ReplyQuote


Subject
Written By
Posted
Re: Calling stored procedures ... Dynamic SQL
March 08, 2006 11:53PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.