SQL存储过程:条件返回

Dav*_*idS 1 sql branch stored-procedures

嗨,我想创建一个简单的存储proecudre,它执行以下操作:

Psudocode

@tempSelect = "SELECT * FROM Table"

if (@Param is NULL) then
 exec @tempSelect
else
 exec @tempSelect + ' WHERE id = ' + @Param + '
Run Code Online (Sandbox Code Playgroud)

这种方法有效吗?谢谢.

Joh*_*yre 10

尝试

select *
from table
where id=isnull(@param, id)
Run Code Online (Sandbox Code Playgroud)