views:

44

answers:

1

I added my local server pblack as linked server in SQL Server 2008 R2

---1)
EXEC master.dbo.sp_addlinkedserver @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 size

Update2:

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.

+1  A: 

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 ''''?'''' '' ') 
gbn