select * from sp_who的解決方案
發(fā)布日期:2022-02-02 11:49 | 文章來源:源碼中國
方法一:使用臨時表。
首先創(chuàng)建一個與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';
執(zhí)行上面這個語句,如果提示:SQL Server 阻止了對組件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的訪問,因為此組件已作為此服務(wù)器安全配置的一部分而被關(guān)閉。系統(tǒng)管理員可以通過使用 sp_configure 啟用 'Ad Hoc Distributed Queries'。有關(guān)啟用 'Ad Hoc Distributed Queries' 的詳細(xì)信息。
說明你沒有配置 '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
然后就可以運行上面的代碼了。
使用完成后,如果想關(guān)閉Ad Hoc Distributed Queries,執(zhí)行如下代碼:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
首先創(chuàng)建一個與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';
執(zhí)行上面這個語句,如果提示:SQL Server 阻止了對組件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的訪問,因為此組件已作為此服務(wù)器安全配置的一部分而被關(guān)閉。系統(tǒng)管理員可以通過使用 sp_configure 啟用 'Ad Hoc Distributed Queries'。有關(guān)啟用 'Ad Hoc Distributed Queries' 的詳細(xì)信息。
說明你沒有配置 '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
然后就可以運行上面的代碼了。
使用完成后,如果想關(guān)閉Ad Hoc Distributed Queries,執(zhí)行如下代碼:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章