Do not use on any
shared computer
August 30, 2008 04:44am pdt
 
[x]
Attachment Details

Banging Head Transitioning from Access Queries to T-SQL in SQL 2005

Tags: Microsoft, SQL, 2005
In MS Access you build queries upon queries.  You can put in parameters so you can pass user or program input into the queries.  This where my problem is, building Views upon other Views in SQL with parameters.  I know that Views cannot have parameters so you should use Stored Procedures.  I am used to building complex queries in Access but I am stuck in SQL.  For example I have a list of Employees with Punch In and Out dates with times.   So I wrote a stored procedure below.  This returns the values I need for the next step.  Now I need to take that data (Q1) and do something to it and get (Q2) and then I need to take (Q1) and outer join it to (Q2) and get (Q3).  Then I need to take (Q3) and do something to that and get (Q4).  This was very easy to do in Access.  If I have to construct some long text based solution in a stored procedure I will go nuts.  How can I get the initial data I need with parameters and then continue with Views so I can build upon each one?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
********** STORED PROCEDURE **********
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[uspTmDiff] 
	@DateFrom DateTime = Null, 
	@DateTo DateTime = Null
AS
BEGIN
SET NOCOUNT ON;
	SELECT
	dbo.TimeClock.*,
	DATEPART(yyyy, Dt) * 10000 + DATEPART(mm, Dt) * 100 + DATEPART(dd, Dt) AS YrMnDy
	FROM
	dbo.TimeClock
	WHERE
	(
	(Dt BETWEEN CONVERT(DATETIME, @DateFrom, 102) AND CONVERT(DATETIME, @DateTo, 102))
	AND 
	(EventType=1 OR EventType=2)
	)
	ORDER BY EmpID, Dt
END
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Microsoft
Question Asked By: ascnd
Solution Provided By: chapmandew
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by BriCrowe
Expert Comment by BriCrowe:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by chapmandew

Rank: Genius

Expert Comment by chapmandew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by ascnd
Author Comment by ascnd:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by chapmandew

Rank: Genius

Accepted Solution by chapmandew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by ascnd
Author Comment by ascnd:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by chapmandew

Rank: Genius

Expert Comment by chapmandew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by ascnd
One last thing.  How do I change the paramter values in the query
select * from dbo.myfun('1/1/2008','2/1/2008')
programmatically?
Otherwise this would be static.
 
 
Comment by chapmandew
How are you trying to call it?
 
 
Comment by ascnd
By the last query (Q4).

****** Q1 ******
SELECT     TmClkID, EmpID, Dt, EventType, Loc, YrMnDy
FROM         dbo.fncRAWdataByDate('3/19/2008 00:00:00 AM', '3/19/2008 11:59:59 AM') AS fncRAWdataByDateTable

****** Q2 ******
SELECT     EmpID, YrMnDy
FROM         dbo.vwEvalData_10
GROUP BY EmpID, YrMnDy
HAVING      (COUNT(EmpID) % 2 = 0)

****** Q3 ******
SELECT     dbo.vwEvalData_10.*
FROM         dbo.vwEvalData_10 INNER JOIN
                      dbo.vwEvalData_20 ON dbo.vwEvalData_10.EmpID = dbo.vwEvalData_20.EmpID AND dbo.vwEvalData_10.YrMnDy = dbo.vwEvalData_20.YrMnDy

****** Q4 ******
SELECT     TOP (100) PERCENT outP.EmpID, outP.Dt, outP.EventType, DATEDIFF(mi, inP.Dt, outP.Dt) AS TmMn
FROM         dbo.vwEvalData_30 AS inP INNER JOIN
                      dbo.vwEvalData_30 AS outP ON inP.EmpID = outP.EmpID AND outP.Dt > inP.Dt LEFT OUTER JOIN
                      dbo.vwEvalData_30 AS inbetween ON inP.EmpID = inbetween.EmpID AND inbetween.Dt > inP.Dt AND inbetween.Dt < outP.Dt
WHERE     (inP.EventType = 1) AND (inbetween.EmpID IS NULL) AND (outP.EventType = 2)
ORDER BY inP.EmpID, inP.Dt


 
 
Comment by chapmandew
Ok, so are you needing to use values from fields in other tables to pass into it?
 
 
Comment by ascnd
I am creating a report so the user will, via a web interface or MS Access, select the date range and then run the report.  Here is the queries again with names:

****** Q1 (dbo.vwEvalData_10) ******
SELECT     TmClkID, EmpID, Dt, EventType, Loc, YrMnDy
FROM         dbo.fncRAWdataByDate('3/19/2008 00:00:00 AM', '3/19/2008 11:59:59 AM') AS fncRAWdataByDateTable

****** Q2 (dbo.vwEvalData_20) ******
SELECT     EmpID, YrMnDy
FROM         dbo.vwEvalData_10
GROUP BY EmpID, YrMnDy
HAVING      (COUNT(EmpID) % 2 = 0)

****** Q3  (dbo.vwEvalData_30)******
SELECT     dbo.vwEvalData_10.*
FROM         dbo.vwEvalData_10 INNER JOIN
                      dbo.vwEvalData_20 ON dbo.vwEvalData_10.EmpID = dbo.vwEvalData_20.EmpID AND dbo.vwEvalData_10.YrMnDy = dbo.vwEvalData_20.YrMnDy

****** Q4  (dbo.vwEvalData_40)******
SELECT     TOP (100) PERCENT outP.EmpID, outP.Dt, outP.EventType, DATEDIFF(mi, inP.Dt, outP.Dt) AS TmMn
FROM         dbo.vwEvalData_30 AS inP INNER JOIN
                      dbo.vwEvalData_30 AS outP ON inP.EmpID = outP.EmpID AND outP.Dt > inP.Dt LEFT OUTER JOIN
                      dbo.vwEvalData_30 AS inbetween ON inP.EmpID = inbetween.EmpID AND inbetween.Dt > inP.Dt AND inbetween.Dt < outP.Dt
WHERE     (inP.EventType = 1) AND (inbetween.EmpID IS NULL) AND (outP.EventType = 2)
ORDER BY inP.EmpID, inP.Dt
 
 
Comment by chapmandew
How are you getting the date values?  Where will they be coming from?
 
 
Comment by ascnd
I guess at this point you tell me because I don't know what method or process to do this.  If everything was in MS Access then my User Form would have hidden fields that would have the date that the query would read when it ran.  Now that I am doing this using an MS Access interface I don't know how I would get these values form the form to the query.  I don't know if the SQL statement would take the [Forms]![frm]![DateFrom], if your familar with MS Access.
 
 
Comment by chapmandew
Hmm...Id ask another question in the Access zone...I would imagine that you would need to be calling a stored procedure, but not sure.
HTH,
Tim
 
 
Comment by ascnd
One last question.  Can a Stored Procedure reference a View?
 
 
Comment by chapmandew
certainly
 
 
Comment by ascnd
Thank you for all your help.
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628