October 18, 2007 at 12:10 pm
I have a view definition as below.
create view dbo.view1 as
select * from table1
where date1 >= '2007-01-01' or
date2 >= '2007-01-01' or
date3 >= '2007-01-01'
I'd like to define the date value '2007-01-01' as a constant and reframe the query like this :
create view dbo.view1 as
select * from table1
where date1 >= @dateConstant or
date2 >= @dateConstant or
date3 >= @dateConstant
I'd like to know how and where to define this @dateConstant and whether or not it's possible to do so.
October 18, 2007 at 12:25 pm
i might be second guessing you, but i believe you want to change the date to a PARAMETER, and not a CONSTANT, right?
If my re-interpretation is right, then you want to use a stored procedure (or a function that returns a table) instead:
Create Procedure prGetView1 (@dateConstant datetime)
As
begin
SET NOCOUNT ON
select * from table1
where date1 >= @dateConstant or
date2 >= @dateConstant or
date3 >= @dateConstant
END
GO
Lowell
October 18, 2007 at 12:49 pm
I want to use a constant for comparision in the where clause of the query used in view.
I tried using CTE like this but this too fails as after myCTE it expects 'ON':
create view dbo.view1 as
with myCTE as( select cast('20070101' as dateTime) as dateConstant )
select * from table1 t1 inner join table2 t2 on t1.joinCol1 = t2.joinCol2
inner join myCTE
where date1 >= dateConstant or
date2 >= dateConstant or
date3 >= dateConstant
October 18, 2007 at 7:48 pm
Another name for "View with parameters" is "Table function".
_____________
Code for TallyGenerator
October 19, 2007 at 12:54 am
I think Manoj wants a constant. Not a parameter.
Best Regards,
Chris Büttner
October 19, 2007 at 4:48 am
the way to do that would be with a scalar function then:
Create Function dateConstant ()
returns datetime
AS
begin
return '01/01/2007'
end
select dbo.dateConstant()
Lowell
October 19, 2007 at 5:11 am
Or use a CTE as described here:
http://www.sqlservercentral.com/Forums/Topic412364-145-1.aspx#bm412471
Best Regards,
Chris Büttner
October 20, 2007 at 6:27 pm
:blink::ermm::Whistling: The constant is also a LITERAL in this case... it's not like they're selecting it or anything... the view is simple...
create view dbo.view1 as
select * from table1 t1 inner join table2 t2 on t1.joinCol1 = t2.joinCol2
inner join myCTE
where date1 >= '20070101' or
date2 >= '20070101' or
date3 >= '20070101'
...takes less code, too! 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply