Jon Ownbey
Jon Ownbey

Reputation: 1448

Parameterized SQL Query error

I have an application that builds dynamic parameterized SQL queries. The following query returns an inner exception of "syntax error at or near "="...

I am thinking it is in the way that I am assigning the parameter to the column name but, I'm not sure. Code follows.

SELECT TOP 50 77 AS Repository, DocID, LastFileDate, ImageCount,
 LockedUserID,   DocClass, Offline, FileType, OrigFileName, C_CredCardSSN, C_CredCardName,C_CredCardDocType, C_CredCardDocDate, C_CredCardStatus 

FROM D77 

WHERE C_CredCardSSN 

LIKE C_CredCardSSN = @sp1% 

ORDER BY C_CredCardSSN,C_CredCardName

EDIT: Thanks to everyone. All of these answers helped. =)

Upvotes: 1

Views: 428

Answers (4)

scottm
scottm

Reputation: 28699

Try adding the '%' to the text before adding it as a parameter to the query:

SqlParameter p = new SqlParameter("@sp1");
p.value = C_CredCardSSNText + "%";

Upvotes: 1

MarkusQ
MarkusQ

Reputation: 21950

Try just

WHERE C_CredCardSSN LIKE @sp1

or

WHERE C_CredCardSSN LIKE @sp1+'%'

depending on why you had a "%" there (thanks to Joel Coehoorn's comment for pointing out the "%" I'd missed).

Or (in response to your comment) maybe:

WHERE C_CredCardSSN LIKE ''+@sp1

Upvotes: 5

Eppz
Eppz

Reputation: 3226

Try this:

WHERE C_CredCardSSN like @sp1 + '%'

This will only find your C_CredCardSSN if the first few characters are correct. To find anything within that value try it like this

WHERE C_CredCardSSN like '%' + @sp1 + '%'

Upvotes: 2

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

The LIKE statement should look like this:

WHERE C_CredCardSSN LIKE @sp1 + '%'

Upvotes: 4

Related Questions