select * from sp_who的解决方案

方法一:使用临时表。
首先创建一个与sp_who相同字段的临时,然后用insert into 方法赋值,这样就可以select这个临时表了。具体代码如下:

create table #temptable(spid int,ecid int,status varchar(32),loginname varchar(32),hostname varchar(32),blk int,dbname varchar(32),cmd varchar(32),request_id int);

insert into #temptable

exec sp_who;

select * from #temptable where [dbname] = ‘master’;

drop table #temptable


方法二:使用openrowset

代码如下:

select * from openrowset(‘sqloledb’,’servername’;’username’;’password’,’sp_who’) where [dbname] = ‘master’;

执行上面这个语句,如果提示:sql server 阻止了对组件 ‘ad hoc distributed queries’ 的 statement’openrowset/opendatasource’ 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 ‘ad hoc distributed queries’。有关启用 ‘ad hoc distributed queries’ 的详细信息。

说明你没有配置 ‘ad hoc distributed queries’ ,按如下方法配置

启用ad hoc distributed queries:

exec sp_configure ‘show advanced options’,1

reconfigure

exec sp_configure ‘ad hoc distributed queries’,1

reconfigure

然后就可以运行上面的代码了。

使用完成后,如果想关闭ad hoc distributed queries,执行如下代码:

exec sp_configure ‘ad hoc distributed queries’,0

reconfigure

exec sp_configure ‘show advanced options’,0

reconfigure

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐