4

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?

unknown
  • 4,859
  • 10
  • 44
  • 62
Andrew Vershinin
  • 147
  • 3
  • 3
  • 10

1 Answers1

6

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
user3358344
  • 193
  • 1
  • 9