2
declare @Date DateTime
set @Date='2012-04-16'
select s.sid,'Status'=case a.sid when isnull(a.sid,null)   
then 'absent' else 'present' end from
student s left outer join (select * from absent where date=@Date) as a
on s.sid=a.sid

I have a sql query like this, and I need to create a view with this one.....is it possible..

I created a function for this like

CREATE FUNCTION dbo.Attendance (@Date DateTime)
RETURNS TABLE
AS
RETURN
(
    select s.sid,'Status'=case a.sid when isnull(a.sid,null)   
    then 'absent' else 'present' end from
    student s left outer join (select * from absent where date=@Date) as a
    on s.sid=a.sid

)

the view is created successfully....but when I call the view like

 select * from dbo.Attendance('2012-04-11')

it reports error like "Conversion failed when converting date and/or time from character string."......how can I call this function

7
  • 3
    not really - views don't have parameters... you could only do the join in the view and use the parameter in the where when you query the view... Commented Apr 16, 2012 at 4:43
  • 1
    You want to assign parameter value at runtime, create Stored Procedure either Commented Apr 16, 2012 at 4:45
  • 1
    you will need to cast the string to a date,cast(@Date as date) you should also explicitly check the date makes sense and raise an error if it doesn't. Commented Apr 16, 2012 at 5:48
  • 1
    BEGIN TRY cast(@Date as date) END TRY BEGIN CATCH RAISERROR(' The supplied date was not recognised as valid!',16,1) END CATCH; N.B. look up raiserror for all options which will depend on your implementation Commented Apr 16, 2012 at 5:54
  • 1
    after the AS, declare a new variable of type date, call it \@dDate then do the try from the line above followed by the \@dDate = cast(\@Date as date) & use \@dDate in your query Commented Apr 16, 2012 at 5:59

3 Answers 3

2

paramerterised views are allowed in MS Access but no serious db server will allow you to construct a view that requires a mandatory parameter.

your example describes a stored procedure which should be the implementation of choice for your requirement.

another way of meeting your requirement would be to keep your @Date parameter in another table, provided it's there the view can refer to that and will return a row if the criteria is met

Sign up to request clarification or add additional context in comments.

3 Comments

thanks Paddy Carroll...I dont understand how to keep the @Date in another table, can u give me the query sample
create table application_parameters( param_name varchar,param_value varchar); insert into application_parameters (param_name,param_value) values ('my_app_date','2012-04-16');
select s.sid,'Status'=case a.sid when isnull(a.sid,null) then 'absent' else 'present' end from student s left outer join (select * from absent ,application_paramaters where param_name = 'my_app_date' and date = cast(my_param_value as Date)) as a on s.sid=a.sid ----not checked but you get the idea
2

You cannot create a parametrized view atleast in SQL server rather use a table function.

Please refer Create parameterized VIEW in SQL Server 2008 for more details.

Hope it will give you an idea.

1 Comment

Try taking '2012-04-11' into a datetime variable then use select * from dbo.Attendance(@myDate). I hope after this modification it should work
1

View with Parameter is possible as per following info. Please visit following link:

https://msdn.microsoft.com/library/c11kd98s(v=vs.80).aspx

CREATE SQL VIEW Customer_Remote_View ;
   AS SELECT * FROM Customers WHERE Customers.Country = ?cCountry

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.