Pass a string into SP and parse on space

  • 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.

  • 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.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • 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...

  • I'm sorry, but we really can't answer that question for you. If you post the stored procedure we can look at it to see if there's anything specific to the proc that's causing your error message. Other than that, we are out of suggestions.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • venoym (5/10/2013)

    I think the likely scenario is that the vb code is doing something like this:

    ocmd = ADODB.Command

    ocmd.CommandText = "EXEC dbo.[Procedure] " + sVariable

    ocmd.Execute()

    If you're passing the parameter value into the CommandText in-line, you should try wrapping it in single quotes like this:

    ocmd.CommandText = "EXEC dbo.[Procedure] " + "'" + sVariable + "'"

    This isn't a best practice - it makes your data layer susceptible to SQL Injection attacks. You'd do better passing the value in through a strongly typed parameter. But this should fix the immediate problem.

    Unfortunately, I'm not sure you can fix this without making some change to the VB code - without the single quotes the SP will consume ABC as the parameter value and throw the exception when it gets to the characters after the space (which sounds like exactly what you're seeing).

  • 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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • ...

    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!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • 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.

  • 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!

Viewing 10 posts - 16 through 24 (of 24 total)

You must be logged in to reply to this topic. Login to reply