开发者

Custom "WHERE" in stored procedure (Informix)?

开发者_如何学JAVAI'm doing the specification for an application that has to search a table with different conditions in the WHERE clause.

For example (not an actual table):

type 1

select name from employees where active = true;

or type 2

select name from employees where idBoss = 3;

I would rather create one stored procedure with a parameter "type" than create two stored procedures with the same code and different "where" clauses.

Is this possible?

Note: I have 2 programmers; one knows only Informix, one knows only .NET. To minimize problems, I'm doing all the calls to the database with stored procedures so the db programmer and the .net programmer don't have to need each other.


You can use dynamic SQL in informix - see this link for more details.

DEFINE v_sql VARCHAR(250);

LET v_sql = "select name from employees";

IF IN_PARAMETER = 1 THEN
   LET v_sql = v_sql || " WHERE active = true"
ELSE
   LET v_sql = v_sql || " WHERE idboss = 3"
END IF;

PREPARE stmt FROM v_sql;
EXECUTE stmt;

FREE stmt;

If you can't use dynamic SQL, the next best thing would be:

IF IN_PARAMETER = 1 THEN
   select name from employees WHERE active = true;
ELSE
   select name from employees WHERE idboss = 3;
END IF;


Why create a stored procedure for something this short?

Consider using Linq. Where is just a filter which takes a lambda/function which returns true/false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜