MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old September 17th, 2004, 05:23 AM
pwynne33 pwynne33 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 18 pwynne33 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Trouble passing parameters from query to UDF

I'm not sure if this is the right way to get the required results but here we go....

I've got a table (in it's simplest form) with 3 fields, ID number, User Name, Status.

Some values in the user field are null but the there are only six users I'm after (see the WHERE in the code below).

I want four values from this table for each user. (also using a date range)
1) Total number of records with the User value being any of six values. (See above)
2) Same as above but having any of three statuses.
3) Total number of records for that user.
4) Same as above but having any of three statuses.

Now because I want to use the same table four times with different criteria I thought I could use UDF for each value. I'm not 100% sure about crosstab queries so I'm not sure if this is the right method to use.

I'm passing the function 3 parameters, date from, date to, FRFTier. FRFTier is the field where the username comes from (could have been named better originally I know).

dbo.fnReport_MonthlyBusinessReviewDealCount(@Date_From, @Date_To, FRFTier) AS IndivCount

Stored procedure code...

Code:
SELECT     @Date_From AS dtmFrom, @Date_To AS dtmTo, @Total_Approved_Target AS numApprovedTarget, @Total_Dealt_With_Target AS numDealtTarget, 
                      FRFTier, dbo.fnReport_MonthlyBusinessReviewChoiceCount(@Date_From, @Date_To) AS AppCount, 
                      dbo.fnReport_MonthlyBusinessReviewChoiceCountDealt(@Date_From, @Date_To) AS DealtCount, 
                      dbo.fnReport_MonthlyBusinessReviewDealCount(@Date_From, @Date_To, FRFTier) AS IndivCount, 
                      dbo.fnReport_MonthlyBusinessReviewDealtCount(@Date_From, @Date_To, FRFTier) AS IndivDealtCount
FROM         dbo.tblApplications
WHERE     ({ fn LENGTH(FRFTier) } > 3) AND (FRFTier <> 'None') AND (FRFTier <> 'Failed') AND (FRFTier <> '040704')
GROUP BY FRFTier


UDF Code...

Code:
ALTER FUNCTION dbo.fnReport_MonthlyBusinessReviewDealCount
	(
	@Date_From datetime,
	@Date_To datetime,
            @Name nvarchar
	)
RETURNS int
AS	
BEGIN			
DECLARE @COUNT int
SET @COUNT = (SELECT     COUNT(*) AS Expr1
FROM         dbo.tblApplications
WHERE     (dtmApplicationDate >= @Date_From) AND (dtmApplicationDate <= @Date_To + 1) AND (FRFTier = @Name))
RETURN @COUNT
END


These are the results I'm getting... the users values are all 0 which are wrong.

From - To - Target 1 - Target 2 - User - Total - Total Dealt - User Total - User Total Dealt
01/08/2004 17/08/2004 50 2 Kate S 378 52 0 0
01/08/2004 17/08/2004 50 2 Mel W 378 52 0 0
01/08/2004 17/08/2004 50 2 Paul H 378 52 0 0
01/08/2004 17/08/2004 50 2 Zoe S 378 52 0 0
01/08/2004 17/08/2004 50 2 Mark S 378 52 0 0
01/08/2004 17/08/2004 50 2 Carole B 378 52 0 0

Is there are better way to go about this or can anyone see an error in the SP or UDF?

Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Trouble passing parameters from query to UDF


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway