473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Using stored procedure result set in another stored procedure

I've been developing a stored procedure that uses a user defined
function in the query portion of the procedure. However, since the end
product needs to allow for dynamic table names, the UDF will not work.
I've been trying to get this to work with converting the UDF to a
procedure, but I'm having no luck.

Here is the background on what I'm trying to accomplish. I need to
perform a sub-identity on a table, I have the normal identity set, but
there are multiple duplicates in the table and I need each set of
duplicates numbered also (1,2,3,4 for duplicate set 1, 1,2,3 for dup
2).

Here is what I have using the UDF (the UDF returns a variable table
with indetity and ID for each record)

********** UDF *************

CREATE FUNCTION dbo.setDuplicateTransactions(@accountNumber as
varchar(50))
RETURNS @dupTransactions TABLE
(
ID int IDENTITY,
transactionID int
)
AS

BEGIN

INSERT @dupTransactions
SELECT t1.transactionID
FROM providerTransactions t1
WHERE t1.accountNumber = @accountNumber
ORDER BY t1.transactionID

RETURN

END

******** Stored Procedure ************

CREATE PROCEDURE dbo.sp_parseTransactions

AS

DECLARE @accountNumber varchar(50)

DECLARE temp_cursor CURSOR FORWARD_ONLY FOR
SELECT t1.accountNumber
FROM providerTransactions t1
GROUP BY t1.accountNumber
HAVING MAX(isNull(t1.duplicateCount,0)) != COUNT(t1.transactionID)
ORDER BY t1.accountNumber

OPEN temp_cursor

FETCH NEXT FROM temp_cursor
INTO @accountNumber

WHILE @@FETCH_STATUS = 0

BEGIN
UPDATE providerTransactions
SET duplicateCount = t1.ID
FROM setDuplicateTransactions(@accountNumber) t1,
providerTransactions t2
WHERE t1.transactionID = t2.transactionID

FETCH NEXT FROM temp_cursor
INTO @accountNumber

END

CLOSE temp_cursor
DEALLOCATE temp_cursor
Now this does work and accomplishes what I want. But as mentioned, I
need to make the tables dynamic which will require the UDF to be
eliminated and either the UDF portion moved to the stored procedure or
create another stored procedure to be called by this one.

I've tried moving the UDF into the procedure, however, the identities
did not reset on the subsequent loops. If there is a way to reset the
table variable, that would be a big help.

I did move the UDF to a stored procedure, then tried to tie it into the
main procedure.

**** UDF as Stored Procedure ******

CREATE PROCEDURE dbo.sp_setDuplicateTransactions
@accountNumber as varchar(50)
AS
DECLARE @dupTransactions TABLE
(
ID int IDENTITY,
transactionID int
)

BEGIN
INSERT @dupTransactions
SELECT t1.transactionID
FROM providerTransactions t1
WHERE t1.accountNumber = @accountNumber
ORDER BY t1.transactionID

END

Here is the snippet of code replacing the current UPDATE query.

DECLARE @dupTransactions sysname
EXECUTE @dupTransactions = sp_setDuplicateTransactions @accountNumber

EXEC('
UPDATE providerTransactions
SET duplicateCount = t1.ID
FROM ' + @dupTransactions + ' t1, providerTransactions t2
WHERE t1.transactionID = t2.transactionID
')

When I run the main stored procedure I get the following error when I
stop running it.

(1 row(s) affected)

Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '0'.

(1 row(s) affected)

Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '0'.

(1 row(s) affected)

Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '0'.

(1 row(s) affected)

So any help would be greatly appreciated.

Thanks,
Marc

Jul 23 '05 #1
4 13449
Please post DDL (CREATE TABLE statements), sample data (INSERT
statements), show your required end result and explain a bit more for
us. In particular, is this a one-off process to eliminate duplicates or
do you intend to use this as a mechanism to generate keys?

The problem with generating keys like this is that it requires you to
serialize your INSERT statements. In a multi-user environment that may
prove unacceptable. If you really need to do it then I suggest you
retrieve and increment the next value as part of the proc that performs
the INSERT.

Unfortunately from the sample you've given I'm not clear exactly what
is supposed to drive the sequence. If it's from insertion order then
you may be better off just to preserve the insertion DATETIME, leave
out the Count column and then derive the "row number" in a query when
you retrieve the data. Anyway, I'm certain there is a better
alternative to using a cursor and a function in this way. See if this
helps:

UPDATE Transactions
SET duplicatecount =
(SELECT COUNT(*)
FROM Transactions AS T
WHERE T.accountnumber = Transactions.accountnumber
AND T.transactionid <= Transactions.transactionid);

P.S. Don't use the "sp_" prefix for procs unless you intend to create a
system proc in Master. "sp_" is reserved for system procs and comes
with some disadvantages when applied to user procs.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
>> the end product needs to allow for dynamic table names <<

Stop what you are doing and get a book. You have no understanding of
on basic data modeling or SQL.

Q: What is a table?
A: A set of one kind of entities or relationships. They must map to
thigns in the reality of the data model.

Q: What do dynamic table names mean?
A: That you are modeling a world in which elephants can fall out of
the sky!

I see that you use an ORDER BY with an INSERT; did you know that tables
have no ordering? It is the first rule OF RM.

what is a "sub-identity"? Surely you know that an IDENTITY cannot ever
be a key.
You talk about "multiple duplicates", which leads us to ask what a
"single duplicate" means. I thinik you might have meant a two-column
key, but who knows?

Do you know that "sp_" has a special proprietary meaning in T-SQL? Why
did anyone design a CHAR(50) account number?? That has to be a bitch
to verify! But I bet that you have no validation or verification, do
you?

Usually we ask that someone post DDL,data and clear spec. but i do
not think this will really help. Procedures, cursors, sequential
numberings, no constraints and trying to put ordering on tables -- this
is not an RDBMS at all. You are writting a magnetic tape file system
in SQL. You need outside coinsulting and not teh kludges you can get
from a Newsgroup.

Jul 23 '05 #3
[posted and mailed, please reply in news]

(ma**@oneleaf.com) writes:
I've been developing a stored procedure that uses a user defined
function in the query portion of the procedure. However, since the end
product needs to allow for dynamic table names, the UDF will not work.
Ehum, if you have a need for dynamic table names, you probably have a
problem with your design. The idea is that the schema in a relational
database is stable. It may change when there are upgrades to the product,
but when the system is in normal production mode, the set of tables
should be constant.
I've tried moving the UDF into the procedure, however, the identities
did not reset on the subsequent loops. If there is a way to reset the
table variable, that would be a big help.
No, but use a temp table instead. Then you can use TRUNCATE TABLE to
delete the old data, and have the counter reset to 1.

However, study the suggestion from David. Queries likes are not always
that effective, but it's probably more effective than iterating over
each account with duplicates.
Here is the snippet of code replacing the current UPDATE query.

DECLARE @dupTransactions sysname
EXECUTE @dupTransactions = sp_setDuplicateTransactions @accountNumber


The return value from a stored procedure is always an integer value.
Normally you use this only to indicate success/failure, with 0 meaning
success and everything else means failure. Since you don't have a RETURN
statement in your procedure, you get back 0, and the 0 then causes syntax
errors in your dynamic SQL.

I don't really understand what the above is supposed to mean. But judging
from what you say in the rest of the article, you may be interested in
an article on my web site about sharing data between stored procedures,
http://www.sommarskog.se/share_data.html.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Sorry about that, I know not to use "sp_", I was testing some of this
to get the results I wanted.

The main reason for doing the duplicate count was to speed up importing
new data. We import new data ever week. Some of the accounts have
duplicate transactions, so we need to check for any new duplicates. So
if the import table has an account with 5 duplicates and the
transaction table has 4, then we know that only one new transaction
needs to be added. We currently have a process in place to do this, but
we are trying to speed up the process by using a different method. So
with numbering the duplicates, we can match up any existing duplicates
and delete them from the import table. Thus leaving only new
transactions to be imported.

On that note, I took you suggested query and it worked extremely well.
My original stored procedure took about 25 minutes, with this new query
only 1 minute.

Thank you very much David!

Marc

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robin Tucker | last post by:
Hi, I have one stored procedure that calls another ( EXEC proc_abcd ). I would like to return a result set (a temporary table I have created in the procedure proc_abcd) to the calling procedure...
0
by: jamiemcc | last post by:
Hi, I would like to have 1 stored procedure call another stored procedure (which contains multiple select statements) and then be able to access the 3 result sets. Example Create Procedure ....
2
by: Dan Flynn | last post by:
We have a stored procedure that returns a result set. We call this stored procedure from an RPG program using SQL CLI. The first call is successful and we can fetch rows, etc. The RPG program...
2
by: Twan Kennis | last post by:
Question: How do I pass a returning resultset from a nested Stored Procedure (which opens a cursor including option "WITH RETURN TO CALLER") as a returning resultset from it's own? When I...
2
by: Patrick Olurotimi Ige | last post by:
When i run the code below with stored proc :- I get only the first table results :-"templates" even if i fill the dataset with another table for example category,pages etc.. Any ideas? ...
4
by: Jaraba | last post by:
In Microsoft SQL I can run the following command: insert into table execute proc1 to insert data into a table. Is there a comparable way to do this in DB2?
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
3
by: mandible | last post by:
I'm trying to call one stored procedure inside another. I was wondering if this is possible Some ideas I was toying with is putting the first stored procedure inside of a temp table but haven't...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.