Re: Pass SQL function as stored procedure param?
From: Dan Guzman (danguzman_at_nospam-earthlink.net)
Date: 02/22/04
- Next message: David Portas: "Re: Pass SQL function as stored procedure param?"
- Previous message: Luiz Lima: "Re: Error: Could not complete cursor operation because table schema changed...."
- In reply to: Jeremy Collins: "Pass SQL function as stored procedure param?"
- Next in thread: Jeremy Collins: "Re: Pass SQL function as stored procedure param?"
- Reply: Jeremy Collins: "Re: Pass SQL function as stored procedure param?"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 22 Feb 2004 08:13:22 -0600
You have a few options options.
1) Build a dynamic SQL statement in the proc and execute via sp_executesql
or EXECUTE. See http://www.sommarskog.se/dynamic_sql.html for dynamic SQL
considerations.
2) Build the SQL statement in your application rather than use a proc.
3) Return each value and select the one you need in your app code:
SELECT ItemID,
SUM(Score) AS TotalScore,
AVG(Score) AS AverageScore,
MIN(Score) AS MinimumScore,
MAX(Score) AS MaximumScore
FROM tb_table
WHERE ItemID = @ItemID
GROUP BY ItemID
4) Create separate procs for each aggregate function (i.e. astp_summary_sum,
astp_summary_avg, etc)
-- Hope this helps. Dan Guzman SQL Server MVP "Jeremy Collins" <jd.collins@ntlworld-not.com> wrote in message news:jt0_b.358$ta.37@newsfe1-win... > Hi all, > > Is it possible to create a stored procedure where I can > pass "SUM" or "AVG" etc as a parameter? > > Something like > > CREATE PROCEDURE astp_summary @ItemID int, @Func varchar(3) > AS > > SELECT ItemID, @Func(Score) AS TotalScore > FROM tb_table > WHERE ItemID = @ItemID > GROUP BY ItemID > GO > > > So > EXEC summary 100, 'SUM' > would return a result set with SUM(Score) > > (I have an application which draws graphs based on the data, > and the user can choose sum, average, max, min etc, and it would > be nice to just have one SP). > > TIA > > -- > jc > > Remove the -not from email
- Next message: David Portas: "Re: Pass SQL function as stored procedure param?"
- Previous message: Luiz Lima: "Re: Error: Could not complete cursor operation because table schema changed...."
- In reply to: Jeremy Collins: "Pass SQL function as stored procedure param?"
- Next in thread: Jeremy Collins: "Re: Pass SQL function as stored procedure param?"
- Reply: Jeremy Collins: "Re: Pass SQL function as stored procedure param?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|