SQL 存储过程:条件返回

发布于 2024-07-15 02:15:19 字数 233 浏览 7 评论 0原文

您好,我想创建一个简单的存储过程,它执行以下操作:

Psudocode

@tempSelect = "SELECT * FROM Table"

if (@Param is NULL) then
 exec @tempSelect
else
 exec @tempSelect + ' WHERE id = ' + @Param + '

这种方法有效吗? 谢谢。

Hi I want to create a simple stored proecudre which does the following:

Psudocode

@tempSelect = "SELECT * FROM Table"

if (@Param is NULL) then
 exec @tempSelect
else
 exec @tempSelect + ' WHERE id = ' + @Param + '

Is this method efficent? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆追雨的时光 2024-07-22 02:15:20

尝试

select *
from table
where id=isnull(@param, id)

Try

select *
from table
where id=isnull(@param, id)
萌酱 2024-07-22 02:15:20

从表中选择 *
其中(ID = @Param 或 @Param 为空)

从表中选择 *
其中 ID=Coalesce(@Param, ID)

[如果您追求效率,请将 * 替换为您要返回的特定字段。]

Select * from Table
Where (ID = @Param or @Param is null)

Or

Select * from Table
Where ID=Coalesce(@Param, ID)

[And if you are aiming for efficiency, replace * with the specific field you want to return.]

我一直都在从未离去 2024-07-22 02:15:20

是的——我当然认为这没有什么问题。 不过,您可以使其变得更简单:

Set NOCOUNT On;
if (@Param is NULL) 
   Select * From Table;
else 
   Select * From Table Where (ID=@Param);

注意:不过,我可能会拼写出这些字段。

Yes - I certainly see nothing wrong with it. You could make it even simpler though:

Set NOCOUNT On;
if (@Param is NULL) 
   Select * From Table;
else 
   Select * From Table Where (ID=@Param);

Note: I'd probably spell out the fields, though.

意犹 2024-07-22 02:15:20

根据具体情况,我可能会使用动态 SQL。

但是,您需要记住 SQL 注入,以防 @param 源自用户,这就是为什么您不应该直接向 sql 添加参数。

在 t-sql 中,它看起来像(在我的脑海中且未经测试;):

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = N'
  SELECT ...
  FROM   table t
  WHERE  1 = 1' (

IF(@param IS NOT NULL)
  SET @SQL = @SQL + '
    AND t.id = @id'

... possibly more things added to the query ...

EXEC sp_executesql 
   @SQL
, '@id AS INT'
,  @id = @Param

通过这样做,您将为每种情况获得优化的查询计划(并且通过使用 sp_executesql,查询缓存也将被使用)

我会特别要避免使用 OR 解决方案,如果您检查使用 OR 生成的查询计划与不使用 OR 生成的查询计划进行比较,您就会明白原因。

Depending on the case, I would probably use dynamic SQL.

However you need to remember about SQL injection in case @param originates from a user, thats why you should never add a parameter directly to your sql.

In t-sql it would look something like (out of my head and untested ;):

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = N'
  SELECT ...
  FROM   table t
  WHERE  1 = 1' (

IF(@param IS NOT NULL)
  SET @SQL = @SQL + '
    AND t.id = @id'

... possibly more things added to the query ...

EXEC sp_executesql 
   @SQL
, '@id AS INT'
,  @id = @Param

By doing this, you will get an optimized query plan for each case (and by using sp_executesql, the query cache will be used as well)

I would especially avoid the OR solution, if you check the query plans generated with the OR compared to one without, you will understand why.

流年里的时光 2024-07-22 02:15:20

试试这个代码:

CREATE PROCEDURE [dbo].[ProcedureName]
@Param varchar(50)

AS
BEGIN

declare @tempSelect nvarchar(max)  

SET NOCOUNT ON;

set @tempSelect = 'SELECT Col1, Col2 FROM Table where Col1 <> '' '

         if @Param <> '' 
    begin
    set @resultSet = @resultSet +  ''' and Col1='''+@Param1 
    end


EXEC(@resultSet)

END

Try this code:

CREATE PROCEDURE [dbo].[ProcedureName]
@Param varchar(50)

AS
BEGIN

declare @tempSelect nvarchar(max)  

SET NOCOUNT ON;

set @tempSelect = 'SELECT Col1, Col2 FROM Table where Col1 <> '' '

         if @Param <> '' 
    begin
    set @resultSet = @resultSet +  ''' and Col1='''+@Param1 
    end


EXEC(@resultSet)

END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文