Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Pass a string into SP and parse on space Expand / Collapse
Author
Message
Posted Friday, May 10, 2013 8:13 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 2,749, Visits: 7,153
j2cagle (5/10/2013)
By "lost" I mean...the code is out there running, and we have versions of the source code in multiple locations. These multiple locations don't appear to be the version that is out there running. So, I was attempting to catch and correct the issue in the called SQL.

Just to reiterate...

I'm just looking for a way, that when passed "ABC DEF GHI", I could handle it in SQL to simply look only at the value up to the first space.

If that can't be done, that's fine...I was just doing some investigation...


How is it being passed currently. How is that data getting into the VB program? Is it a text box in an interface or from a file or from another database. This is important if you want to do this without changing code in VB.

Once you get it in there, you'll want to use
SUBSTRING(@TheVariable, 1, CHARINDEX(' ', @TheVariable) - 1)

The error you're getting is because when the ABC DEF GHI gets to your stored proc, there's no single quotes around it, so DEF is considered the beginning of another statement.


--------------------------------------
When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
--------------------------------------
It’s unpleasantly like being drunk.
What’s so unpleasant about being drunk?
You ask a glass of water. -- Douglas Adams
Post #1451616
Posted Friday, May 10, 2013 8:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:58 AM
Points: 12,877, Visits: 31,791
Brandie Tarvin (5/10/2013)
j2cagle (5/10/2013)
Yes, Say Hey Kid, it's very similar to what you posted. I agree with a previous post...the real issue is in the VB code. However, I wanted to determine if I could handle this situation within the SQL code. The VB code is potentially lost and not re-buildable, unfortunately.


Until you can get the VB code to push the correct value to SQL Server, there's nothing T-SQL can do to rectify the problem.

When you say "lost," what exactly do you mean? I assume there's a working version of it somewhere or you wouldn't be getting anything at all from it.


you are correct, in this case, it cannot be done, because the error is coming from the syntax parsing of the statement when it hits the server; it doesn't get "inside" the stored procedure where you could manipulate the parameters.

the code needs to change in vb6 to use a parameter instead of creating and executing a string; only if that was done would you be able to chop the passed values inside the procedure:
"EXEC dbo.[Procedure] " + sVariable
--becomes
EXEC dbo.[Procedure] @MyStringParameter



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1451618
Posted Friday, May 10, 2013 8:16 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062

...
Once you get it in there, you'll want to use
SUBSTRING(@TheVariable, 1, CHARINDEX(' ', @TheVariable) - 1)

...


Just in case if you will need to pass string without any space:

SELECT LEFT(val,ISNULL(NULLIF(CHARINDEX(' ', val) - 1,-1),LEN(val)))
FROM (VALUES ('ABC DEF GHI'),('WDR')) q(val)



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1451619
Posted Friday, May 10, 2013 8:16 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 290, Visits: 1,657
j2cagle (5/10/2013)

I'm just looking for a way, that when passed "ABC DEF GHI", I could handle it in SQL to simply look only at the value up to the first space.

If that can't be done, that's fine...I was just doing some investigation...


Don't think it can be done (at least not in any way I'm familiar with - which might not be setting the bar very high).

You're basically sending in the string without the required single quotes. SQL will consume up to the first space without complaining, but once it hits characters after that space it will throw an exception. So you need a fix at the point Procedure is being called.
Post #1451620
Posted Friday, May 10, 2013 8:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 7, 2014 11:14 AM
Points: 19, Visits: 22
Thanks everyone! What everyone is describing is exactly what is happening...with the space followed by more text error. I appreciate everyone's input on this...it was very educational for me!
Post #1451627
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse