help me SELECT-ing from chained stored procedures
I added my local server pblack as linked server in SQL Server 2008 R2
---1)
EXEC master.dbo.sp_addli开发者_JAVA百科nkedserver @server = N'pblack',
--'pblack' is my localhost
@srvproduct=N'SQL Server'
and executed successfully 2) and 3):
--2)
sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
--3)
SELECT * INTO #temp
FROM OPENQUERY(pblack,'exec sp_who')
SELECT * FROM #temp
4)
How can I insert 2) into 3) instead of 'exec sp_who'. I cannot manage to correctly quote expressions...Why cannot I link local or localhost in SQL Server 2008R2?
Update:
The purpose is to further modify SELECT, for example, outputting max(data) - finding the table with maximum sizeUpdate2:
SELECT * INTO #temp
FROM OPENQUERY
(
pblack,
'EXEC sp_MSforeachtable @command1= '' EXEC sp_spaceused ''''?'''' '' '
)
gives error:
Msg 208, Level 16, State 1, Procedure sp_MSforeach_worker, Line 102
Invalid object name '#qtemp'.
I tried concating the strings + using temp varchar variables recieving the same error.
I suspect that the problem is deeper since "select" seems like not not from one rowset but multiple ones?
Well, I can drown in studying the code but I expect to find some (magic) work-around.
Just double up the quotes for each nest level, and always use single quotes too.
Think this is it:
SELECT * INTO #temp
FROM OPENQUERY(
pblack,
'EXEC sp_MSforeachtable @command1= '' EXEC sp_spaceused ''''?'''' '' ')
精彩评论