3

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?

4

1 回答 1

5

您可以在查询中添加尾随 '%'。假设这@param是您的存储过程的参数:

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

select * from table where column like @param2

这将返回以@param 中的值开头的通配符搜索。对于部分匹配使用'%' + @param + '%'

[编辑]

基于评论中的以下说明:

if @id != '*' 
begin
    select * from table where column = @id
end
else
begin
    select * from table
end
于 2014-03-02T10:56:24.227 回答