Guest_imported
New member
- Jan 1, 1970
- 0
I have a stored proc I am writing in SQL Server2000 that needs to pass an unpredictably long number of values as a parameter to be used in an in() statement in the stored procedure. I have a stored proc that properly creates the string, but doesn't want to return the records. If I run a 'print' command to display the variable and then cut-and-paste it into the stored proc (as hard-coded), it works fine. What do I need to do to get SQL Server to understand that I am passing it a string to use in an in() statement?
example of stored procedure call:
ssDOCTORS_PATIENTS '1/1/2000', '12/31/2002', '23-24-28-67'
sample of stored procedure:
CREATE PROCEDURE test_proc
@StartDate As DateTime,
@EndDate As DateTime,
@aryProviders As varchar(20)
AS
/* Process the values in the array*/
DECLARE
@returnval varchar(50),
@nCntStart int,
@nCntEnd int,
@nLen int,
@nGetItem int,
@csfProviders varchar(20),
@sTmp Varchar(10),
@sItem VarChar(10)
BEGIN
select @returnval = 0
select @csfProviders = ''
select @aryProviders = @aryProviders + '-'
/* BEGIN TRANSACTION AddOrderTrans */
if (@@ERROR <> 0) Goto OnErrorTrap
/* get the length of the @items_ordered_Array string for later use. */
select @nLen = Len(@aryProviders)
/* set our counters to start with 1 as that will be the first position in the delimited string that we look at */
select @nCntStart = 1
select @nCntEnd = 1
/* enter into a loop where we will check each character in the @aryProviders string. */
WHILE (@nCntEnd <= @nLen)
BEGIN
/* Get the current character and store it in the temp variable @sTmp */
select @sTmp = SUBSTRING(@aryProviders,@nCntEnd,1)
/* if we find the '-' character, then we insert the value into the csfProviders variable*/
if (@sTmp = '-')
BEGIN
/* Get the value between our counter markers */
select @sItem = SUBSTRING(@aryProviders, @nCntStart, @nCntEnd - @nCntStart)
/* make sure there are no trailing blanks in the variable */
select @sItem = ltrim(rtrim(@sItem))
/* Insert this item into the storage array. */
/* select @csfProviders = @csfProviders + quotename(@sItem, '''') + ', '
*/ select @csfProviders = @csfProviders + '''' + @sItem + '''' + ', '
if (@@ERROR <> 0) Goto OnErrorTrap
/* reset the variables back to an empty string and prepare for another record */
select @sItem = ''
/* reset the start counter to the position after the current end position */
select @nCntStart = @nCntEnd + 1
END
/* increment our loop counter and move on to
the next value in the items_ordered_Array
string */
select @nCntEnd = @nCntEnd + 1
END
/* Strip the last 2 characters off the string */
select @csfProviders = Substring(@csfProviders, 1, datalength(@csfProviders) - 2)
/* if we get here (after the while loop),
all has gone well and we can commit the transaction */
/* COMMIT TRANSACTION AddOrderTrans */
goto OnExit
OnErrorTrap:
OnExit:
/* Enter values into the query */
/*select distinct res_last_name, res_first_name, res_mi_name, pr_address_line1, pr_address_line2, pr_city, pr_state, zip_pc
*/
select count(*)
from patients2 p, patient_alerts, responsible_parties, pat_resp_addresses, zips
where soc_sec = res_soc_sec
and res_soc_sec = pr_soc_sec
and pat_identity = pal_pat_identity
and pr_zip = zip_pc
and pr_address_label = 'H'
and primary_guarantor = 'Y'
and deceased_date is null
and pal_alert != 'deceased'
and exists (select patient_id from patservices
where patient_id = p.patient_id
and service_date >= @StartDate
and service_date <= @EndDate
and prov_id in (@csfProviders))
print @csfProviders
END
GO
>>>>Result<<<<<<<<
prints '23', '24', '28', '67'
Returns 0 records
example of stored procedure call:
ssDOCTORS_PATIENTS '1/1/2000', '12/31/2002', '23-24-28-67'
sample of stored procedure:
CREATE PROCEDURE test_proc
@StartDate As DateTime,
@EndDate As DateTime,
@aryProviders As varchar(20)
AS
/* Process the values in the array*/
DECLARE
@returnval varchar(50),
@nCntStart int,
@nCntEnd int,
@nLen int,
@nGetItem int,
@csfProviders varchar(20),
@sTmp Varchar(10),
@sItem VarChar(10)
BEGIN
select @returnval = 0
select @csfProviders = ''
select @aryProviders = @aryProviders + '-'
/* BEGIN TRANSACTION AddOrderTrans */
if (@@ERROR <> 0) Goto OnErrorTrap
/* get the length of the @items_ordered_Array string for later use. */
select @nLen = Len(@aryProviders)
/* set our counters to start with 1 as that will be the first position in the delimited string that we look at */
select @nCntStart = 1
select @nCntEnd = 1
/* enter into a loop where we will check each character in the @aryProviders string. */
WHILE (@nCntEnd <= @nLen)
BEGIN
/* Get the current character and store it in the temp variable @sTmp */
select @sTmp = SUBSTRING(@aryProviders,@nCntEnd,1)
/* if we find the '-' character, then we insert the value into the csfProviders variable*/
if (@sTmp = '-')
BEGIN
/* Get the value between our counter markers */
select @sItem = SUBSTRING(@aryProviders, @nCntStart, @nCntEnd - @nCntStart)
/* make sure there are no trailing blanks in the variable */
select @sItem = ltrim(rtrim(@sItem))
/* Insert this item into the storage array. */
/* select @csfProviders = @csfProviders + quotename(@sItem, '''') + ', '
*/ select @csfProviders = @csfProviders + '''' + @sItem + '''' + ', '
if (@@ERROR <> 0) Goto OnErrorTrap
/* reset the variables back to an empty string and prepare for another record */
select @sItem = ''
/* reset the start counter to the position after the current end position */
select @nCntStart = @nCntEnd + 1
END
/* increment our loop counter and move on to
the next value in the items_ordered_Array
string */
select @nCntEnd = @nCntEnd + 1
END
/* Strip the last 2 characters off the string */
select @csfProviders = Substring(@csfProviders, 1, datalength(@csfProviders) - 2)
/* if we get here (after the while loop),
all has gone well and we can commit the transaction */
/* COMMIT TRANSACTION AddOrderTrans */
goto OnExit
OnErrorTrap:
OnExit:
/* Enter values into the query */
/*select distinct res_last_name, res_first_name, res_mi_name, pr_address_line1, pr_address_line2, pr_city, pr_state, zip_pc
*/
select count(*)
from patients2 p, patient_alerts, responsible_parties, pat_resp_addresses, zips
where soc_sec = res_soc_sec
and res_soc_sec = pr_soc_sec
and pat_identity = pal_pat_identity
and pr_zip = zip_pc
and pr_address_label = 'H'
and primary_guarantor = 'Y'
and deceased_date is null
and pal_alert != 'deceased'
and exists (select patient_id from patservices
where patient_id = p.patient_id
and service_date >= @StartDate
and service_date <= @EndDate
and prov_id in (@csfProviders))
print @csfProviders
END
GO
>>>>Result<<<<<<<<
prints '23', '24', '28', '67'
Returns 0 records