?
Solved

ASP page, stored procedure, dynamic where clause problem

Posted on 2006-10-21
3
Medium Priority
?
242 Views
Last Modified: 2012-06-27
MS SQL Server 2000 (remotely hosted by others)
ASP

Hi, and thank you very much, in advance, for any help you can offer!

I am able to retrieve records from several stored procedures on an ASP page, so there should be no connectivity issues.

But, my big headache is a stored procedure that has a dynamic WHERE clause. In trying to troubleshoot the problem, I have used Response.Write to get an exact copy of the parameter, and I have executed the stored procedure (using MS SQL Server Management Studio), and have pasted that exact phrase as the "Value." It works!

But when I try to run it from the ASP page, I get the following error:
=================================================
Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver]Invalid character value for cast specification

/RCTSearchResults.asp, line 1143
=================================================

Just for reference, here are lines 1142, and line 1143. 1143 is the .Open(), and that's where it fails:
rsInvItems.Source = "{call dbo.sp7smInvDynSql37(" + strSQLSourceString +  ")}"
rsInvItems.Open ()

I have looked up the error by code and by searching, to no avail. As far as I can tell, there is no "difficult" type casting (like dates) - I'm using only integers (tinyint and smallint integers.) Obviously, sending a string with numbers in the string forces SQL Server to typecast text strings into integers, but remember, the exact same phrase will work as the value parameter when input at the server.

Here's the latest attempt at the dynamic WHERE clause that is the parameter:

strSQLSourceString = ((dbo.tblInv.intInvDisplay = 1) AND (fk_intInvSpecID IN (3)) AND (fk_intInvStatID = 2) ) ORDER BY strItemTypeN, strSpecName, strPatt, fk_intInvStatID DESC, curInvPrice DESC

Note that I am using the "IN" operator, in case that helps to figure this out.

Finally, so anyone willing to help will have everything, here is enough of the stored procedure to give you an idea if anything is wrong. You'll see that I tried both ways to execute, and I have tried declaring the @SQLStatement as both varchar and nvarchar. The stored proc is kind of big, so I snipped out what would probably not be necessary to see for troubleshooting:

=======================================================

ALTER Procedure [dbo].[sp7smInvDynSql37] (@DynSQLString varchar(1000) )

AS

DECLARE @SQLStatement nvarchar(4000)

set @SQLStatement = 'SELECT dbo.tblInv.pk_intInvIID,
    dbo.tblItemType.strItemTypeName,
    dbo.tblSpecies.strSpecName,

   (snip)

    dbo.tblInvItemStatus.strInvItemStatus
FROM dbo.tblInv INNER JOIN
    dbo.tblItemType ON

   (snip)

 WHERE ' + '  ' + @DynSQLString  + ' '
 
-- EXEC sp_executesql @SQLStatement
EXEC (@SQLStatement)

=====================================

Again, thanks for any help you can offer.

Dennis
0
Comment
Question by:dtleahy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 17780520
you need single quotes around the string value:

rsInvItems.Source = "{call dbo.sp7smInvDynSql37('" + strSQLSourceString +  "')}"


I assume that the following line:
strSQLSourceString = ((dbo.tblInv.intInvDisplay = 1) AND (fk_intInvSpecID IN (3)) AND (fk_intInvStatID = 2) ) ORDER BY strItemTypeN, strSpecName, strPatt, fk_intInvStatID DESC, curInvPrice DESC
is actually:

strSQLSourceString = "((dbo.tblInv.intInvDisplay = 1) AND (fk_intInvSpecID IN (3)) AND (fk_intInvStatID = 2) ) ORDER BY strItemTypeN, strSpecName, strPatt, fk_intInvStatID DESC, curInvPrice DESC "

0
 

Author Comment

by:dtleahy
ID: 17780806
Thast's it!

AngelIII, you are indeed an angel! And, a bright one too! Thank you VERY much! I am happy to award the 500 points, and a big cyber-hug to you!

Dennis
0
 

Author Comment

by:dtleahy
ID: 17780817
"Thast's it!" = "That's it!"
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question