How to make this SQL Query more dynamic and linear?
I have a view named VIEW_FORM_SALE_SUBMISSION_INFO
I have query as
select a.FormSl
from VIEW_FORM_SALE_SUBMISSION_INFO as a
where FormSl between convert(int,'113990') and convert(int,'1131000')
My objective:
- I want to pass these two values inside the
convert()
function as a parameter - I want this query to be more linear, i.e. can I make these convertions earlier to this query so that I can use between operator without as si开发者_StackOverflow中文版mple as possible?
How about this?
select a.FormSl
from VIEW_FORM_SALE_SUBMISSION_INFO as a
where FormSl between @param1 and @param2
Where @param1
and @param2
are integer parameters?
To parameterise this, you could make a table valued function.
CREATE FUNCTION myFunction (@lower_bound AS VARCHAR(32), @upper_bound AS VARCHAR(32))
RETURNS TABLE AS
RETURN
select a.FormSl
from VIEW_FORM_SALE_SUBMISSION_INFO as a
where FormSl between convert(int,@lower_bound) and convert(int,@upper_bound)
SELECT * FROM dbo.myFunction('11111', '22222')
In terms of optimising the Convert, what you have is as optimal as it will get.
If you want to improve it further, don't pass in strings, pass int integers in the first place.
精彩评论