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
 Transact-SQL (2000)
 Using Parameters in Stored Procedures

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-05-26 : 07:04:47
NarryB writes "I have a table (a lil short of 4 million records) with zip code values in a column. I need to run a count and also extract the records that match a set of zip codes - this set varies every time a search is submitted. I copied this code and modified it to output counts for zip codes, however, the sp returns only the first value entered for the "Array" parameter. Please help me. How can I achive my objective?
------------------------------------
CREATE procedure sp_ZipCode_Count
( @Array varchar(1000),
@Separator char(1) )
AS
set nocount on
-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
declare @separator_position int -- This is used to locate each separator character
declare @array_value varchar(5) -- this holds each array value as it is returned

-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
set @array = @Array + @Separator

-- Loop through the string searching for separator characters
while patindex('%' + @separator + '%' , @array) <> 0
begin

-- patindex matches the a pattern against a string
select @separator_position = patindex('%' + @separator + '%' , @array)
select @array_value = left(@array, @separator_position - 1)

-- This is where you process the values passed.
-- Replace this select statement with your processing
-- @array_value holds the value of this element of the array
select Array_Value = @array_value
while @array_value <> '0'
SELECT count(*) FROM customer
WHERE zip in (@array_value)
-- This replaces what we just processed with and empty string
select @array = stuff(@array, 1, @separator_position, '')
--end
end
set nocount off
GO"

mtomeo
Starting Member

30 Posts

Posted - 2004-05-26 : 09:01:30
Maybe I'm missing the real issue here, but I don't understand the use of the "while @array_value <> '0'" (or the line above if for that matter...is something missing?). Would this work? Replace the while @array_value... line with an IF statement:

CREATE procedure sp_ZipCode_Count
( @Array varchar(1000),
@Separator char(1) )
AS
set nocount on
-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
declare @separator_position int -- This is used to locate each separator character
declare @array_value varchar(5) -- this holds each array value as it is returned

-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
set @array = @Array + @Separator

-- Loop through the string searching for separator characters
while patindex('%' + @separator + '%' , @array) <> 0
begin

-- patindex matches the a pattern against a string
select @separator_position = patindex('%' + @separator + '%' , @array)
select @array_value = left(@array, @separator_position - 1)

-- This is where you process the values passed.
-- Replace this select statement with your processing
-- @array_value holds the value of this element of the array
select Array_Value = @array_value -- ???
IF len(@array_value) > 0
begin
SELECT count(*) FROM customer
WHERE zip in (@array_value)
end

-- This replaces what we just processed with and empty string
select @array = stuff(@array, 1, @separator_position, '')
--end
end
set nocount off
GO
Go to Top of Page
   

- Advertisement -