select * from sp_who的解决方案
16lz
2022-01-18
方法一:使用临时表。
首先创建一个与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
首先创建一个与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
更多相关文章
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- [RK3399][Android7.1.1] WifiAp:开机默认打开wifi热点
- Android(安卓)Studio bug - attribute 'android:versionCode' no
- Selector、shape详解(一)
- Android获取设备唯一标识完美解决方案
- android背景选择器selector用法汇总
- Android中onContextItemSelected不响应
- Android(安卓)中数据库查询方法 query() 中的 select
- android中SqLite query中用selectionArgs处理字符传值