我正在尝试使用ADO对SQL Server 2005运行(两个)查询:
SELECT
sp.nt_username AS NTUsername,
sp.hostname AS HostName
FROM sys.dm_tran_locks tl
INNER JOIN master.dbo.sysprocesses sp
ON tl.request_session_id = sp.spid
WHERE tl.resource_type = N'APPLICATION'
AND tl.resource_database_id = (
SELECT dbid
FROM master.dbo.sysprocesses
WHERE spid = @@spid)
和SQL Server 2000兼容版本:
SELECT
sp.nt_username AS NTUsername,
sp.hostname AS HostName
FROM master.dbo.syslockinfo sli
INNER JOIN master.dbo.sysprocesses sp
ON sli.req_spid = sp.spid
WHERE rsc_type = 10 --10=Appliction
AND rsc_dbid = (
SELECT dbid
FROM master.dbo.sysprocesses
WHERE spid = @@spid)
查询执行罚款:
IRecordset rs =new Recordset();
rs.CursorLocation = adUseClient; //the default for a Recordset is adUseServer (Connection.Execute's default is adUseClient)
rs.CursorType := adOpenForwardOnly; //the default
rs.Open(szQuery, conn.ConnectionObject,
adOpenForwardOnly, //CursorType
adLockReadOnly, //LockType
adCmdText);
然后 检查 状态BOF
或EOF
:
if (rs.EOF) then
触发异常:
导致ADO失败的查询是什么?
注意:确实 是 匹配的行:
我在连接字符串中使用的provider
是Provider=SQLOLEDB
。
Bonus Chatter:
的sysprocesses
nt_username nchar(128) hostname nchar(128)
并查询两者 - 但一次一个 - 有效,例如:
SELECT
sp.nt_username AS NTUsername
和
SELECT
sp.hostname AS HostName
的工作。查询两者都失败了。
答案 0 :(得分:0)
即使nt_username
和hostname
已经 nchar(128)
,您仍需要手动将其转换为nchar(128)
SELECT
CAST(sp.nt_username AS nchar(128)) AS NTUsername,
CAST(sp.hostname AS nchar(128)) AS HostName
FROM sys.dm_tran_locks tl
INNER JOIN master.dbo.sysprocesses sp
ON tl.request_session_id = sp.spid
WHERE tl.resource_type = N'APPLICATION'
AND tl.resource_database_id = (
SELECT dbid
FROM master.dbo.sysprocesses
WHERE spid = @@spid)
SQL Server 2005和ADO也存在已知错误,并且在查询中使用@@spid
;你做不了。幸运的是,我可以使用DB_ID()
:
SELECT
CAST(sp.nt_username AS nchar(128)) AS NTUsername,
CAST(sp.hostname AS nchar(128)) AS HostName
FROM sys.dm_tran_locks tl
INNER JOIN master.dbo.sysprocesses sp
ON tl.request_session_id = sp.spid
WHERE tl.resource_type = N'APPLICATION'
AND tl.resource_database_id = DB_ID()
八达冰。
SQL Server 2005错误。
它可能仍然存在于SQL Server 2008,SQL Server 2008 R2,SQL Server 2012
中