473,796 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Stored Procedure

Dear All,

I have 2 SP's, one (let's call it sp_A) which returns a list of files
and another (let's call it sp_B) which recursively looks through a
menu table to give me the location of the file. I call sp_B within
sp_A as I want to return the data in one table. I am having trouble in
getting the results back. Both SP's work independantly but when they
are put together I get an error. Any help will be greatly
appreciated!!

Thanks,

Jose

Code below...

sp_A
CREATE PROCEDURE spSearchTest
@Search nvarchar (50)
AS

-- NOTE: We're creating the temporary table and populating it before
we know the search results.

--1. Create a temporary table to hold the search results in.
CREATE TABLE #TempResults
(
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[FileID] [int] NOT NULL ,
[FileName] nvarchar (50),
[CategoryID] int,
[CategoryPath] nvarchar (250)
)
ON [PRIMARY]

--2. Run the search query and insert the results into the temporary
table.
INSERT INTO #TempResults (FileID, [FileName], CategoryID)
SELECT
MySupportFiles. FileID,
MySupportFiles.[FileName],
MySupportFileCa tegory.Category ID
FROM
MySupportFiles
INNER JOIN MySupportFileCa tegory ON MySupportFiles. FileID =
MySupportFileCa tegory.FileID
INNER JOIN MySupportCatego ries ON MySupportFileCa tegory.Category ID =
MySupportCatego ries.CategoryID
WHERE
--(MySupportCateg ories.CategoryD esc LIKE N'%' + @Search + '%') OR
--(MySupportCateg ories.CategoryN ame LIKE N'%' + @Search + '%') OR
(MySupportFiles .[FileName] LIKE N'%' + @Search + '%') OR
(MySupportFiles .LongDescriptio n LIKE N'%' + @Search + '%')
OR
(MySupportFiles .ShortDesc LIKE N'%' + @Search + '%') OR
(MySupportFiles .Platform LIKE N'%' + @Search + '%')

--.3. Let's look at the results. See what the RowCount is and taken
action based on that.
DECLARE @RowCount int
DECLARE @Path nvarchar (250)
DECLARE @ID int
SET @RowCount = (SELECT COUNT(*) FROM #TempResults)

IF @RowCount IS NOT NULL
WHILE (@RowCount > 0)
BEGIN
SET @ID = (SELECT CategoryID FROM #TempResults WHERE [ID] =
@RowCount)

EXEC @Path = GetMySupportCat egoryPath @ID

UPDATE #TempResults SET CategoryPath = @Path WHERE [ID] = @RowCount

-- Decrease the counter by 1
SET @RowCount = @RowCount - 1
END

-- 4. Return the data to the caller and delete the temporary table.
SELECT * FROM #TempResults

DROP TABLE #TempResults
GO
sp_B
CREATE PROCEDURE GetMySupportCat egoryPath
@ID int
AS

DECLARE @ParentCategory ID int
DECLARE @CategoryName nvarchar(50)

-- 1. Create a temporary table. This code block is run just once.
IF @@NESTLEVEL = 1
BEGIN
CREATE TABLE #TempTable
(
[ID] [int] IDENTITY (1, 1) NOT NULL,
[CategoryName] [nvarchar] (50)
)
ON [PRIMARY]
END

-- 2. Select the CategoryName and put it in the temporary table.
SELECT
@ParentCategory ID = ParentCategoryI D,
@CategoryName = CategoryName
FROM
MySupportCatego ries
WHERE
CategoryID = @ID

INSERT INTO #TempTable (CategoryName)
VALUES (@CategoryName)

-- 3. When the ParentCategoryI D is -1 we have reached the top of the
hierarchy.
IF @ParentCategory ID = -1 AND @@NESTLEVEL < 32 -- max nesting level =
32
BEGIN
DECLARE @Path nvarchar (250)
DECLARE @RowCount int
SET @RowCount = (SELECT COUNT(*) FROM #TempTable)
SET @Path = ''

WHILE (@RowCount > 0)
BEGIN
SET @Path = @Path + (SELECT CategoryName FROM #TempTable WHERE [ID]
= @RowCount) + ' > '

-- Decrease the counter by 1
SET @RowCount = @RowCount - 1
END

-- Tidy up the string and return it
SET @Path = RTRIM(@Path)
SET @Path = SUBSTRING(@Path , 1, (LEN(@Path) - 1))

SELECT @Path

-- Delete the temporary table
DROP TABLE #TempTable
END
ELSE
EXEC GetMySupportCat egoryPath @ParentCategory ID
GO
Jul 20 '05 #1
2 4962
Dude, you need to give more information.

1. What is the error thrown?
2. What is the structure of the tables used in the sproc?
jl**@totalise.c o.uk (Jose Perez) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
Dear All,

I have 2 SP's, one (let's call it sp_A) which returns a list of files
and another (let's call it sp_B) which recursively looks through a
menu table to give me the location of the file. I call sp_B within
sp_A as I want to return the data in one table. I am having trouble in
getting the results back. Both SP's work independantly but when they
are put together I get an error. Any help will be greatly
appreciated!!

Thanks,

Jose

Code below...

sp_A
CREATE PROCEDURE spSearchTest
@Search nvarchar (50)
AS

-- NOTE: We're creating the temporary table and populating it before
we know the search results.

--1. Create a temporary table to hold the search results in.
CREATE TABLE #TempResults
(
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[FileID] [int] NOT NULL ,
[FileName] nvarchar (50),
[CategoryID] int,
[CategoryPath] nvarchar (250)
)
ON [PRIMARY]

--2. Run the search query and insert the results into the temporary
table.
INSERT INTO #TempResults (FileID, [FileName], CategoryID)
SELECT
MySupportFiles. FileID,
MySupportFiles.[FileName],
MySupportFileCa tegory.Category ID
FROM
MySupportFiles
INNER JOIN MySupportFileCa tegory ON MySupportFiles. FileID =
MySupportFileCa tegory.FileID
INNER JOIN MySupportCatego ries ON MySupportFileCa tegory.Category ID =
MySupportCatego ries.CategoryID
WHERE
--(MySupportCateg ories.CategoryD esc LIKE N'%' + @Search + '%') OR
--(MySupportCateg ories.CategoryN ame LIKE N'%' + @Search + '%') OR
(MySupportFiles .[FileName] LIKE N'%' + @Search + '%') OR
(MySupportFiles .LongDescriptio n LIKE N'%' + @Search + '%')
OR
(MySupportFiles .ShortDesc LIKE N'%' + @Search + '%') OR
(MySupportFiles .Platform LIKE N'%' + @Search + '%')

--.3. Let's look at the results. See what the RowCount is and taken
action based on that.
DECLARE @RowCount int
DECLARE @Path nvarchar (250)
DECLARE @ID int
SET @RowCount = (SELECT COUNT(*) FROM #TempResults)

IF @RowCount IS NOT NULL
WHILE (@RowCount > 0)
BEGIN
SET @ID = (SELECT CategoryID FROM #TempResults WHERE [ID] =
@RowCount)

EXEC @Path = GetMySupportCat egoryPath @ID

UPDATE #TempResults SET CategoryPath = @Path WHERE [ID] = @RowCount

-- Decrease the counter by 1
SET @RowCount = @RowCount - 1
END

-- 4. Return the data to the caller and delete the temporary table.
SELECT * FROM #TempResults

DROP TABLE #TempResults
GO
sp_B
CREATE PROCEDURE GetMySupportCat egoryPath
@ID int
AS

DECLARE @ParentCategory ID int
DECLARE @CategoryName nvarchar(50)

-- 1. Create a temporary table. This code block is run just once.
IF @@NESTLEVEL = 1
BEGIN
CREATE TABLE #TempTable
(
[ID] [int] IDENTITY (1, 1) NOT NULL,
[CategoryName] [nvarchar] (50)
)
ON [PRIMARY]
END

-- 2. Select the CategoryName and put it in the temporary table.
SELECT
@ParentCategory ID = ParentCategoryI D,
@CategoryName = CategoryName
FROM
MySupportCatego ries
WHERE
CategoryID = @ID

INSERT INTO #TempTable (CategoryName)
VALUES (@CategoryName)

-- 3. When the ParentCategoryI D is -1 we have reached the top of the
hierarchy.
IF @ParentCategory ID = -1 AND @@NESTLEVEL < 32 -- max nesting level =
32
BEGIN
DECLARE @Path nvarchar (250)
DECLARE @RowCount int
SET @RowCount = (SELECT COUNT(*) FROM #TempTable)
SET @Path = ''

WHILE (@RowCount > 0)
BEGIN
SET @Path = @Path + (SELECT CategoryName FROM #TempTable WHERE [ID]
= @RowCount) + ' > '

-- Decrease the counter by 1
SET @RowCount = @RowCount - 1
END

-- Tidy up the string and return it
SET @Path = RTRIM(@Path)
SET @Path = SUBSTRING(@Path , 1, (LEN(@Path) - 1))

SELECT @Path

-- Delete the temporary table
DROP TABLE #TempTable
END
ELSE
EXEC GetMySupportCat egoryPath @ParentCategory ID
GO

Jul 20 '05 #2
[posted and mailed, please reply in news]

Jose Perez (jl**@totalise. co.uk) writes:
I have 2 SP's, one (let's call it sp_A) which returns a list of files
and another (let's call it sp_B) which recursively looks through a
menu table to give me the location of the file. I call sp_B within
sp_A as I want to return the data in one table. I am having trouble in
getting the results back. Both SP's work independantly but when they
are put together I get an error. Any help will be greatly
appreciated!!
...
DECLARE @Path nvarchar (250)
...

EXEC @Path = GetMySupportCat egoryPath @ID
...
CREATE PROCEDURE GetMySupportCat egoryPath
@ID int
AS


The return value of a stored procedure is always an integer. (And should
in my opinion, only be used to indicate success/failure, with 0 indicating
success, and everything else failure.) The above could have worked ir
GetMySupportCat egoryPath had been a scalar user-defined function.

For a stored procudure, you need to use an output parameter:

CREATE PROCEDURE outpar_sp @outpar int OUTPUT
SELECT @outpar = 4711
do
DECLARE @outpar int
EXEC outpar_sp @outpar OUTPUT
SELECT @outpar

Note that you need to specify OUTPUT both in declaration and in EXEC
statement!

But there are more problems. The inner procedure has:

IF @@NESTLEVEL = 1
BEGIN
CREATE TABLE #TempTable
(
[ID] [int] IDENTITY (1, 1) NOT NULL,
[CategoryName] [nvarchar] (50)
)
ON [PRIMARY]
END

But since you call the inner procedure from the outer, @@nestlevel is 2,
and the table never gets created.


--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

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

Similar topics

2
7325
by: Yves Touze | last post by:
Hi All, I'm trying to migrate from SQL Server 7.0 to SQL Server 2000. I've got some ASP page which call VB components that retrieve shaped recordsets from SQL Server using the MSDATASHAPE provider. Precisely, here is the code i have Dim Cmdobj As New ADODB.Command Cmdobj.ActiveConnection = oconn Cmdobj.CommandType = adCmdStoredProc
2
9246
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
2
4283
by: Rhino | last post by:
I am getting an sqlcode of -927 when I execute SQL within a COBOL stored procedure in DB2 OS/390 Version 6 on OS/390. I have looked at the error message for that condition and tried everything I could think of to resolve the problem but nothing works. The stored proc is running in the DB2 Stored Procedures Address Space and both the client and the proc have DSNELI linked into their load modules. The client and proc are running in TSO via...
5
3664
by: Timppa | last post by:
Hi, Could anyone help me with my problem ? Environment: Access 2000 and Sql Server 2000. I have a stored procedure as follows: DROP table1 SELECT alias1.field1,alias2.field2,table2.field6 INTO table1
6
1740
by: Not4u | last post by:
Hello Config : SQL 2000 on WIN 2000 (IIS 5.0) In my ASP page for some queries i have this error : Microsoft OLE DB Provider for SQL Server error '80040e31' Timeout expired
7
3470
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
4
3996
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine but when he runs the stored procedure, he gets the following error message. "SYSPROC".CSGCSB54 - Run started. Data returned in result sets is limited to the first 100 rows. Data returned in result set columns is limited to the first 20...
4
7255
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which - "Modified" of type DateTime - is hidden since it should not be edited by a user. The system handles the update for this column. So, I have hidden (Visible=false) this column on the grid. In order to access the value in this field, I have created a...
12
2264
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my office. The original code's backend is using the SQL Server 2000 and I am testing to use it on the Express edition. And I run into the following problem.
1
2979
by: amgupta8 | last post by:
Note: This problem occurred when I updated the JDK from 1.3.1 to 1.4.1 or 1.4.2. Nothing else was changed in the code, other than updating the JDK on the database server (dbm cfg parm jdk_path) and recompiling/executing the code with 1.4.1 (deploying the newly compiled stored procedure code as well). This is the original exception that I got before I tried any code modifications: java.io.IOException: invalid offset/length at...
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.