相当于LINQ在T-SQL中的延迟执行

时间:2014-03-05 09:09:09

标签: c# linq tsql

在LINQ中,可以利用延迟执行在设计时将彼此的不同关注分开。我的提供程序将生成一个最佳的SQL语句来获取数据。请考虑以下示例:

// some complex query to fetch matches goes here
IEnumerable<Match> data = null;

if (/* some condition */)
{
  data = from m in Context.Matches
         where /* ... */
         select m;
}
else 
{
  data = from m in Context.Matches
         join /* .. */
         join /* .. */
         where /* ... */
         select m;
}

// later on, order the data depending on some user selection
IOrderedEnumerable<Match> orderedData = null;

switch (someOption) 
{
  case /*...*/:
      orderedData = from m in data orderby by m.Start select m;
      break;
  case /*...*/
      orderedData = from m in data orderby by m.ID select m;
      break;
}

// do something with the computed data

是否有一种有效的方法可以在存储过程中实现相同的功能而无需将查询构建为字符串?我能想到的只是表变量,但我不知道它们对性能的影响:

CREATE @matches TABLE 
(
  ID int,
  -- ...
  Start datetime
)


-- some complex query to fetch matches goes here
IF -- some condition
  INSERT INTO @matches(ID, Start) 
       SELECT ID, ..., Start FROM Matches WHERE ...
ELSE
  INSERT INTO @matches(ID, Start) 
       SELECT ID, ..., Start FROM Matches JOIN ... JOIN ... WHERE ...

-- later on, order the data depending on some user selection
CREATE @orderedMatches TABLE 
(
  ID int,
  -- ...
  Start datetime
)

IF -- some option
  INSERT INTO @orderedMatches (ID, Start) 
       SELECT ID, ..., Start FROM @matches ORDER BY Start ASC
ELSE IF -- some option
  INSERT INTO @orderedMatches (ID, Start) 
       SELECT ID, ..., Start FROM @matches ORDER BY ID ASC

-- do something with the computed data

1 个答案:

答案 0 :(得分:2)

T-SQL中不存在延迟执行。您使用的任何声明都将立即执行。最接近的是动态SQL:从片段构建包含逻辑的SQL文本,然后最终执行SQL:

declare @sql nvarchar(max) = N'';

if <some condition>
 @sql += N'SELECT ...'

if <some other condition>
 @sql += N'INSERT ...'

-- finally, execute the SQL:
exec sp_executesql @sql;

毋庸置疑,这远不如LINQ那么强大。像添加可选的WHERE子句那样做任何花哨的事情都需要复杂的逻辑来构建@sql脚本。它还会导致代码容易出错且极难调试。请阅读The Curse and Blessings of Dynamic SQL以进行更全面的讨论。

SQLCLR是另一种选择,如果你愿意走那条路。