Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Mixing Dyanmic Stored Procedure with a Paging Stored Procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-09-05 : 09:42:47
Jon writes "I am trying to make a stored prodcedure that takes a few steps. First I have a stored procedure that querries for specific paremeters. (I have tested calling this from a "Paging" Stored Procedure by itself and it works.) Next I create an inital temp Table to hold those result. Then I create a second temp Table and try to insert into from a Dyanmic call (also tested and working). The error I get is 156: Incorrect Syntax near the keyword 'Declare'. Oh and if you are wondering why there are so many @where identifiers it is because they are limited to 128 chars. Win 2k Advanced Server SP2 with MS SQL 2k Enterprise.
Thank you in advance for your help/time...!

CREATE PROCEDURE sp_DynamicPrice
(
@Page int,
@RecsPerPage int,
@lowval int,
@higval int,
@WHERE1 VarChar(4000),
@WHERE2 VarChar(4000),
@WHERE3 VarChar(4000),
@WHERE4 VarChar(4000),
@WHERE5 VarChar(4000),
@WHERE6 VarChar(4000),
@WHERE7 VarChar(4000),
@WHERE8 VarChar(4000),
@WHERE9 VarChar(4000),
@WHERE10 VarChar(4000)
)
AS
-- Find out where we will start our records from
DECLARE @RecCount int
SELECT @RecCount = @RecsPerPage * @Page + 1

--Create a temporary table
CREATE TABLE #TempItems
(
ID int IDENTITY,
PdID int,
PrBraNam nvarchar (50),
PdSku nvarchar (50),
PdNam nvarchar (50),
PdDes nvarchar (50),
PrTyp nvarchar (50),
PdManNum nvarchar (50),
DeTit nvarchar (50),
DeBod nvarchar (4000),
PdSalPri money,
PrID int,
PrPicLink nvarchar (50),
PdCurInv int,
PdStock bit,
)
-- Insert the rows from sp_PriceRange into the temp. table
INSERT INTO #TempItems
(
PdID,
PrBraNam,
PrDes,
PdSku,
PdNam,
PdDes,
PrTyp,
PdManNum,
DeTit,
DeBod,
PdSalPri,
PrID,
PrPicLink,
PdCurInv,
PdStock
)
Exec('sp_PriceRange ' + @lowval + ', ' + @higval)

--Create a new temporary table from the results
CREATE TABLE #TempItemsresults
(
ID int IDENTITY,
PdID int,
PrBraNam nvarchar (50),
PdSku nvarchar (50),
PdNam nvarchar (50),
PrTyp nvarchar (50),
PdSalPri money,
PrID int,
PrPicLink nvarchar (50),
PdCurInv int,
PdStock bit,
allcount int
)

-- Insert the rows from sp_PriceRange results into the new temp. table
INSERT INTO #TempItemsresults
(
PdID,
PrBraNam,
PdSku,
PdNam,
PdDes,
PrTyp,
PdSalPri,
PrID,
PrPicLink,
PdCurInv,
PdStock
)
-- THE SYNTAX ERROR IS HERE
Declare @SQL nVarChar(4000)
Select @SQL = "SELECT DISTINCT PdID, PrBraNam, PrTyp, PrID, PrPicLink, PdNam, PdSalPri, PdSku, PdCurInv, PdStock "
+ " FROM "
+ #TempItems
+ @WHERE1
+ @WHERE2
+ @WHERE3
+ @WHERE4
+ @WHERE5
+ @WHERE6
+ @WHERE7
+ @WHERE8
+ @WHERE9
+ @WHERE10

EXEC(@SQL)

-- Find out the first and last record we want
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@Page - 1) * @RecsPerPage
SELECT @LastRec = (@Page * @RecsPerPage + 1)
-- Now, return the set of paged records, plus, an indiciation of we
-- have more records or not!
SELECT *,
MoreRecords =
(
SELECT COUNT(*)
FROM #TempItemsresults TI
WHERE TI.ID > @LastRec
)
FROM #TempItemsresults
WHERE ID > @FirstRec AND ID < @LastRec
GO
"
   

- Advertisement -