How to use parameter with LIKE in Sql Server Compact Edition

前端 未结 5 1406
后悔当初
后悔当初 2020-12-17 19:07

I\'m trying to parameterise a search query that uses the LIKE keyword with a wildcard. The original sql has dynamic sql like this:

\"AND JOB_POSTCODE LIKE \'         


        
5条回答
  •  时光取名叫无心
    2020-12-17 19:55

    This returns appropriate results in SQL Server 05 (also works wrapped in a SP), so it seems like the last thing you tried should have worked. But I don't have a test bed for Compact Edition, so maybe that makes a difference (I'd be curious to see if that's so, and why).

    declare @p1 nvarchar(50)
    set @p1 = 'f'         -- initial value passed from your app
    set @p1 = @p1 + '%'   -- edit value for use with LIKE
    select * from JOB
    where JOB_POSTCODE like @p1
    

    EDIT:

    What version of SQLCE are you using? Can you tell if your parameter values are actually being passed from your code to the DB? I skimmed through this MSDN tutorial and was able to get the results you're looking for, at least from the Visual Studio Query Designer. (only difference is that I'm using VS 2008 and SQL Server Compact 3.5). See the section titled "Creating a new query"; I mocked up a table with some data and this query worked as you intend.

    SELECT     JobID, PostCode, OtherValue
    FROM         Job
    WHERE     (PostCode LIKE @p1 + '%')
    

    Like I said, I didn't write any code to call the query, but it worked from within the designer. In other words, "it works on my machine".

提交回复
热议问题