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)
 Cursor-less SP, best solution?

Author  Topic 

lmendoza
Starting Member

12 Posts

Posted - 2001-07-18 : 13:07:52
Hey all -
I'm started using SQL Server about 6 months ago, and I was wondering if you could give me some help making this stored proc. The data is designed like so

new_sku: (about 4000 rows)
sku int

longdesc: (about 600,000 rows)
ace_sku int
sequence_number int
bullet_text nvarchar(64)

it's a 1:N relationship for sku:longdesc.
I need to create one table containing the skus and the bullet_text, in the correct sequence (why we're creating one table is beyond me, since this schema is better, but I digress).

so ex:
sku: longdesc
1 1 1 bullet1
1 2 bullet2
1 3 bullet3

will make the table
1 bullet1*bullet2*bullet3

My coworker designed a explicit cursor based solution, which took about 4 hours to process 160 rows, so I took it upon myself to design another solution. The only one I came up with uses an implicit cursor. I'm convinced that there is a way to do this without using any cursors, though...Am I right?

thanks in advance-
leo

in case you're wondering, here's my implicit cursor based sp:
CREATE PROCEDURE usp_newsku
AS

CREATE TABLE #t
(
pk INT
IDENTITY(1,1),
sku INT,
descr NVARCHAR(2048)
)

INSERT INTO #t (sku)
(SELECT sku FROM new_sku)

DECLARE @cnt INT, @max INT, @sku NVARCHAR(20)
DECLARE @desc nvarchar(1048)

SET @cnt = 0

SELECT @max = MAX(pk) FROM #t

--start the loop
WHILE (@cnt <= @max)
BEGIN
SET @cnt = @cnt + 1
SELECT @sku = sku FROM #t WHERE pk = @cnt --get the sku

--infuse the descriptions
SELECT @desc = COALESCE(@desc + ' ',' ') + LTRIM(RTRIM(bullet_text))
FROM longdesc WHERE ace_sku = @sku

--update the temp table
UPDATE #t SET descr = @desc WHERE pk = @cnt
SET @desc = ' '
END


SELECT * FROM #t
DROP TABLE #t


   

- Advertisement -