?
Solved

passing column name to stored procedure for avg function; returns cannot convert varchar to numeric error

Posted on 2005-03-01
3
Medium Priority
?
299 Views
Last Modified: 2012-06-27
Hi all,

I need to work out the average of particular columns in a table in my database.  But, i need the column name to be set to a variable, thus allowing the user to select what column they want the average value of.  I am encountering a problem when trying to do this, because the average function takes a numeric value in its argument and i have declared the column name as a varchar, as this is what it gets passed across as.  Below is my stored procedure:

create procedure sp_CalcAvg
(
    @col varchar(20),
    @model varchar(20)
)

AS

SELECT AVG(@col)
FROM ParameterChecks
WHERE ParameterChecks.SerialNumber_ID
IN (SELECT HeadData.SerialNumber_ID
    FROM HeadData
    WHERE HeadData.Model = @model)

I am totally unsure how to do this, so that i am passing in the datatype of the column and not the column name itself.  @col should be determined by the user as to which column they want to calculate the average of depending on the @model parameter.  I have tried casting the @col parameter to a decimal, but to no avail...please help!!
0
Comment
Question by:mitesh114
[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
3 Comments
 
LVL 23

Accepted Solution

by:
adathelad earned 500 total points
ID: 13430211
You need to use dynamic sql for this:
create procedure sp_CalcAvg
(
    @col varchar(20),
    @model varchar(20)
)
AS
DECLARE @strSql VARCHAR(1000)
SET @strSql = 'SELECT AVG(' + @col + ')
FROM ParameterChecks
WHERE ParameterChecks.SerialNumber_ID
IN (SELECT HeadData.SerialNumber_ID
    FROM HeadData
    WHERE HeadData.Model = ' + '''' + @model + '''' + ')'
EXECUTE(@strSql)
0
 

Author Comment

by:mitesh114
ID: 13430446
Thank you!!  I've been trying to get an answer to this for days...nobody else's answer seems to make sense!  If you could just clear up a few things for me though; what does (' + @col + ') and '''' mean??
0
 
LVL 23

Expert Comment

by:adathelad
ID: 13430519
The best thing would be for you to change EXECUTE with PRINT temporarily - this will print out the statement  that is built up and being executed. Run it in query analyser and it should become clear what is going on.

Basically,
 (' + @col + ')  - this appends the name of the column into the SQL statement
i.e. assuming @col = 'MyColumn', then the SQL query will read: "SELECT AVG(MyColumn)...."

'''' - this just makes sure that the model parameter has single quotes around.

e.g. the fully built up SQL statement being executed will look something like:
SELECT AVG(MyColumn)
FROM ParameterChecks
WHERE ParameterChecks.SerialNumber_ID
IN (SELECT HeadData.SerialNumber_ID
    FROM HeadData
    WHERE HeadData.Model = 'SomeModel')

Hope this helps
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

752 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