Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing string in sproc

Status
Not open for further replies.

ChopinFan

Technical User
Joined
Oct 4, 2004
Messages
149
Location
US
I'm having problems parsing a string that is passed to this stored procedure. It works fine (creates the view) when only one StudentNumber is passed to it, but when multiple numbers are passed (separated by commas with no spaces), it returns no records. How can I do this differently?

ALTER PROCEDURE sp_SprocName

@StudentNumber varchar(500)

AS

Declare @inCriteria as Varchar(500)
Set @inCriteria = '''' + (Replace(@StudentNumber ,',',''',''' )) + ''''
declare @SQL as varchar (2000)

set @SQL = 'SELECT ... WHERE (StudentNumberField = '+@inCriteria+')'

Declare @MyView as varchar (5000)
Set @MyView ='alter view vw_ViewName as ' +@SQL
Exec (@MyView)
 
Check out htis thread thread183-1167924

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

Thanks! Good info. I'll post again if I run into trouble.
 
Trouble in paradise...
I'm trying out option 1 of nocoolhandle's link first as it seems the easiest. I'm getting an odd message though and I'm reasonably sure it's a dumb mistake on my part.

I made some changes so I'm now passing two variables instead of one. @StudentNumber is a string of parsed four-digit numbers and @Country is a two-letter code.

Here's my front end code passing variables to the sproc:
With Com
.ActiveConnection = Con
.CommandType = adCmdStoredProc
.CommandText = "dbo.sp_MM_TEMPLATE_Update_CreateView"
.Parameters.Append .CreateParameter("@StudentNumber", adVarChar, adParamInput, 500, Trim(StudentNumber.Value))
.Parameters.Append .CreateParameter("@Country", adVarChar, adParamInput, 2, Trim(Country.Value))
End With
Com.Execute , , adExecuteNoRecords

Here is my sproc:
ALTER PROCEDURE sp_MM_TEMPLATE_Update_CreateView

@StudentNumber varchar(500),
@Country varchar(2)
AS

declare @SQL as varchar (5000)
set @SQL = '
Select fieldnames
From tables
WHERE (RIGHT(dbo.tblLDPStudentInfo.LDPStudentID, 4) in('+@StudentNumber+')) and (dbo.vw_LatestPerfGrading.PerfGradeCatID =5) and
(dbo.vw_LatestUpdateInfo.FinalUpdate =0) and (dbo.tblCountry.CountryID in('+@Country+'))'

It's giving me an "invalid column name" error when it gets to the @Country variable. The column exists in the select clause. When I comment out the (dbo.tblCountry.CountryID in('+@Country+'))' line, it works perfectly.

What am I doing wrong?
 
If Country is a character field, you have to inject the quotes around the value, so

(dbo.tblCountry.CountryID in('''+@Country+'''))

should work.

HTH,

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'm not as think as you confused I am.
-----------
Flabbergasted (a.): Amazed at how much weight one has gained.
-----------
Oyster (n.): One who sprinkles their conversation with Yiddish expressions.
 
Also, you list 4 tables/views in your where clause, but a different table in your from.


dbo.tblLDPStudentInfo
dbo.vw_LatestPerfGrading
dbo.vw_LatestUpdateInfo
dbo.tblCountry

You need to list these tables and views in the from clause and join them on a common field.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

Works great! Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top