Showing posts with label fails. Show all posts
Showing posts with label fails. Show all posts

Wednesday, March 7, 2012

Issue with executing dynamic sql with inbound paramter as varchar striong

Hi All:

I am trying to execute a dynamic sql, the dynamic sql makes use of an inbound paramter defined as varchar.

When I try to execute it fails, because it does not plavce the inbound paramter in quotes.

Any help would be appreciated.

In the bound search as an eaxmple can be" 'NY'

@.P_SEARCH_VALUE='NY'

SET @.V_SQL_FILTER = N' WHERE STATE = '+@.P_SEARCH_VALUE

SET @.V_SQL=@.V_BASE_SQL+@.V_SQL_FILTER

EXEC sp_executesql @.V_SQL

Here is the v_sql out put:

SELECT TOP 100 * FROM V$ZIPCODE_LOOKUP_ALL WHERE STATE = NY

As you can see the sql will fail because the NY is not in quotes.

I tried using '@.P_SEARCH_VALUE''' and other forms but could not get it work.

There are a couple of ways (and variations) to approach this issue.

First, and the most 'robust', is to use the capability of sp_executesql to handle parameters. For the best explanition and demonstration, see Erland's article here.

Dynamic SQL - The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html
http://msdn2.microsoft.com/en-us/library/ms188332.aspx
http://msdn2.microsoft.com/en-us/library/ms175170.aspx

Otherwise, you have to devise some scheme to manage quotes. For example, in you code above you are not managing the quotes inside your string. To embed a single quote inside a string, you have to double it up. So your filter would be more like this:

SET @.V_SQL_FILTER = N' WHERE STATE = ''' + @.P_SEARCH_VALUE + ''''

Handling the quotes can seem to get out of hand, and very confusing. For that reason, the first option is the best.

|||

Since you are using the sp_executesql, itself supports the parameterized quires. You need not to concatenate those values (contaminating values may cause sql injection).

You can achieve the same result using the following query,

Code Snippet

SET @.P_SEARCH_VALUE='NY'

SET @.V_SQL_FILTER = N' WHERE STATE = @.P_SEARCH_VALUE'

SET @.V_SQL = @.V_BASE_SQL+@.V_SQL_FILTER

SET @.V_PARAM = N'@.P_SEARCH_VALUE VARCHAR(100)'

EXEC sp_executesql @.V_SQL, @.V_PARAM , @.P_SEARCH_VALUE

Sample,

Code Snippet

Declare @.SQL as Nvarchar(1000);

Declare @.Value as varchar(100);

Declare @.Param as Nvarchar(100);

Set @.SQL = N'Select * from sysobjects where name=@.value'

Set @.Param = N'@.value varchar(100)'

Set @.Value = 'sysobjects'

exec sp_executesql @.SQL, @.Param, @.value

|||

For something like this:

SET @.V_SQL_FILTER = N' WHERE STATE = ''' + @.P_SEARCH_VALUE + ''''

You can use:

declare @.p_search_value nvarchar(20)

set @.p_search_value = 'This is a quote '''

select N' WHERE STATE = ' + quoteName(@.P_SEARCH_VALUE ,'''')

It is best to use sp_executeSQL, but if you have to work with quotes, this is the best tool for the job.|||try the following use three single quotes

@.P_SEARCH_VALUE='''NY'''

SET @.V_SQL_FILTER = N' WHERE STATE = '+@.P_SEARCH_VALUE

SET @.V_SQL=@.V_BASE_SQL+@.V_SQL_FILTER

EXEC sp_executesql @.V_SQL

Monday, February 20, 2012

ISQL query fails after reboot

Good morning we are having a wierd issue with our Windows 2003 server and SQ
L
2000 sp4 server. From the client side we use isql queries. When we reboot
the server the isql queries that are larger then 512 bytes will fail from
both the client or from the sql server itself smaller ones will work. Query
analyzer works perfectly. The strange part is if we run any query in either
query analyzer or using isql then restart the services everything works. We
also have another server which appears to be identical and everything works
with no issues. If anyone can point me in the right direction to
troubleshoot this issue it would be greatly appreciated.
Richard TracyHave you tried using OSQL instead of ISQL?
In terms of why you are having problems, that's hard to say
without knowing what error message you receive when running
the queries that fail. As a guess, it sounds like you could
be running into issues related to ISQL and the packet size.
Try using the -a switch and increase the default packet
size.
-Sue
On Wed, 12 Apr 2006 07:50:02 -0700, Richard Tracy
<RichardTracy@.discussions.microsoft.com> wrote:

>Good morning we are having a wierd issue with our Windows 2003 server and S
QL
>2000 sp4 server. From the client side we use isql queries. When we reboot
>the server the isql queries that are larger then 512 bytes will fail from
>both the client or from the sql server itself smaller ones will work. Quer
y
>analyzer works perfectly. The strange part is if we run any query in eithe
r
>query analyzer or using isql then restart the services everything works. W
e
>also have another server which appears to be identical and everything works
>with no issues. If anyone can point me in the right direction to
>troubleshoot this issue it would be greatly appreciated.
>Richard Tracy|||We have tried using the -a with no success the error we get is a network
error even if we run it on the localhost. We have also uninstalled sql
server and tried it on a plain vanilla sql server with no success. Any idea
s
of how to troubleshoot this would be greatly appreciated
Thank you
"Sue Hoegemeier" wrote:

> Have you tried using OSQL instead of ISQL?
> In terms of why you are having problems, that's hard to say
> without knowing what error message you receive when running
> the queries that fail. As a guess, it sounds like you could
> be running into issues related to ISQL and the packet size.
> Try using the -a switch and increase the default packet
> size.
> -Sue
> On Wed, 12 Apr 2006 07:50:02 -0700, Richard Tracy
> <RichardTracy@.discussions.microsoft.com> wrote:
>
>|||You really have to look at the exact full error messages and
the error number. You can try googling on those or try
posting with the full information. Connectivity issues are
generally difficult to troubleshoot via newsgroups. If you
want to do that, you need to post a lot of details, full
error messages, configurations, exact steps to reproduce,
etc. Even then, it can be difficult via newsgroups.
-Sue
On Thu, 13 Apr 2006 23:18:02 -0700, Richard Tracy
<RichardTracy@.discussions.microsoft.com> wrote:
[vbcol=seagreen]
>We have tried using the -a with no success the error we get is a network
>error even if we run it on the localhost. We have also uninstalled sql
>server and tried it on a plain vanilla sql server with no success. Any ide
as
>of how to troubleshoot this would be greatly appreciated
>Thank you
>"Sue Hoegemeier" wrote:
>|||Thank you for your help Sue. We found what the issue was, we found it was
being caused by an application called NCS (Native Command Substitution. It
is installed with lightspeed which was created by imceda software which has
been bought by quest software. We removed this software and it has worked
without an issue. Again thak you very much for your help
"Sue Hoegemeier" wrote:

> You really have to look at the exact full error messages and
> the error number. You can try googling on those or try
> posting with the full information. Connectivity issues are
> generally difficult to troubleshoot via newsgroups. If you
> want to do that, you need to post a lot of details, full
> error messages, configurations, exact steps to reproduce,
> etc. Even then, it can be difficult via newsgroups.
> -Sue
> On Thu, 13 Apr 2006 23:18:02 -0700, Richard Tracy
> <RichardTracy@.discussions.microsoft.com> wrote:
>
>|||That's interesting one ...thanks for posting back.
-Sue
On Sun, 16 Apr 2006 14:01:02 -0700, Richard Tracy
<RichardTracy@.discussions.microsoft.com> wrote:
[vbcol=seagreen]
>Thank you for your help Sue. We found what the issue was, we found it was
>being caused by an application called NCS (Native Command Substitution. It
>is installed with lightspeed which was created by imceda software which has
>been bought by quest software. We removed this software and it has worked
>without an issue. Again thak you very much for your help
>"Sue Hoegemeier" wrote:
>