all groups > sql server mseq > june 2004 >
You're in the

sql server mseq

group:

Using Stored Procedure returned fields in another Stored Proc or Query



Using Stored Procedure returned fields in another Stored Proc or Query BGS
6/17/2004 7:21:19 PM
sql server mseq: Hi all,
I've got a Join All stored proc which returns about
120,000 records.

It simpley returns Account number field and Balance field
for each acustomer ccount for each table.

I want to use the returned data as if it were just
another table or view. As in:


Select * from spGetAllAccountBalances

or Retrieving the customers Credit Limit and linking to
the customers AccountBallance:


SELECT
spGetAllAccountBalances.Acct,
spGetAllAccountBalances.Bal,
Customer.CreditLimit,
FROM
dbo.Customer
INNER JOIN
spGetAllAccountBalances
ON
Customer.Acct = spGetAllAccountBalances.Acct

I just can't get at the stored proc data...

I don't mind doing this thru a stored procedure but would
prefer to use in a view...

Any help would be appreciated,

Thanks
BGS




-----------------


CREATE PROCEDURE spGetAllAccountBalances
@year as char(4),
@period as char(2)
AS
SELECT
A.ACCT, A.BAL
FROM
dbo.A
WHERE CAST(RTRIM(A.FISCYR) + RTRIM(A.FISCPER) AS INT)
<= CAST(@year + @period AS INT)
UNION ALL

SELECT
B.ACCT, B.BAL
FROM
dbo.B
WHERE CAST(RTRIM(B.FISCYR) + RTRIM(B.FISCPER) AS INT)
<= CAST(@year + @period AS INT)
UNION ALL
----- Table c,d,e, etc...

GO
Re: Using Stored Procedure returned fields in another Stored Proc or Query Hari
6/18/2004 2:42:22 PM
Hi,

Have a look into Inline table valued User defined functions in books online

--
Thanks
Hari
MCDBA
[quoted text, click to view]

Re: Using Stored Procedure returned fields in another Stored Proc or Query anonymous NO[at]SPAM discussions.microsoft.com
6/20/2004 4:29:36 PM
Thanks for the reply, Hari.

Just the name seems to elude to an extremely advanced
solution.

Searched the MS Online Support for:
Inline table valued User defined functions

but found no related documents


Searched google for same and found
http://www.yafla.com/papers/sqlhierarchies/sqlhierarchies.
htm
would this be what you are suggesting?

Thanks,
Re: Using Stored Procedure returned fields in another Stored Proc or Query anonymous NO[at]SPAM discussions.microsoft.com
6/21/2004 2:18:31 PM
Thanks for the clarification Hugo.

I was getting

Start | All Programs | Microsoft SQL Server | Books On-
line

mixed up with

Internet Explorer | Help | Online Support

Re: Using Stored Procedure returned fields in another Stored Proc or Query anonymous NO[at]SPAM discussions.microsoft.com
6/21/2004 2:27:07 PM
Yes, much better.

Just had another idea. Perhaps I could group my records
in my previously written(enclosed in origianl post)
stored proc?

I can't get the stored proc to accept my grouping...

Anyone know if I can use grouping with my Stored Proc?

Or should I do it with the returned table?

Thanks,
Re: Using Stored Procedure returned fields in another Stored Proc or Query anonymous NO[at]SPAM discussions.microsoft.com
6/21/2004 6:15:43 PM
CREATE PROCEDURE Create_Worksheet_Summary
@year as char(4),
@period as char(2)
AS
SELECT
* FROM
dbo.A
Group By A.Acct
WHERE CAST(RTRIM(A.FISCYR) + RTRIM(A.FISCPER) AS INT)
<= CAST(@year + @period AS INT)
UNION ALL
(SELECT
* FROM
dbo.B
Group By B.Acct
WHERE CAST(RTRIM(B.FISCYR) + RTRIM(B.FISCPER) AS INT) <=
CAST(@year + @period AS INT))

GO


----

Incorrect syntax near 'groupby' (or Group by)

Thanks,
Re: Using Stored Procedure returned fields in another Stored Proc or Query Hugo Kornelis
6/21/2004 8:30:12 PM
On Sun, 20 Jun 2004 16:29:36 -0700, <anonymous@discussions.microsoft.com>
[quoted text, click to view]

Hi Barry,

In Books Online, activate the Index tab, enter the keyword "user-defined
functions" (without the quotation marks) and select the sub-heading
"return a table".

Best, Hugo
--

Re: Using Stored Procedure returned fields in another Stored Proc or Query Hugo Kornelis
6/22/2004 12:55:15 AM
On Mon, 21 Jun 2004 14:27:07 -0700, <anonymous@discussions.microsoft.com>
[quoted text, click to view]

Hi baz,

Both stored procedures and functions allow the use of GROUP BY in the
enclosed queries. If you post the code you used and the error message you
got, I might be able to help you.

Best, Hugo
--

Re: Using Stored Procedure returned fields in another Stored Proc or Query Hugo Kornelis
6/22/2004 10:22:56 PM
On Mon, 21 Jun 2004 18:15:43 -0700, <anonymous@discussions.microsoft.com>
[quoted text, click to view]

Hi Baz,

You got the clauses in the wrong order - group by comes after where. Use
this instead:

CREATE PROCEDURE Create_Worksheet_Summary
@year as char(4),
@period as char(2)
AS
SELECT A.Acct, other columns in aggregate functions
FROM dbo.A
WHERE CAST(RTRIM(A.FISCYR) + RTRIM(A.FISCPER) AS INT)
<= CAST(@year + @period AS INT)
GROUP BY A.Acct
UNION ALL
SELECT B.Acct, other columns in aggregate functions
FROM dbo.B
WHERE CAST(RTRIM(B.FISCYR) + RTRIM(B.FISCPER) AS INT)
<= CAST(@year + @period AS INT)
GROUP BY B.Acct
GO
(untested)

Note - besides changing the order of WHERE and GROUP BY, I also changed
SELECT * to something more sensible. SELECT * is never a good idea in
production code; worse is that it will not work in a query that has a
group by clause. The only things allowed in the SELECT list if you use
GROUP BY are the columns listed in GROUP BY or aggregate functions.

Best, Hugo
--

AddThis Social Bookmark Button