473,378 Members | 1,605 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,378 software developers and data experts.

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],
MySupportFileCategory.CategoryID
FROM
MySupportFiles
INNER JOIN MySupportFileCategory ON MySupportFiles.FileID =
MySupportFileCategory.FileID
INNER JOIN MySupportCategories ON MySupportFileCategory.CategoryID =
MySupportCategories.CategoryID
WHERE
--(MySupportCategories.CategoryDesc LIKE N'%' + @Search + '%') OR
--(MySupportCategories.CategoryName LIKE N'%' + @Search + '%') OR
(MySupportFiles.[FileName] LIKE N'%' + @Search + '%') OR
(MySupportFiles.LongDescription 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 = GetMySupportCategoryPath @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 GetMySupportCategoryPath
@ID int
AS

DECLARE @ParentCategoryID 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
@ParentCategoryID = ParentCategoryID,
@CategoryName = CategoryName
FROM
MySupportCategories
WHERE
CategoryID = @ID

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

-- 3. When the ParentCategoryID is -1 we have reached the top of the
hierarchy.
IF @ParentCategoryID = -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 GetMySupportCategoryPath @ParentCategoryID
GO
Jul 20 '05 #1
2 4937
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.co.uk (Jose Perez) wrote in message news:<37**************************@posting.google. 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],
MySupportFileCategory.CategoryID
FROM
MySupportFiles
INNER JOIN MySupportFileCategory ON MySupportFiles.FileID =
MySupportFileCategory.FileID
INNER JOIN MySupportCategories ON MySupportFileCategory.CategoryID =
MySupportCategories.CategoryID
WHERE
--(MySupportCategories.CategoryDesc LIKE N'%' + @Search + '%') OR
--(MySupportCategories.CategoryName LIKE N'%' + @Search + '%') OR
(MySupportFiles.[FileName] LIKE N'%' + @Search + '%') OR
(MySupportFiles.LongDescription 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 = GetMySupportCategoryPath @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 GetMySupportCategoryPath
@ID int
AS

DECLARE @ParentCategoryID 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
@ParentCategoryID = ParentCategoryID,
@CategoryName = CategoryName
FROM
MySupportCategories
WHERE
CategoryID = @ID

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

-- 3. When the ParentCategoryID is -1 we have reached the top of the
hierarchy.
IF @ParentCategoryID = -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 GetMySupportCategoryPath @ParentCategoryID
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 = GetMySupportCategoryPath @ID
...
CREATE PROCEDURE GetMySupportCategoryPath
@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
GetMySupportCategoryPath 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
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...
2
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...
2
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...
5
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...
6
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
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...
4
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...
4
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 -...
12
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...
1
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.