This is a classic SQL Injection target! You should be glad that you found it now.
Back to your problem, when users enter '*'
, replace it with '%'
, and use LIKE
instead of =
in your query.
For example, when end-users enter "US*123"
, run this query:
SELECT * FROM table WHERE companyID LIKE @companyIdTemplate
set @companyIdTemplate
parameter to "US%123"
, and run the query.
I used .NET's @
in the example, but query parameters are denoted in ways specific to your hosting language. For example, they become ?
in Java. Check any DB programming tutorial on use of parameterized queries to find out how it's done in your system.
EDIT : If you would like to perform an insert based on a wildcard that specifies records in another table, you can do an insert-from-select, like this:
INSERT INTO CompanyNotes (CompanyId, Note)
SELECT c.companyId, @NoteText
FROM Company c
WHERE c.companyId LIKE 'G378%'
This will insert a record with the value of the @NoteText
parameter into CompanyNotes
table for each company with the ID matching "G378%"
.