Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Value from Function 1

Status
Not open for further replies.

gpalmer711

IS-IT--Management
May 11, 2001
2,445
GB
This seems to be something that should be easy - however i've been googling this for ages and cannot find anything similar.

This is a continuation of thread183-1338691

I'm now trying to add another feature which will basically calculate whether the venue is open or not. I have this working fine within asp.net.

However what I would like to do is add another column to the data that is returned from the stored procedure that can be found in the code box below.

Code:
USE [searchforamenu]
GO
/****** Object:  StoredProcedure [dbo].[RetrieveVenues]    Script Date: 04/14/2007 01:42:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[RetrieveVenues] 
  @userlat decimal(9,6), 
  @userlong decimal(9,6), 
  @distance int = 5, 
  @takeaway bit = 0,
  @restaurant bit = 0,
  @pub bit = 0,
  @cafe bit = 0,
  @others bit = 0,
  @alcohol bit = 0,
  @playarea bit = 0,
  @pizza bit = 0,
  @chinese bit = 0,
  @indian bit = 0,
  @friedchicken bit = 0,
  @sandwiches bit = 0,
  @mexican bit = 0,
  @fish bit = 0,
  @burgers bit = 0,
  @babychange bit = 0,
  @bottlewarming bit = 0,
  @vegetarian bit = 0,
  @vegan bit = 0,
  @delivery bit = 0,
  @desserts bit = 0,
  @seafood bit = 0,
  @glutenfree bit = 0,
  @halal bit = 0,
  @kosher bit = 0,
  @italian bit = 0,
  @japanese bit = 0

as
Select *, 
       convert(decimal(4,2), dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)) as VenueDistance
From   menu_venues
Where  dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude) < @distance
		And
		(@TakeAway = 0 or [takeaway] = @takeaway)
		And
		(@restaurant = 0 or [restaurant] = @restaurant)
		And
		(@pub = 0 or [pub] = @pub)
		And
		(@cafe = 0 or [cafe] = @cafe)
		And
		(@others = 0 or [other] = @others)
		And
		(@alcohol = 0 or [alcohol] = @alcohol)
		And
		(@playarea = 0 or [playarea] = @playarea)
		And
		(@pizza = 0 or [pizza] = @pizza)
		And
		(@chinese = 0 or [chinese] = @chinese)
		And
		(@indian = 0 or [indian] = @indian)
		And
		(@friedchicken = 0 or [friedchicken] = @friedchicken)
		And
		(@sandwiches = 0 or [sandwiches] = @sandwiches)
		And
		(@mexican = 0 or [mexican] = @mexican)
		And
		(@fish = 0 or [fish] = @fish)
		And
		(@burgers = 0 or [burgers] = @burgers)
		And
		(@babychange = 0 or [babychange] = @babychange)
		And
		(@bottlewarming = 0 or [bottlewarming] = @bottlewarming)
		And
		(@vegetarian = 0 or [vegetarian] = @vegetarian)
		And
		(@vegan = 0 or [vegan] = @vegan)
And
		(@delivery = 0 or [delivery] = @delivery)
And
		(@desserts = 0 or [desserts] = @desserts)
And
		(@seafood = 0 or [seafood] = @seafood)
And
		(@glutenfree = 0 or [glutenfree] = @glutenfree)
And
		(@halal = 0 or [halal] = @halal)
And
		(@kosher = 0 or [kosher] = @kosher)
And
		(@italian = 0 or [italian] = @italian)
And
		(@japanese = 0 or [japanese] = @japanese)
Order By dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)

So what I would like to do is change this line

Code:
Select *, 
       convert(decimal(4,2), dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)) as VenueDistance

to

Code:
Select *, 
       convert(decimal(4,2), dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)) as VenueDistance,  dbo.fnCalculateOpen(@venueID) as VenueOpen

What I then want is the fnCalculateOpen to return the day of week and hour of the day, that is then checked to see if that venue is open on that day and time. Again I have the code for that working.

Code:
Declare @Day int

Set @Day = (DATEPART(dw, GETDATE()) + @@DATEFIRST + 5) % 7 + 1

Declare @mySQL varchar(100)

set @mySQL = 'select [' + convert(varchar(2),DATENAME (hour,GETDATE())) + '] from [menu_opening_hours] where [venue_ID] = ' + convert(varchar, @venueID) + 'AND day_of_week = ''' + convert(varchar(1),@Day) + ''''

exec(@mySQL)

This returns a single column and single row of data, I know that it will always return a single row and column of data.

What I cannot work out is how to get that single cell of data back to the original Stored Procedure.

I would really appreciate it if someone could point me in the right direction as it is driving me gaga.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
You can't use getdate() in a function. So you want to pass it from your query. But I would go a step beyond that. You need to clean up your table so that it doesn't have a column for each day (requiring you do do that dynamic SQL mess). From what I recall, you are early enough in the game to do this. Here's how I would have the table look (I only have put in datepart for today, for testing purposes. of course you'd have one row for each day/venueID pairing):

Code:
[COLOR=blue]create[/color] [COLOR=blue]table[/color] menu_opening_hours
(
VenueID [COLOR=blue]int[/color],
[COLOR=#FF00FF]DayOfWeek[/color] [COLOR=blue]int[/color],
HourOpen [COLOR=blue]int[/color],
HourClosed [COLOR=blue]int[/color]
)

[COLOR=blue]go[/color]

[COLOR=blue]insert[/color] menu_opening_hours
[COLOR=blue]select[/color] 1, 7, 9, 23 union all
[COLOR=blue]select[/color] 2, 7, 7, 9  union all
[COLOR=blue]select[/color] 3, 7, 10, 22

[COLOR=blue]go[/color]

This would allow for much easier querying in your function (I designed this to only return a bit value)

Code:
[COLOR=blue]Alter[/color] [COLOR=#FF00FF]Function[/color] GetOpen(@now [COLOR=#FF00FF]datetime[/color], @venueID [COLOR=blue]int[/color])
returns [COLOR=blue]bit[/color]
[COLOR=blue]as[/color]
[COLOR=blue]begin[/color]

[COLOR=blue]Declare[/color] @Day [COLOR=blue]int[/color], @Hour [COLOR=blue]int[/color], @Open [COLOR=blue]bit[/color]

[COLOR=blue]Set[/color] @Day = [COLOR=#FF00FF]datepart[/color](dw, @now)
[COLOR=blue]Set[/color] @Hour = [COLOR=#FF00FF]datepart[/color]([COLOR=#FF00FF]hour[/color], @now)

[COLOR=blue]select[/color] @Open = [COLOR=blue]CASE[/color] [COLOR=blue]WHEN[/color] (@Hour >= HourOpen and @Hour < HourClosed) [COLOR=blue]THEN[/color] 1
				[COLOR=blue]ELSE[/color] 0 [COLOR=blue]END[/color]
				[COLOR=blue]from[/color] [menu_opening_hours]
				[COLOR=blue]where[/color] [COLOR=#FF00FF]DayOfWeek[/color] = @day and VenueID = @venueID
[COLOR=blue]return[/color] @Open

[COLOR=blue]end[/color]

It can then be called like this:

Code:
[COLOR=blue]select[/color] venueID
, [COLOR=blue]Case[/color] dbo.GetOpen([COLOR=#FF00FF]getdate[/color](), venueID)
	[COLOR=blue]when[/color] 1 [COLOR=blue]then[/color] [COLOR=red]'Open'[/color]
	[COLOR=blue]else[/color] [COLOR=red]'Closed'[/color] [COLOR=blue]end[/color] [COLOR=blue]as[/color] OpenClosed
 [COLOR=blue]from[/color] menu_opening_hours

Post back if you have any questions.

I hope this helps,

Alex



Ignorance of certain subjects is a great part of wisdom
 
I may not be getting your process down but are you just looking for the "returns" in a UDF? That would answer the title of your thread anyhow. sorry, don't have much time to read the other thread.

e.g.
Code:
[COLOR=blue]create[/color] [COLOR=#FF00FF]function[/color] tst ()
returns [COLOR=blue]varchar[/color](10)
[COLOR=blue]as[/color]
[COLOR=blue]begin[/color]
[COLOR=blue]declare[/color] @var [COLOR=blue]varchar[/color](10)
[COLOR=blue]set[/color] @var = ([COLOR=blue]select[/color] top 1 col [COLOR=blue]from[/color] [COLOR=blue]table[/color])
[COLOR=blue]return[/color] @var
[COLOR=blue]end[/color]


____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done
 
Hi Alex,

Thanks for your response - I should really have added the table layout with you as well. It doesn't have a column for each day, rather a single column named day_of_week that contains an int of 1-7.

Code:
USE [searchforamenu]
GO
/****** Object:  Table [dbo].[menu_opening_hours]    Script Date: 04/14/2007 19:53:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[menu_opening_hours](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[venue_ID] [int] NOT NULL,
	[day_of_week] [int] NOT NULL,
	[0] [bit] NOT NULL,
	[1] [bit] NOT NULL,
	[2] [bit] NOT NULL,
	[3] [bit] NOT NULL,
	[4] [bit] NOT NULL,
	[5] [bit] NOT NULL,
	[6] [bit] NOT NULL,
	[7] [bit] NOT NULL,
	[8] [bit] NOT NULL,
	[9] [bit] NOT NULL,
	[10] [bit] NOT NULL,
	[11] [bit] NOT NULL,
	[12] [bit] NOT NULL,
	[13] [bit] NOT NULL,
	[14] [bit] NOT NULL,
	[15] [bit] NOT NULL,
	[16] [bit] NOT NULL,
	[17] [bit] NOT NULL,
	[18] [bit] NOT NULL,
	[19] [bit] NOT NULL,
	[20] [bit] NOT NULL,
	[21] [bit] NOT NULL,
	[22] [bit] NOT NULL,
	[23] [bit] NOT NULL,
 CONSTRAINT [PK_menu_opening_hours] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

The problem that I have is that there may be up to 3 seperate periods of opening hours that each venue could have in a 24 hour period. These opening hours may span into 2 days as well. So I figured having a single column for each hour would make sense. Each venue therefore has 7 rows in this table, 1 for each day.

Using the code I have above, it will return the single column and row that I need. I just can't figure how to get to that value from that single cell to return it.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Actually I think Alex has given me another Idea.

Basically to use the table layout he suggested, it doesn't matter how many different entries I have for each venue for a specific day based on that table and it gives me the flexability to take it further in the sense that I can bring the minutes into it.

I'll have a play around an post back with the solution I come up with.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
No that isn't going to work as I cannot have more that one entry per day as I thought in my last post.

Code:
ALTER Function [dbo].[DateTest99](@now datetime, @venueID int)
returns bit

as
begin

Declare @Day int, @Hour int, @Open bit

Set @Day = datepart(dw, @now)
Set @Hour = datepart(hour, @now)

select @Open = CASE WHEN (@Hour = 1) THEN 1
                ELSE 0 END
                from [menu_opening_hours]
                where day_of_week = @day and venue_ID = @venueID
return @Open

end

Is really what I am looking for - Where it says "CASE WHEN (@Hour = 1)"

I want the @Hour to be the variable value so that if it is 8pm @Hour will = 20.

As such making the code

Code:
ALTER Function [dbo].[DateTest99](@now datetime, @venueID int)
returns bit

as
begin

Declare @Day int, @Hour int, @Open bit

Set @Day = datepart(dw, @now)
Set @Hour = datepart(hour, @now)

select @Open = CASE WHEN ([20] = 1) THEN 1
                ELSE 0 END
                from [menu_opening_hours]
                where [day_of_week] = @day and [venue_ID] = @venueID
return @Open

end

The code above works perfectly if I manually enter the hour of the day, like I have 20.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
you would need to use dynamic SQL to change the column name in your select (where you have the [20]). I am really not sure if you can use dynamic SQL in this way though.

One advantage of using the table design that I posted is you are comparing the current hour to the same two columns each time (hourOpen and hourClosed). I think you would find this much easier to write queries against and maintain.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Hi Alex,

I totally agree with the fact that it would be easier to use your table design.

What I am unsure of if the SQL that I would need to use to accomplish what I need.

In Asp.net i'd be able to do it in a second but on the advice of both yourself and gmmastros i'm tring to use stored procedures and functions wherever possible.

So if we take your database layout as an example.

Lets say venue1 on a sunday is open from 12 midnight to 2am then 11am to 2pm and finally 6pm to 11pm.

The select query would return 3 rows, only one would return a 1 as correct. So in SQL Server how would I iterate through the results and see if one of them is a 1?

Sorry if this is really basic stuff, as soon as this project is done and i've made some money :) I will be doing a SQL course to try and get to grips with this side of things.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Ah, I can see how funky hours like that would give you fits. I think you'd be best to normalize your 'open hours' data with a table like this:

Code:
[COLOR=blue]Create[/color] [COLOR=blue]Table[/color] Venue_Open_Hours
(
venueID [COLOR=blue]int[/color],
weekday [COLOR=blue]int[/color],
OpenHour [COLOR=blue]int[/color]
)
You could then left join to this table on venueID, weekday, and
Code:
@Hour = OpenHour
. If you have a NULL value for OpenHour in the lookup table, that means it is closed. Does this make sense?

Hope it helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Ah I've got you so basically each venue would have a row for each hour that it is open.

That make perfect sense. I'll have a play around with this tonight and let you know how I get on.

Many thanks for you continued help with this.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
No problem Greg. Post back if you run into any questions (and also with whatever your solution ends up being).

One nice benefit of this approach is you don't need to use a 'GetOpen' function or anything like that. Everything can be accomplished in your query.

Good Luck,

ALex



Ignorance of certain subjects is a great part of wisdom
 
Hi Alex,

Finally had time to look into this.

Based on what you suggested this is what I came up with

Code:
USE [searchforamenu]
GO
/****** Object:  StoredProcedure [dbo].[RetrieveVenues]    Script Date: 04/16/2007 20:22:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[RetrieveVenues] 
  @userlat decimal(9,6), 
  @userlong decimal(9,6), 
  @distance int = 5, 
  @takeaway bit = 0,
  @restaurant bit = 0,
  @pub bit = 0,
  @cafe bit = 0,
  @others bit = 0,
  @alcohol bit = 0,
  @playarea bit = 0,
  @pizza bit = 0,
  @chinese bit = 0,
  @indian bit = 0,
  @friedchicken bit = 0,
  @sandwiches bit = 0,
  @mexican bit = 0,
  @fish bit = 0,
  @burgers bit = 0,
  @babychange bit = 0,
  @bottlewarming bit = 0,
  @vegetarian bit = 0,
  @vegan bit = 0,
  @delivery bit = 0,
  @desserts bit = 0,
  @seafood bit = 0,
  @glutenfree bit = 0,
  @halal bit = 0,
  @kosher bit = 0,
  @italian bit = 0,
  @japanese bit = 0

as

Declare @Day int, @Hour int, @now datetime

set @now = getdate()
Set @Day = datepart(dw, @now)
Set @Hour = datepart(hour, @now)

Select *, 
       convert(decimal(4,2), dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)) as VenueDistance, menu_venue_opening_hours.hour_open as VenueOpen
From   menu_venues
LEFT JOIN menu_venue_opening_hours
ON menu_venues.ID = menu_venue_opening_hours.venue_ID
Where  
		menu_venue_opening_hours.day_of_week = @Day	
		and
		menu_venue_opening_hours.hour_open = @Hour	
		and
		dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude) < @distance
		And
		(@TakeAway = 0 or [takeaway] = @takeaway)
		And
		(@restaurant = 0 or [restaurant] = @restaurant)
		And
		(@pub = 0 or [pub] = @pub)
		And
		(@cafe = 0 or [cafe] = @cafe)
		And
		(@others = 0 or [other] = @others)
		And
		(@alcohol = 0 or [alcohol] = @alcohol)
		And
		(@playarea = 0 or [playarea] = @playarea)
		And
		(@pizza = 0 or [pizza] = @pizza)
		And
		(@chinese = 0 or [chinese] = @chinese)
		And
		(@indian = 0 or [indian] = @indian)
		And
		(@friedchicken = 0 or [friedchicken] = @friedchicken)
		And
		(@sandwiches = 0 or [sandwiches] = @sandwiches)
		And
		(@mexican = 0 or [mexican] = @mexican)
		And
		(@fish = 0 or [fish] = @fish)
		And
		(@burgers = 0 or [burgers] = @burgers)
		And
		(@babychange = 0 or [babychange] = @babychange)
		And
		(@bottlewarming = 0 or [bottlewarming] = @bottlewarming)
		And
		(@vegetarian = 0 or [vegetarian] = @vegetarian)
		And
		(@vegan = 0 or [vegan] = @vegan)
And
		(@delivery = 0 or [delivery] = @delivery)
And
		(@desserts = 0 or [desserts] = @desserts)
And
		(@seafood = 0 or [seafood] = @seafood)
And
		(@glutenfree = 0 or [glutenfree] = @glutenfree)
And
		(@halal = 0 or [halal] = @halal)
And
		(@kosher = 0 or [kosher] = @kosher)
And
		(@italian = 0 or [italian] = @italian)
And
		(@japanese = 0 or [japanese] = @japanese)

Order By dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)

Which works really well.

I wonder if I could ask your opinion on something else though.

Basically the reason for me wanting to edit this stored procedure is that I wanted the users to be able to say whether they wanted open venues only or all venues.

If I use the code above it will only pick up the open venues, so do you think it would be best to maintain 2 procedures and choose which one to pick on the asp side.

Or use the following code that basically returns all venues and attaches the open hour if it is open?

Code:
USE [searchforamenu]
GO
/****** Object:  StoredProcedure [dbo].[RetrieveVenuesTest]    Script Date: 04/16/2007 20:31:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[RetrieveVenuesTest] 
  @userlat decimal(9,6), 
  @userlong decimal(9,6), 
  @distance int = 5, 
  @takeaway bit = 0,
  @restaurant bit = 0,
  @pub bit = 0,
  @cafe bit = 0,
  @others bit = 0,
  @alcohol bit = 0,
  @playarea bit = 0,
  @pizza bit = 0,
  @chinese bit = 0,
  @indian bit = 0,
  @friedchicken bit = 0,
  @sandwiches bit = 0,
  @mexican bit = 0,
  @fish bit = 0,
  @burgers bit = 0,
  @babychange bit = 0,
  @bottlewarming bit = 0,
  @vegetarian bit = 0,
  @vegan bit = 0,
  @delivery bit = 0,
  @desserts bit = 0,
  @seafood bit = 0,
  @glutenfree bit = 0,
  @halal bit = 0,
  @kosher bit = 0,
  @italian bit = 0,
  @japanese bit = 0

as

Declare @Day int, @Hour int, @now datetime

set @now = getdate()
Set @Day = datepart(dw, @now)
Set @Hour = datepart(hour, @now)

Select *, 
       convert(decimal(4,2), dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)) as VenueDistance
From   menu_venues, menu_venue_opening_hours

Where  
		menu_venues.ID = menu_venue_opening_hours.venue_ID
		AND
		menu_venue_opening_hours.day_of_week = @Day	
		and
		menu_venue_opening_hours.hour_open = @Hour	
		and
		dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude) < @distance
		And
		(@TakeAway = 0 or [takeaway] = @takeaway)
		And
		(@restaurant = 0 or [restaurant] = @restaurant)
		And
		(@pub = 0 or [pub] = @pub)
		And
		(@cafe = 0 or [cafe] = @cafe)
		And
		(@others = 0 or [other] = @others)
		And
		(@alcohol = 0 or [alcohol] = @alcohol)
		And
		(@playarea = 0 or [playarea] = @playarea)
		And
		(@pizza = 0 or [pizza] = @pizza)
		And
		(@chinese = 0 or [chinese] = @chinese)
		And
		(@indian = 0 or [indian] = @indian)
		And
		(@friedchicken = 0 or [friedchicken] = @friedchicken)
		And
		(@sandwiches = 0 or [sandwiches] = @sandwiches)
		And
		(@mexican = 0 or [mexican] = @mexican)
		And
		(@fish = 0 or [fish] = @fish)
		And
		(@burgers = 0 or [burgers] = @burgers)
		And
		(@babychange = 0 or [babychange] = @babychange)
		And
		(@bottlewarming = 0 or [bottlewarming] = @bottlewarming)
		And
		(@vegetarian = 0 or [vegetarian] = @vegetarian)
		And
		(@vegan = 0 or [vegan] = @vegan)
And
		(@delivery = 0 or [delivery] = @delivery)
And
		(@desserts = 0 or [desserts] = @desserts)
And
		(@seafood = 0 or [seafood] = @seafood)
And
		(@glutenfree = 0 or [glutenfree] = @glutenfree)
And
		(@halal = 0 or [halal] = @halal)
And
		(@kosher = 0 or [kosher] = @kosher)
And
		(@italian = 0 or [italian] = @italian)
And
		(@japanese = 0 or [japanese] = @japanese)

Order By dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)

Once again thanks for you help - as always I have gained some more knowledge from the masters.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
I figured you would want a choice. Consider what you have now:

Code:
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] menu_venue_opening_hours
[COLOR=blue]ON[/color] menu_venues.ID = menu_venue_opening_hours.venue_ID
[COLOR=blue]Where[/color]  
        menu_venue_opening_hours.day_of_week = @Day    
        and
        menu_venue_opening_hours.hour_open = @Hour

notice those first two WHERE conditions especially.

Now, try changing that part to look like this:

Code:
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] menu_venue_opening_hours
[COLOR=blue]ON[/color] menu_venues.ID = menu_venue_opening_hours.venue_ID
AND
        menu_venue_opening_hours.day_of_week = @Day    
AND
        menu_venue_opening_hours.hour_open = @Hour    
[COLOR=blue]WHERE[/color] ...

First thing this will do is limit your result set to one row per venue. With these join conditions, you are going to have NULL values for ANYTHING on the right side of the join (but everything will be returned).

So if you add a parameter like this:

Code:
@OpenOnly [COLOR=blue]bit[/color] = 0

You could add a WHERE clause like this:

Code:
[COLOR=blue]WHERE[/color] (menu_venue_opening_hours.hour_open [COLOR=blue]is[/color] not null OR @OpenOnly = 0)

Because its' a left join, you need to repeat this condition in your where clause (because checking to see if hour_open is not null in the where clause will cause your query to return only the open venues, and the OR @OpenOnly = 0 will cause all rows to be returned (even the closed ones) if the parameter is not set to 1)

Am I making sense, I feel like I'm kind of rambling...

Also, you post some pretty long code sometimes, I think that this might be helpful to you in making it easier to read: George wrote this program and was kind enough to share it, and it makes it super easy to add color-coding to your posts.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Hi Alex,

You were not rambling at all I really appreciate all the help you have given me with this project, I wish there was some other way I could thank you as you have saved me litterally days of trial and error getting this to work correctly.

Also thanks for the heads up with the SQL to TGML - i've put it to use below [bigsmile]

Code:
[COLOR=blue]USE[/color] [searchforamenu]
[COLOR=blue]GO[/color]
[COLOR=green]/****** Object:  StoredProcedure [dbo].[RetrieveVenues]    Script Date: 04/18/2007 11:18:59 ******/[/color]
[COLOR=blue]SET[/color] ANSI_NULLS [COLOR=blue]ON[/color]
[COLOR=blue]GO[/color]
[COLOR=blue]SET[/color] [COLOR=#FF00FF]QUOTED_IDENTIFIER[/color] [COLOR=blue]ON[/color]
[COLOR=blue]GO[/color]
[COLOR=blue]ALTER[/color] [COLOR=blue]Procedure[/color] [dbo].[RetrieveVenues] 
  @userlat [COLOR=blue]decimal[/color](9,6), 
  @userlong [COLOR=blue]decimal[/color](9,6), 
  @distance [COLOR=blue]int[/color] = 5, 
  @takeaway [COLOR=blue]bit[/color] = 0,
  @restaurant [COLOR=blue]bit[/color] = 0,
  @pub [COLOR=blue]bit[/color] = 0,
  @cafe [COLOR=blue]bit[/color] = 0,
  @others [COLOR=blue]bit[/color] = 0,
  @alcohol [COLOR=blue]bit[/color] = 0,
  @playarea [COLOR=blue]bit[/color] = 0,
  @pizza [COLOR=blue]bit[/color] = 0,
  @chinese [COLOR=blue]bit[/color] = 0,
  @indian [COLOR=blue]bit[/color] = 0,
  @friedchicken [COLOR=blue]bit[/color] = 0,
  @sandwiches [COLOR=blue]bit[/color] = 0,
  @mexican [COLOR=blue]bit[/color] = 0,
  @fish [COLOR=blue]bit[/color] = 0,
  @burgers [COLOR=blue]bit[/color] = 0,
  @babychange [COLOR=blue]bit[/color] = 0,
  @bottlewarming [COLOR=blue]bit[/color] = 0,
  @vegetarian [COLOR=blue]bit[/color] = 0,
  @vegan [COLOR=blue]bit[/color] = 0,
  @delivery [COLOR=blue]bit[/color] = 0,
  @desserts [COLOR=blue]bit[/color] = 0,
  @seafood [COLOR=blue]bit[/color] = 0,
  @glutenfree [COLOR=blue]bit[/color] = 0,
  @halal [COLOR=blue]bit[/color] = 0,
  @kosher [COLOR=blue]bit[/color] = 0,
  @italian [COLOR=blue]bit[/color] = 0,
  @japanese [COLOR=blue]bit[/color] = 0,
  @OpenOnly [COLOR=blue]bit[/color] = 0

[COLOR=blue]as[/color]

[COLOR=blue]Declare[/color] @Day [COLOR=blue]int[/color], @Hour [COLOR=blue]int[/color], @now [COLOR=#FF00FF]datetime[/color]

[COLOR=blue]set[/color] @now = [COLOR=#FF00FF]getdate[/color]()
[COLOR=blue]Set[/color] @Day = [COLOR=#FF00FF]datepart[/color](dw, @now)
[COLOR=blue]Set[/color] @Hour = [COLOR=#FF00FF]datepart[/color]([COLOR=#FF00FF]hour[/color], @now)

[COLOR=blue]Select[/color] *, 
       [COLOR=#FF00FF]convert[/color]([COLOR=blue]decimal[/color](4,2), dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)) [COLOR=blue]as[/color] VenueDistance, menu_venue_opening_hours.hour_open [COLOR=blue]as[/color] VenueOpen
[COLOR=blue]From[/color]   menu_venues
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] menu_venue_opening_hours
[COLOR=blue]ON[/color] menu_venues.ID = menu_venue_opening_hours.venue_ID
		and
		menu_venue_opening_hours.day_of_week = @Day	
		and
		menu_venue_opening_hours.hour_open = @Hour	
[COLOR=blue]Where[/color]
		dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude) < @distance
		And
		(menu_venue_opening_hours.hour_open [COLOR=blue]is[/color] not null OR @OpenOnly = 0)
		And
		(@TakeAway = 0 or [takeaway] = @takeaway)
		And
		(@restaurant = 0 or [restaurant] = @restaurant)
		And
		(@pub = 0 or [pub] = @pub)
		And
		(@cafe = 0 or [cafe] = @cafe)
		And
		(@others = 0 or [other] = @others)
		And
		(@alcohol = 0 or [alcohol] = @alcohol)
		And
		(@playarea = 0 or [playarea] = @playarea)
		And
		(@pizza = 0 or [pizza] = @pizza)
		And
		(@chinese = 0 or [chinese] = @chinese)
		And
		(@indian = 0 or [indian] = @indian)
		And
		(@friedchicken = 0 or [friedchicken] = @friedchicken)
		And
		(@sandwiches = 0 or [sandwiches] = @sandwiches)
		And
		(@mexican = 0 or [mexican] = @mexican)
		And
		(@fish = 0 or [fish] = @fish)
		And
		(@burgers = 0 or [burgers] = @burgers)
		And
		(@babychange = 0 or [babychange] = @babychange)
		And
		(@bottlewarming = 0 or [bottlewarming] = @bottlewarming)
		And
		(@vegetarian = 0 or [vegetarian] = @vegetarian)
		And
		(@vegan = 0 or [vegan] = @vegan)
And
		(@delivery = 0 or [delivery] = @delivery)
And
		(@desserts = 0 or [desserts] = @desserts)
And
		(@seafood = 0 or [seafood] = @seafood)
And
		(@glutenfree = 0 or [glutenfree] = @glutenfree)
And
		(@halal = 0 or [halal] = @halal)
And
		(@kosher = 0 or [kosher] = @kosher)
And
		(@italian = 0 or [italian] = @italian)
And
		(@japanese = 0 or [japanese] = @japanese)

[COLOR=blue]Order[/color] [COLOR=blue]By[/color] dbo.fnCalculateDistance(@userlong, @userlat, menu_venues.longitude, menu_venues.latitude)

So that is my final code and works perfectly for me.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Greg - There is no need for any additional thanks. Maybe post a link when your site is up though, your project looks very interesting. I am glad to have been able to help you with it. Many others on this site have done the same for me :)

Also, doesn't SQL2TGML make things look nice :-D

Ignorance of certain subjects is a great part of wisdom
 
AlexCuse said:
Maybe post a link when your site is up though, your project looks very interesting.

I sure will do.

AlexCuse said:
Also, doesn't SQL2TGML make things look nicequote]

So much easier to read, i'll be sure to use it in the future.

Incidently are there any resources, books or courses you would recommend for me to become a bit more proficient in this sort of thing? Should I go specifically for SQL Server resources or something more generic?

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Books Online, found in SQL Server help, is a great resource.

I consider The Guru's Guide To Transact-SQL an essential addition to anyone's shelf.

If you want to learn more about SQL Server's guts, the Inside SQL Server series (by Kalen Delaney) is really good, at least for 2000.

Also, I have heard great things about this book, which would be helpful if you are using 2005.

There is actually an FAQ's on books found here: (but I couldn't help but plug a few of my favorites ;-) )

Good Luck!

Alex

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top