Showing posts with label single. Show all posts
Showing posts with label single. 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

Sunday, March 11, 2012

Calling a single UDF multiple times within 1 View

Lets say I have a User Defined Function. I need to call the UDF a number of times within the same View. However, the results are not as expected. The only way (I know of) is to have seperate instances on the same function saved under different names. Is there a way to call a single UDF multiple times within a View without the returned values being messed-up?read the sticky at the top of the forum, and why not post what you have.|||Sorry about the ambiguity, missed the new sticky.

I had this problem a few weeks ago, and ended-up having to create multiple instances of the UDF. However, I am now having problems recreating the error and was hoping that someone would have come accross what I described.|||Calling a function multiple times within a view will not screw anything up. There is something else fishy going on. Follow Brett's posting advice.

Calling a Stored Procedure from a strored Procedure

Hi,
I want call a stored procedure A that reurns a single value from another
Stored Proc B. Stored Proc B must use the returned value from Stored Proc A
as an input
SOS
create proc usp_SprocB
as
return -1
go
create proc usp_SprocA
as
declare @.retValB int
exec @.retValB = usp_SprocB
return @.retValB * 105
go
declare @.retVal int
exec @.retVal = usp_SprocA
select @.retVal
hth
Eric
Givosky wrote:
> Hi,
> I want call a stored procedure A that reurns a single value from
> another Stored Proc B. Stored Proc B must use the returned value from
> Stored Proc A as an input
> SOS
|||Hi ,
I used the other option.
I made Stored Proc A a Function A
and call it from Stored Proc B
e.g
Stored Proc B
Select * from
XX where XX.ID in (Select * from Function A(Param1) )
"Eric Sabine" wrote:

> create proc usp_SprocB
> as
> return -1
> go
> create proc usp_SprocA
> as
> declare @.retValB int
> exec @.retValB = usp_SprocB
> return @.retValB * 105
> go
> declare @.retVal int
> exec @.retVal = usp_SprocA
> select @.retVal
> hth
> Eric
>
> Givosky wrote:
>
>

Calling a Stored Procedure from a strored Procedure

Hi,
I want call a stored procedure A that reurns a single value from another
Stored Proc B. Stored Proc B must use the returned value from Stored Proc A
as an input
SOScreate proc usp_SprocB
as
return -1
go
create proc usp_SprocA
as
declare @.retValB int
exec @.retValB = usp_SprocB
return @.retValB * 105
go
declare @.retVal int
exec @.retVal = usp_SprocA
select @.retVal
hth
Eric
Givosky wrote:
> Hi,
> I want call a stored procedure A that reurns a single value from
> another Stored Proc B. Stored Proc B must use the returned value from
> Stored Proc A as an input
> SOS|||Hi ,
I used the other option.
I made Stored Proc A a Function A
and call it from Stored Proc B
e.g
Stored Proc B
--
Select * from
XX where XX.ID in (Select * from Function A(Param1) )
"Eric Sabine" wrote:
> create proc usp_SprocB
> as
> return -1
> go
> create proc usp_SprocA
> as
> declare @.retValB int
> exec @.retValB = usp_SprocB
> return @.retValB * 105
> go
> declare @.retVal int
> exec @.retVal = usp_SprocA
> select @.retVal
> hth
> Eric
>
> Givosky wrote:
> > Hi,
> >
> > I want call a stored procedure A that reurns a single value from
> > another Stored Proc B. Stored Proc B must use the returned value from
> > Stored Proc A as an input
> >
> > SOS
>
>

Thursday, March 8, 2012

Caller identity

I am using SQL Server 2000 and SQL authentication in a web
appliacation. All data access is done via single SQL Server account.
In my front end I am using forms authentication. Is there a way to
retrieve the forms identity (or just a username) from SQL Server?

Thanks,
LacSQL can only know what's passed through the connection string, which
will be the SQL username.

Of course, there might be a solution if you describe more of the
problem; are you trying to log user activity? Do you just need to know
when a certain user is logged on or off?

Stu|||[posted and mailed, please reply in news]

(lac@.myrealbox.com) writes:
> I am using SQL Server 2000 and SQL authentication in a web
> appliacation. All data access is done via single SQL Server account.
> In my front end I am using forms authentication. Is there a way to
> retrieve the forms identity (or just a username) from SQL Server?

Yes, but the form has to cooperate, and do this:

DECLARE @.bin varbinary(128)
SELECT @.bin = convert(varbinary(128), 'Username')
SET CONTEXT_INFO @.bin

Then you can retrieve the username in SQL Server this way:

DECLARE @.username varchar(128)
SELECT @.username = convert(varchar(128), context_info)
FROM master.dbo.sysprocesses
WHERE spid = @.@.spid

Note that on SQL 2005, you should replace the access to sysprocesses to
use the built-in function context_info(). (Which unfortunately is not
available in SQL 2000.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Wednesday, March 7, 2012

Call function from a procedure and assign return value to variable

What is that cursor supposed to be doing?
Are you really looping through the whole table to get a single value'
And possibly some random value at that?
i cannot in good conscience give you an answer and let you keep that UDF...
Chris wrote:
> Hi all,
> I have a procedure proc1, which needs to update a table.
> One of the columns to update the table is calculated using a function
> funct1 (which returns a smalldatetime)
> My problem is how do I call the function funct1 from procedure proc1,
> so that the return value of function is saved in a variable in
> procedure.
>
> Sample Procedure Proc1
> ALTER PROCEDURE proc1
> @.x int
> AS
> declare @.ret smalldatetime
> --Here, I want @.ret to be a smalldatetime value returned by function
> funct1.
> --I tried the below (though knowing it wont work):
> --@.ret = select dbo.funct1(param1), but it gives error.
> --I even tried(again, though knowing it wont work):
> --update myTable set field1 = select dbo.funct1(param1), but it gives
> error.
> --WHERE ...
> update myTable set field1 = @.ret
> WHERE ...
>
> Sample function funct1:
> alter FUNCTION dbo.funct1
> (
> @.val1 decimal,
> @.val2 int
> )
> RETURNS smalldatetime
> AS
> BEGIN
> DECLARE @.ret1 decimal
> DECLARE @.ret2 smalldatetime
> DECLARE sel_Cursor CURSOR FOR SELECT field1, field2
> FROM myTable
> OPEN sel_Cursor
> FETCH NEXT FROM sel_Cursor INTO @.ret1, @.ret2
> CLOSE sel_Cursor
> DEALLOCATE sel_Cursor
> if @.val1 = @.ret2
> return @.ret2
> else
> set @.ret2 = some function...
> RETURN @.ret2
> END
> ---
> Pls help..
> TIA..
>ah - i've been enlightened by a colleague
MSSQL isn't Oracle - many things don't work the same - functions and
cursors in particular.
in oracle (prior to 9i), it's best to use a function to return single
values [in mssql - only if the scalar function will truly get a lot of
re-use; sql2005 is better at udfs than sql2k],
and i understand that performance can be better with the cursor/fetch
first row value [hopefully, there was supposed to be a where on that
select *] in mssql, cursors are to be avoided.
unless this udf is re-used a lot, in mssql this works better in-line
with more specific ddl, a better mssql approach can be suggested
Trey Walpole wrote:
> What is that cursor supposed to be doing?
> Are you really looping through the whole table to get a single value'
> And possibly some random value at that?
> i cannot in good conscience give you an answer and let you keep that UDF..
.
>
> Chris wrote:
>|||see reply to myself :)
Chris wrote:
> Thanks guys..
> Damn, I dont knw why i was using the 'select'. Now it worked for me.
> Trey,
> I am using the cursor to fetch value and assign it to a variable.
> The 'where' clause fetches single row.
> And the select query returns single value.
> But still i have used cursor coz thts the only way I know (I am pretty
> new to SQL :( ).
> Is there any other way to do so'
> Trey Walpole wrote:
>
>

Friday, February 24, 2012

Calculations tab - MSAS 2005

Hi

Pretty new to this so bear with me:)

I have created a cube which has atm a single value measure and 5 dimensions which enable me to look at totals of the value measure in various ways - quite pleased I've managed to get this far without too many headaches.

I now need to create a new measure which will be the existing value measure didvided by a subset of the existing value measure. So lets say each value belongs to a group numbered 1-10. I need to have a new value which is 'value' divided by the total of values belonging to group 1. Hope thats clear :)

1. I think I need to create a new calculated member in the calculations tab of MSAS 2005, is that right?

2. Can I have an example of syntax how to do this? I know SQL very well but MDX not at all so knowing what I want but not how to get at it is very frustrating.

Thanks for any help you can give.

Wayne

Hi

Ad 1) Yes

Ad 2) Assuming that your "group" is an attribute in one of your dimensions, the following should work.

CASE WHEN ([Measures].[Value],[Dimension 1].[Group].[Group 1]) = 0
THEN NULL
ELSE [Measures].[Value]/([Measures].[Value],[Dimension 1].[Group].[Group 1])
END

And also you should set the "Non empty behavior" of the new calculated measure to [Measures].[Value].

|||

Couple of comments:

1) IIF is often preferable to CASE for performance reasons

2) For this calculation it is not possible to set NON_EMPTY_BEHAVIOR correctly in general case. Setting it to [Measures].[Value] will cause wrong results when [Measures].[Value] is 0 (as opposed to NULL). If you are sure it is never 0, then probably it will work.

|||

Thanks for those replies.

I've created a calculation but I'm not quite getting what I want yet. I'll flesh out my scenario a bit & maybe you can help some more :)

I am working with accounts data & each value in my cube is held against an accounting code. Each accounting code belongs to a hierarchy of groups which is 3 deep - I call them lvl1, lvl2 and lvl3.

I have data for many businesses which spans several years.

In order to compare one business' performance to another I need to divide each value in the cube by each £1000 of income for that business in that year. The income values have a lvl3 code of 'INCOME'. This is the bit I am having trouble with.

So my calculation as it stands atm is:

CALCULATE;

CREATE MEMBER CURRENTCUBE.[MEASURES].RatioVal

AS

CASE WHEN ([Measures].[Val],[Tbl Acc Codes].[Acc Lvl3 Code].[INCOME]) = 0

THEN 0

ELSE [Measures].[Val] /(([Measures].[Val],[Tbl Acc Codes].[Acc Lvl3 Code].[INCOME]) / -1000)

END

,

VISIBLE = 1 ;

When I look at the cube browser this seems to be giving me a ratioval on most of my group total lines but not for every value in the cube as I would like. I'm thinking that the income total is perhaps being calculated depinding on the position of each Val within the cube, when what I need is for the calculation to use the same income total for each business/year every time. So if a business has a total income of £25,000 for 2005 I want to divide every value in my cube for that business/year by 25.

Any help you can offer much appreciated & sorely needed :)

Thanks