Showing posts with label statistical. Show all posts
Showing posts with label statistical. Show all posts

Monday, March 19, 2012

Calling a Stored Procedure on Multiple Records

I have a stored procedure on an SQL Server database which displays statistical data for a single record. The example below selects a user ID as a parameter and displays the user's name and the total amount of transactions he has made:
CREATE PROCEDURE dbo.GetUserStats @.UserID BIGINT AS
DECLARE @.TempTable TABLE
(
UserID BIGINT,
UserName VARCHAR(60),
TotalAmt FLOAT
)

INSERT INTO @.TempTable (UserID, UserName, TotalAmt)
SELECT u.RecID, u.LastName + ', ' + u.FirstName,
(SELECT SUM(t.Amount) FROM Transactions t WHERE t.UserID = @.UserID)
FROM Users u
WHERE u.RecID = @.UserID

SELECT * FROM @.TempTable
GO

So if I execute this amount entering a single ID, it returns a single row for that user:

UserID UserName TotalAmt
--------
1 Doe, John 100.00

What I would like to do is create another stored procedure which calls this one for every user returned in a query, thus returning the same data for several users:

UserID UserName TotalAmt
--------
1 Doe, John 100.00
2 Smith, Bob 123.45
3 Blow, Joe 150.55

Is there a way to re-use a stored procedure within another, based on the results of a query?Some thoughts:

1. This sounds more like a function than a stored procedure. Would make the execution concept much easier (ie, SELECT dbo.MyFunction(MyUserID) FROM MyTable).

2. You could wrap this with another stored procedure; you could pass in the user list as a parameter (there was an excellent article recently over at SQLServerCentral.com about using an xml string as a parameter).

But I really think that what you want is a function.

Regards,

hmscott|||Thanks for your feedback.

I'm not familiar with functions. Would I want to convert GetUserStats to a function?|||may, maybe not. you do know you can do that whole thing without the temp table.

BTW, bigint for userid? do you know there are not that many people in the world?|||I'm not familiar with functions. Would I want to convert GetUserStats to a function?

I wouldn't "convert" per se; at least not the way that you have written it.
Try this:

Open Query Analyzer
Click on File | New
Double click on Create Function
Double click on Create Scalar Function

Read in SQL BOL about Scalar functions.

I think you will get an idea of what a function is and how to go about writing your own. From there I think you will find it easier to understand how to proceed.

Regards,

hmscott|||may, maybe not. you do know you can do that whole thing without the temp table.

BTW, bigint for userid? do you know there are not that many people in the world?
The example I posted is very simplified. The actual query I'm trying to pull off is pretty complicated and pulls data from several tables, that's why I included the temp table. Can I use a temp table in a function?

Yeah, I guess a BIGINT is more than I need for a user ID. I had the misconception of thinking INTs only go to 32,000 when I set up this database (as they do in many programming languages).|||I have tried building a few functions. I can get them to run using a direct statemtent such as:

SELECT * FROM MyFunction(1)

But If I try to use the line above in a stored procedure or another function, I get an error:

Invalid object name 'MyFunction'.|||They're fiesty. You need the user name as a prefix, as in:SELECT *
FROM dbo.MyFunction(1)-PatP

Thursday, February 16, 2012

calculating with variables statistical values. How?

Dear all,
I am trying to solve this but I can't work out.
Set @.DESV_INSERTED = (SELECT stdev(desv) FROM #DESVIATIONTYPICAL WHERE
desviation_id = @.desviation_id AND desv > 0)
SET @.AVERAGE = (SELECT AVG(DESV) FROM #DESVIATIONTYPICAL WHERE
desviation_id = @.desviation_id AND desv > 0)
SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
SET @.MINIMUM = SELECT (@.AVERAGE - @.AUXMIN)
SET @.MAXIMUM = SELECT (@.AVERAGE + @.AUXMIN)
And I obtain the following errors:
Server: Msg 156, Level 15, State 1, Procedure
up_WHS_PeriodWarningQControl_PROC, Line 850
Incorrect syntax near the keyword 'SELECT'.
Server: Msg 156, Level 15, State 1, Procedure
up_WHS_PeriodWarningQControl_PROC, Line 852
Incorrect syntax near the keyword 'SELECT'.
Server: Msg 156, Level 15, State 1, Procedure
up_WHS_PeriodWarningQControl_PROC, Line 853
Incorrect syntax near the keyword 'SELECT'.
Server: Msg 156, Level 15, State 1, Procedure
up_WHS_PeriodWarningQControl_PROC, Line 855
Incorrect syntax near the keyword 'SELECT'.
Server: Msg 170, Level 15, State 1, Procedure
up_WHS_PeriodWarningQControl_PROC, Line 856
Line 856: Incorrect syntax near '='.
Thanks in advance and best regards,Change:
SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
SET @.MINIMUM = SELECT (@.AVERAGE - @.AUXMIN)
SET @.MAXIMUM = SELECT (@.AVERAGE + @.AUXMIN)
To:
SET @.AUXMIN = 1.96 * @.DESV_INSERTED
SET @.MINIMUM = AVERAGE - @.AUXMIN
SET @.MAXIMUM = AVERAGE + @.AUXMIN
and you probably have a few more similar lines in that procedure _after_ the
code you have shown. The line numbers shown in the errors are the lines in
the body of the stored procedure, after the keyword 'AS', so they have an
offset comapred to the line number in the stored procedure declaration
(which includes CREATE PROC and the parameter declarations).
Jacco Schalkwijk
SQL Server MVP
"Enric" <Enric@.discussions.microsoft.com> wrote in message
news:30173799-B153-49B2-9223-4C4B282A2B27@.microsoft.com...
> Dear all,
> I am trying to solve this but I can't work out.
>
> Set @.DESV_INSERTED = (SELECT stdev(desv) FROM #DESVIATIONTYPICAL WHERE
> desviation_id = @.desviation_id AND desv > 0)
> SET @.AVERAGE = (SELECT AVG(DESV) FROM #DESVIATIONTYPICAL WHERE
> desviation_id = @.desviation_id AND desv > 0)
>
> SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
> SET @.MINIMUM = SELECT (@.AVERAGE - @.AUXMIN)
> SET @.MAXIMUM = SELECT (@.AVERAGE + @.AUXMIN)
>
> And I obtain the following errors:
>
> Server: Msg 156, Level 15, State 1, Procedure
> up_WHS_PeriodWarningQControl_PROC, Line 850
> Incorrect syntax near the keyword 'SELECT'.
> Server: Msg 156, Level 15, State 1, Procedure
> up_WHS_PeriodWarningQControl_PROC, Line 852
> Incorrect syntax near the keyword 'SELECT'.
> Server: Msg 156, Level 15, State 1, Procedure
> up_WHS_PeriodWarningQControl_PROC, Line 853
> Incorrect syntax near the keyword 'SELECT'.
> Server: Msg 156, Level 15, State 1, Procedure
> up_WHS_PeriodWarningQControl_PROC, Line 855
> Incorrect syntax near the keyword 'SELECT'.
> Server: Msg 170, Level 15, State 1, Procedure
> up_WHS_PeriodWarningQControl_PROC, Line 856
> Line 856: Incorrect syntax near '='.
>
> Thanks in advance and best regards,|||Enric,
SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
is invalid syntax. To use a SELECT expression as
a value, you need to surround the entire SELECT
expression in parenthesis:
SET @.AUXMIN = (SELECT (1.96 * @.DESV_INSERTED))
You did this correctly in the first to SET statement,
but not in the final three.
Steve Kass
Drew University
Enric wrote:

>Dear all,
>I am trying to solve this but I can't work out.
>
>Set @.DESV_INSERTED = (SELECT stdev(desv) FROM #DESVIATIONTYPICAL WHERE
> desviation_id = @.desviation_id AND desv > 0)
>SET @.AVERAGE = (SELECT AVG(DESV) FROM #DESVIATIONTYPICAL WHERE
> desviation_id = @.desviation_id AND desv > 0)
>
>SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
>SET @.MINIMUM = SELECT (@.AVERAGE - @.AUXMIN)
>SET @.MAXIMUM = SELECT (@.AVERAGE + @.AUXMIN)
>
>And I obtain the following errors:
>
>Server: Msg 156, Level 15, State 1, Procedure
>up_WHS_PeriodWarningQControl_PROC, Line 850
>Incorrect syntax near the keyword 'SELECT'.
>Server: Msg 156, Level 15, State 1, Procedure
>up_WHS_PeriodWarningQControl_PROC, Line 852
>Incorrect syntax near the keyword 'SELECT'.
>Server: Msg 156, Level 15, State 1, Procedure
>up_WHS_PeriodWarningQControl_PROC, Line 853
>Incorrect syntax near the keyword 'SELECT'.
>Server: Msg 156, Level 15, State 1, Procedure
>up_WHS_PeriodWarningQControl_PROC, Line 855
>Incorrect syntax near the keyword 'SELECT'.
>Server: Msg 170, Level 15, State 1, Procedure
>up_WHS_PeriodWarningQControl_PROC, Line 856
>Line 856: Incorrect syntax near '='.
>
>Thanks in advance and best regards,
>|||yeah, works.
I am feel very proud of your performance.
Enric
"Jacco Schalkwijk" wrote:

> Change:
> SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
> SET @.MINIMUM = SELECT (@.AVERAGE - @.AUXMIN)
> SET @.MAXIMUM = SELECT (@.AVERAGE + @.AUXMIN)
> To:
> SET @.AUXMIN = 1.96 * @.DESV_INSERTED
> SET @.MINIMUM = AVERAGE - @.AUXMIN
> SET @.MAXIMUM = AVERAGE + @.AUXMIN
> and you probably have a few more similar lines in that procedure _after_ t
he
> code you have shown. The line numbers shown in the errors are the lines in
> the body of the stored procedure, after the keyword 'AS', so they have an
> offset comapred to the line number in the stored procedure declaration
> (which includes CREATE PROC and the parameter declarations).
> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "Enric" <Enric@.discussions.microsoft.com> wrote in message
> news:30173799-B153-49B2-9223-4C4B282A2B27@.microsoft.com...
>
>|||Thanks Steve,
Regards to all
"Steve Kass" wrote:

> Enric,
> SET @.AUXMIN = SELECT (1.96 * @.DESV_INSERTED)
> is invalid syntax. To use a SELECT expression as
> a value, you need to surround the entire SELECT
> expression in parenthesis:
> SET @.AUXMIN = (SELECT (1.96 * @.DESV_INSERTED))
> You did this correctly in the first to SET statement,
> but not in the final three.
> Steve Kass
> Drew University
>
>
> Enric wrote:
>
>