Solved

Recurrence for Calendar using MS SQL Server

Posted on 2009-02-14
2
418 Views
Last Modified: 2012-05-06
Hi,
I need help =) I'm very, very, very new to Sql (and very, very, very confused),
 so forgive me if I don't say things properly.  

I have a calendar of events and I want to be able to allow a user to input recurring events.  
I've been hunting around to try to figure out the best method to solve this problem.

From searching around on this site, I found some soltions (to different problems)
that used User Defined Functions, so I thought maybe this would be the route that I should take.

My simplified database schema is as follows:
(table Name = dbo.Events)

EventID      int
CustomerID      int
EventTitle      string
BeginningDate      DateTime
EndingDate      DateTime
Reccurence      int

The Recurrence has the following values

Reccurence = 0 :  No reccurence
Reccurence = 1: Event Reccurs on a weekly basis
Reccurence = 2: Event Reccurs on a monthly basis
Reccurence = 3: Event Reccurs on a yearly basis


I need to be able to retrieve all of the events for a customer.  If the event reccurence is 0, then it's
just a normal retrieval, but if it anything else, then I need to populate a table and join it to the
other results, which is why I thought that User Defined Functions would be a good solution.

After fighting with UDFs for the past 3 hours, I ran into soooo many problems.

1) Is it possible to create UDFs from MS Visual Studio 2005? I ask this becasue I couldn't even get the scalar UDF to work.

2) None of the example that I found showed how to pass in a variable from outside the function...kind of like a stored procedure.

what I mean is this

      fEvent(@CustomerID) is the parameter for the UDF

when I call the function in the SELECT STATEMENT

i.e.
      SELECT CustomerID, EventTitle, BeginningDate, EndingDate
      FROM dbo.Events E
      CROSS APPLY fEvent(E.CustomerID)
      ORDER BY BeginningDate


      What if I don't want to hardcode in the E.CustomerID.  It's dependent on which Customer I looking up.
      How can I do this?

3) CROSS comes up as a keyword for me but APPLY doesn't.  Any thougths as to why that is?  I'm using Visual Studio 2005 Professional
and MS Sql Server Express

4) Is this the best method to achieving my goal?  The only other was that I thought of was to have
a seperate entry for each occurence, but this would generate tons of records, and produce a lot of hits
on my database.  


Thank you so much any help!!!!  
0
Comment
Question by:tenderroni992003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 23646541
1. It will definitely work. Might be there is some other issue in the function

2. Sure. You can Create it. An example below:

CREATE FUNCTION Sales.ufn_CustomerNamesInRegion
                 ( @Region nvarchar(50) )
RETURNS table
AS
RETURN (
        SELECT DISTINCT S.Name AS Store, A.City
        FROM Sales.Store AS S
        JOIN Sales.CustomerAddress AS CA ON CA.CustomerID = S.CustomerID
        JOIN Person.Address AS A ON A.AddressID = CA.AddressID
        JOIN Person.StateProvince SP ON
        SP.StateProvinceID = A.StateProvinceID
        WHERE SP.Name = @Region
       )
GO
-- Example of calling the function for a specific region
SELECT *
FROM Sales.ufn_CustomerNamesInRegion(N'Washington')
GO

And in your case, Since it is a Scalar value you can also select like

      SELECT CustomerID, EventTitle, BeginningDate, EndingDate,fEvent(E.CustomerID)
      FROM dbo.Events E
      ORDER BY BeginningDate

3. APPLY is also a keyword and it will be used for returning the result sets of Table Valued Functions.

4. If you have problems with Functions, you can try Procedures where in you can implement all sorts of functionality without any hassles.
0
 

Author Closing Comment

by:tenderroni992003
ID: 31547000
Thank you so much for all of your help!  This led me in the right direction. Thank you thank you thank you!!!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question