SQL语句中的错误

时间:2012-10-04 11:16:06

标签: sql sql-server

with [BlockedQueries] as 
(
   select 
      der.session_id, der.blocking_session_id, der.start_time, 
      der.total_elapsed_time,
      SUBSTRING(text, (statement_start_offset/2)+1,
      ((CASE statement_end_offset
             WHEN -1 THEN DATALENGTH(text)
             ELSE statement_end_offset
         END - statement_start_offset)/2) + 1) AS sqltext
   from 
      sys.dm_exec_requests as der
   cross apply 
      sys.dm_exec_sql_text (der.sql_handle) as dest
   where 
      blocking_session_id <> 0

   union all

   select 
       c.session_id, c.blocking_session_id, c.start_time, c.total_elapsed_time, 
       dest.text 
   from
       [BlockedQueries] p, sys.dm_exec_requests as c
   cross apply 
       sys.dm_exec_sql_text (c.sql_handle) as dest
   where 
       p.[blocking_session_id] = c.[session_id]
)

在SQL Server 2008上运行时,我收到以下错误。

  

Msg 102,Level 15,State 1,Line 31
  ')'附近的语法不正确。

我正在尝试运行in this link 在阻止查询代理下。

任何指针都将受到赞赏。

2 个答案:

答案 0 :(得分:2)

你需要在with子句之后使用某些东西(通常在它之前是;

   ;with blockedqueries as
   (
       ....
   )
   Select * from blockedqueries

答案 1 :(得分:1)

Common Table Expression(CTE)

中选择,将您的查询更改为此查询
with [BlockedQueries] as (

select der.session_id, der.blocking_session_id, der.start_time, der.total_elapsed_time,

SUBSTRING(text, (statement_start_offset/2)+1,

((CASE statement_end_offset

WHEN -1 THEN DATALENGTH(text)

ELSE statement_end_offset

END - statement_start_offset)/2) + 1) AS sqltext

from sys.dm_exec_requests as der

cross apply sys.dm_exec_sql_text (der.sql_handle) as dest

where blocking_session_id <> 0

union all

select c.session_id, c.blocking_session_id, c.start_time, c.total_elapsed_time, dest.text from

[BlockedQueries] p, sys.dm_exec_requests as c

cross apply sys.dm_exec_sql_text (c.sql_handle) as dest

where p.[blocking_session_id] = c.[session_id]

)
SELECT * FROM [BlockedQueries];