Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Surfer513
Starting Member
29 Posts |
Posted - 2010-09-29 : 22:41:38
|
I have a T-SQL Stored Procedure, and in that SP I want to be able to take the AVG() of a certain field that is determined by an input parameter. Below is my current stored procedure, but I'm getting an error when I try to save it, so I believe I have a syntax/logic error. Any ideas how I can accomplish this?Thanks in advance :)CREATE PROCEDURE dbo.spCalcBillComp @strfield nvarchar(50), @avgbills decimal(10,2) OUTPUT, @numberentries int OUTPUTAS /* SET NOCOUNT ON */ SELECT @numberentries=COUNT(*), @avgbills=AVG(@strfield) FROM aspnet_tblUserBills WHERE aspnet_tblUserBills.@strfield > 0.00 RETURN |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-29 : 23:23:10
|
What is the error & also what is this "aspnet_tblUserBills.@strfield" in the where clause?PBUH |
 |
|
lazycoder
Starting Member
12 Posts |
Posted - 2010-09-30 : 04:35:22
|
AVG function doesn't accept column name in variable a parameter. You can use CASE function instead:SELECT @numberentries=COUNT(*), @avgbills = case @strfield when 'column1' then AVG(column1) when 'column2' then AVG(column2) else AVG(column3) endFROM aspnet_tblUserBillsWHERE aspnet_tblUserBills.@strfield > 0.00 http://it.expertmonster.com/ |
 |
|
Surfer513
Starting Member
29 Posts |
Posted - 2010-09-30 : 20:01:13
|
quote: Originally posted by lazycoder AVG function doesn't accept column name in variable a parameter. You can use CASE function instead:SELECT @numberentries=COUNT(*), @avgbills = case @strfield when 'column1' then AVG(column1) when 'column2' then AVG(column2) else AVG(column3) endFROM aspnet_tblUserBillsWHERE aspnet_tblUserBills.@strfield > 0.00 http://it.expertmonster.com/
I see what you're saying about the case with the AVG() function, and I've modified accordingly, but I still get the error and I think it may be coming from my line that states "WHERE aspnet_tblUserBills.@strfield > 0.00"Is that also incorrect syntax? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
lazycoder
Starting Member
12 Posts |
Posted - 2010-10-01 : 02:18:29
|
quote: Originally posted by Surfer513I see what you're saying about the case with the AVG() function, and I've modified accordingly, but I still get the error and I think it may be coming from my line that states "WHERE aspnet_tblUserBills.@strfield > 0.00"Is that also incorrect syntax?
Sorry, i didn't notice that in the WHERE clause. If I understand you correctly, then just use the CASE function the where clause too. So it would look like this:SELECT @numberentries=COUNT(*), @avgbills = case @strfield when 'column1' then AVG(column1) when 'column2' then AVG(column2) else AVG(column3) endFROM aspnet_tblUserBillsWHERE case @strfield when 'column1' then column1 when 'column2' then column2 else column3 end > 0 -----------------http://it.expertmonster.com/ |
 |
|
lazycoder
Starting Member
12 Posts |
Posted - 2010-10-01 : 02:24:14
|
You could also create different versions of the stored procedure, one for each column and call the appropriate as needed. But that's what you wanted to avoid. However, it would be best for performance.Another option would be to have more versions of the statement in a single stored procedure, and use IF function to run the one you need.If there are too many columns by which you need to vary, then you can use dynamic SQL as tkizer suggested.-----------------http://it.expertmonster.com/ |
 |
|
|
|
|
|
|