简体   繁体   English

此T-SQL select语句有什么问题?

[英]What's wrong with this T-SQL select statement?

I'm reading an article from this website , but when i run the code that article provided, i get the error: 我正在从该网站上阅读文章,但是当我运行该文章提供的代码时,出现错误:

Msg 102, Level 15, State 1, Line 16
Incorrect syntax near '.'.

I double checked the syntax and can't find the cause of this error. 我仔细检查了语法,找不到导致此错误的原因。 The code is as follows, formatted by SQL Prompt. 代码如下,由SQL Prompt格式化。

Anybody can help me with it? 有人可以帮我吗? Great thanks. 万分谢意。

SELECT  es.session_id ,
        es.host_name ,
        es.login_name ,
        er.status ,
        DB_NAME(database_id) AS DatabaseName ,
        SUBSTRING(qt.text, ( er.statement_start_offset / 2 ) + 1, ( ( CASE WHEN er.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
                                                                           ELSE er.statement_end_offset
                                                                      END - er.statement_start_offset ) / 2 ) + 1) AS [Individual Query] ,
        qt.text AS [Parent Query] ,
        es.program_name ,
        er.start_time ,
        qp.query_plan ,
        er.wait_type ,
        er.total_elapsed_time ,
        er.cpu_time ,
        er.logical_reads ,
        er.blocking_session_id ,
        er.open_transaction_count ,
        er.last_wait_type ,
        er.percent_complete
FROM    sys.dm_exec_requests AS er
        INNER JOIN sys.dm_exec_sessions AS es ON es.session_id = er.session_id
        CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt
        CROSS APPLY sys.dm_exec_query_plan(er.plan_handle) qp
WHERE   es.is_user_process = 1
        AND es.session_Id NOT IN ( @@SPID )
ORDER BY es.session_id

Edit: 编辑:

I run the upper query using SSMS 2008 against a SQL Server 2005 database. 我使用SSMS 2008对SQL Server 2005数据库运行上层查询。 When I try to use SSMS 2005 run the same query against a SQL Server 2005 database, everything works fine. 当我尝试使用SSMS 2005对SQL Server 2005数据库运行相同的查询时,一切正常。 As i know, SSMS 2008 can connect to a SQL Server 2005 database and operate on it, right? 据我所知,SSMS 2008可以连接到SQL Server 2005数据库并对其进行操作,对吗? Also I tried another example, listed below. 我也尝试了下面列出的另一个示例。 It's really simple one. 这真的很简单。 Similarly, it execute successfully when using SSMS 2005 against SQL Server 2005. When using SSMS 2008 against SQL Server 2005, it give the same error as i mentioned early. 同样,当对SQL Server 2005使用SSMS 2005时,它可以成功执行。当对SQL Server 2005使用SSMS 2008时,它会给出与我前面提到的相同的错误。

SELECT  stat.sql_handle ,
        sqltext.text SQL
FROM    sys.dm_exec_query_stats stat
        CROSS APPLY sys.dm_exec_sql_text(stat.sql_handle) sqltext

Anybody have some idea on this? 有人对此有想法吗?

Thanks. 谢谢。


Edit 2: 编辑2:

Just tried, using SSMS 2008 against SQL Server 2008 works fine. 刚刚尝试过,对SQL Server 2008使用SSMS 2008可以正常工作。

You say that you're connecting to a "SQL Server 2005 database" a couple of times. 您说要连接到“ SQL Server 2005数据库”几次。 Does that mean a SQL Server 2005 server? 这是否意味着SQL Server 2005服务器?

I am able to reproduce your problem when I use SQL Server 2008 SSMS on SQL Server 2008 server. 当我在SQL Server 2008服务器上使用SQL Server 2008 SSMS时,我能够重现您的问题。 This occurs when my database compatibility level for the current database is SQL Server 2000, which doesn't have the features you're trying. 当我对当前数据库的数据库兼容性级别是SQL Server 2000时,就会发生这种情况,而SQL Server 2000没有您要使用的功能。 When I switch the database the command works. 当我切换数据库时,该命令有效。

Check the compatibility level for your database. 检查数据库的兼容性级别。 The command will work if SQL Server 2005 or later. 如果SQL Server 2005或更高版本,该命令将起作用。

nothing wrong with the SQL per se ["works on my machine"(tm)] so it is probably a data error. SQL本身没有什么问题[“在我的机器上工作”(tm)],所以它可能是数据错误。 The substring function is the most likely suspect. 子字符串函数最有可能被怀疑。 Try running it with TOP 1 and/or in reverse order. 尝试以TOP 1和/或相反的顺序运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM