Answer the question
In order to leave comments, you need to log in
What are the ways to dynamically generate conditions in WHERE?
Hello!
There is an html page on which the user specifies the necessary parameters for fetching data and a set of servlets that process the post request from the page. The essence of processing is reduced to the formation and execution of a query to the database. Depending on the selection/not selection of various options, the WHERE clause in the query changes.
What are the ways to dynamically generate conditions in WHERE (taking into account the fact that the requirements for the page and search conditions will increase in the future)?
Answer the question
In order to leave comments, you need to log in
To be honest, I am not familiar with java, but in sql you can form a dynamic query in nvarchar and then run it in different ways, for example
DECLARE @sql_exec NVARCHAR(4000), @EOL CHAR(2)
SELECT @EOL = CHAR(13) + CHAR(10)
SELECT @sql_exec = 'SELECT 1' + @EOL
-- тут добавить условия
-- формирования динамического запроса
--
SELECT @sql_exec = @sql_exec + 'WHERE 1=1'
-- тут добавить
-- формирование условий выборки
--
-- просмотр сформированного запроса
PRINT @sql_exec
-- выполнение:
EXEC(@sql_exec)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question