Solved

ASP page, stored procedure, dynamic where clause problem

Posted on 2006-10-21
3
241 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 500 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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

688 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