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)
 Can't find my error(s)?

Author  Topic 

label
Posting Yak Master

197 Posts

Posted - 2004-03-08 : 09:28:54
Here's the code:


CREATE PROCEDURE dbo.ap_get_pattern_from_partnumber 

@partnumber varchar(50)

AS

Begin

set nocount on

create table #temp (pattern_code varchar(6), block_index int, value_code varchar(20), value_index int)
create table #temp2 (pattern_code varchar(6))

insert into #temp
SELECT pattern_code, block_index, value_code, value_index
FROM sdb_block_value a
join
sdb_pattern_block b
on
a.block_id=b.block_id
and
pattern_code in ('2139', '2170', '2172', '2186')
order by block_index, value_index

--Grab distinct pattern codes
Declare cursID cursor for
select distinct(pattern_code) from #temp

Declare @curPattern varchar(6),
@isMatch_Value bit,
@isMatch_Block bit,
@isMatch_Pattern bit,
@curPartNumber varchar(50)

set @isMatch_Value=0
set @isMatch_Block=0
set @isMatch_Pattern=0

--Begin First Loop
open cursID
fetch next from cursid into @curPattern
while (@@fetch_status<>-1)
Begin

Declare Cursid2 cursor for
select block_index from #temp
where pattern_code=@curPattern

Declare @curIndex int,
@curmatch varchar,
@minlength int

set @curmatch=''
set @minlength=0

--Begin Second Loop
open cursid2
fetch next from cursid2 into @curIndex
while (@@fetch_status<>-1)
Begin

Declare cursid3 cursor for
select value_index from #temp
where pattern_code=@curPattern
and block_index=@curIndex

Declare @curvIndex int

--Begin Third loop
Open cursid3
fetch next from cursid3 into @curVindex
while (@@fetch_status<>-1)
Begin
select @minlength=len(value_code)
from #temp
where pattern_code=@curPattern
and block_index=@curIndex
and value_index

select @curPartNumber=@curPartNumber + value_code
from #temp
where pattern_code=@curpattern
and block_index=@curIndex
and value_index=@curVindex

if @curPartNumber=substring(@partnumber, 0, len(@curPartNumber))
begin
set @isMatch_Value=1
end

if @isMatch_value=1
Begin
set @isMatch_value=0
fetch next from cursid3 into @curVindex
set @isMatch_Block=1
end
else
begin
set @isMatch_value=0
set @isMatch_Block=0
set @curPartNumber=''
end

end
deallocate cursid3

if @isMatch_block=1
Begin
set @isMatch_Pattern=1
fetch next from cursid2 into @curIndex
end
else
Begin
set isMatch_pattern=0
set isMatch_block=0
end

end
deallocate cursid2

if @isMatch_Pattern=1
begin
insert into #temp2 (@curPattern)
end

fetch next from cursid into @curPattern
end
deallocate cursid



End


The errors I'm throwing are:

Error 156: Incorrect syntaxt near 'select'
Line 114: Incorrect syntaxt near '='
Incorrect syntaxt near the keyword 'end'

Line 114 is "set isMatch_pattern=0" and that looks fine so I'm really not sure what to do here.

Anyone got any ideas where I've gone wrong?

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-03-08 : 09:36:34
This function likely does not require the use of cursors. Can you post some input data and desired output results?
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-03-08 : 09:39:22
lol - I refraine from commenting on the code but point to
http://www.nigelrivett.net/Cursors.html

set isMatch_pattern=0
set isMatch_block=0
s.b.
set @isMatch_pattern=0
set @isMatch_block=0


insert into #temp2 (@curPattern)
s.b.
insert into #temp2 select @curPattern



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-03-08 : 09:58:25
Nested cursors!

Love them!

They keep me in business....

How long do you think that'll take to run?

Collapse the cursors and selects in to 1 statement...



Brett

8-)
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2004-03-08 : 10:34:55
quote:
Originally posted by nr
set isMatch_pattern=0
set isMatch_block=0
s.b.
set @isMatch_pattern=0
set @isMatch_block=0


insert into #temp2 (@curPattern)
s.b.
insert into #temp2 select @curPattern



That took care of 2 out of the three problems. I'm still getting a "Error 156: Incorrect syntaxt near the keyword 'select'

As for the cursors, if there's a better way to do what I'm trying to do, by all means let me know. I'll start a new thread called "do I need cursors or not".






Go to Top of Page

ChrisFretwell
Starting Member

43 Posts

Posted - 2004-03-08 : 12:27:37
When I try to compile, it gives the select error as on this line (line 80)

select @curPartNumber=@curPartNumber + value_code
from #temp
where pattern_code=@curpattern
and block_index=@curIndex
and value_index=@curVindex

Indicating that its not ready for a select following the previous statement. So, looking at the previous statement, you have a hanging where
select @minlength=len(value_code)
from #temp
where pattern_code=@curPattern
and block_index=@curIndex
and value_index

and value_index what??? Fix this and all should be well.

Chris
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2004-03-08 : 13:12:59
quote:
Originally posted by ChrisFretwell

When I try to compile, it gives the select error as on this line (line 80)

Indicating that its not ready for a select following the previous statement. So, looking at the previous statement, you have a hanging where
select @minlength=len(value_code)
from #temp
where pattern_code=@curPattern
and block_index=@curIndex
and value_index

and value_index what??? Fix this and all should be well.




That was it! Thanks.

And it was such a stupid error on my part.

Go to Top of Page
   

- Advertisement -