Something I learned today about database views

The only way to filter or use parameters on a view, is to use unconverted select fields.

For example; if you do the following in a view:
” Select CONVERT(char, dbo.tblStays.DateStart, 101) AS ‘Stay Start’, CONVERT(char, dbo.tblStays.DateStop, 101)  AS ‘Stay Stop’ ”
Then you can’t do “WHERE [Placement Start] > 2008-09-09″ in the query of the view. This is because the value of [Placement Start] is esssentially a string and the value of dbo.tblStays.DateStart is a datetime datatype and they don’t talk to each other except through some kind of recasting, and recasting in .NET means several lines of code and function calls that can make your code long, complicated, and unstructured. If I can break my code into discrete structures, functions and procedures, to me it makes it simpler and less complicated and I’m all for the simple life.
  So in keeping with persuing the simple life, I chose a stored procedure that uses datetime parameter datatypes and selects for the unconverted dbo.tblStays.DateStart. This way you can send a datetime and get back a nice safe string.

Post to Twitter Tweet This Post

  • Share/Bookmark

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WP Hashcash