Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard as parameter of stored procedure

Suppose, I have a stored procedure, that accepts one parameter - id, and returns some data corresponding to this id. What if I want to retrieve all such data, like using wildcard in SELECT query? How to do this?

like image 343
Andrew Vershinin Avatar asked Sep 05 '25 19:09

Andrew Vershinin


1 Answers

You can add a trailing '%' to your query. Assume that @param is the parameter to your stored procedure:

declare @param2 varchar(100)
set @param2 = @param + '%'

select * from table where column like @param2

That will return a wildcard search beginning with the value in @param. For partial match use '%' + @param + '%'

[Edit]

Based on the below clarification in the comments:

if @id != '*' 
begin
    select * from table where column = @id
end
else
begin
    select * from table
end
like image 186
user3358344 Avatar answered Sep 08 '25 11:09

user3358344