Solved

EXECUTE cannot be used as a source when inserting into a table variable.

Posted on 2010-11-16
11
551 Views
Last Modified: 2012-05-10
the following format works in 2005, not in 2000...

what would be the equivalent of the same in 2000?
insert @test
EXEC ('EXEC ' + @database_name + '.dbo.sp_helprotect')

*@test is a table variable
*@database_name is a temp variable
0
Comment
Question by:anushahanna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 34147907
Yep, you can't do that in 2005.  2008 you can I believe, but not 2005.  Use temp tables instead.
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34147972
no, i meant to say
insert @test
EXEC ('EXEC ' + @database_name + '.dbo.sp_helprotect')

works in 2005, not in 2000.

how would you do it in 2000?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34147984
Just a sql server 2000 limitation.
#Temp works, so a 2-step process will work

if object_id('tempdb..#test') is not null drop table #test;
declare @database_name varchaR(100) set @database_name = 'tempdb'
declare @test table (owner sysname, object sysname, grantee sysname, grantor sysname, protecttype sysname, action sysname, [column] varchar(1000))
create table #test (owner sysname, object sysname, grantee sysname, grantor sysname, protecttype sysname, action sysname, [column] varchar(1000))
insert #test
EXEC master.dbo.sp_helprotect
insert @test select * from #test
select * from @test
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 6

Author Comment

by:anushahanna
ID: 34147989
in other words,
EXEC ('EXEC ' + @database_name + '.dbo.sp_helprotect') would work wtih regardless of temp table or table variable.. in 2005..

is there a way to do it in one sentence like that in 2000...
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 34147991
I'd use temp tables.  :)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34147997
Sorry, was testing, this is the complete one including your @database_name variable

if object_id('tempdb..#test') is not null drop table #test;
declare @database_name varchaR(100) set @database_name = 'master'
declare @test table (owner sysname, object sysname, grantee sysname, grantor sysname, protecttype sysname, action sysname, [column] varchar(1000))
create table #test (owner sysname, object sysname, grantee sysname, grantor sysname, protecttype sysname, action sysname, [column] varchar(1000))
declare @sql varchar(1000)
set @sql = 'insert #test EXEC [' + @database_name +'].dbo.sp_helprotect'
exec(@sql)
insert @test select * from #test
select * from @test
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34148007
> I'd use temp tables.  :)

Yes, so would I.. haha
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34148035
Oh-- perfect..

so there are 2 limitations, actually?

the exec within the exec part...
and
table variable, too, right?
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 380 total points
ID: 34148066
There is only one - the table variable
I was iterating through options, and removed the double EXEC while doing that
This works as well
if object_id('tempdb..#test') is not null drop table #test;
declare @database_name varchaR(100) set @database_name = 'master'
declare @test table (owner sysname, object sysname, grantee sysname, grantor sysname, protecttype sysname, action sysname, [column] varchar(1000))
create table #test (owner sysname, object sysname, grantee sysname, grantor sysname, protecttype sysname, action sysname, [column] varchar(1000))
declare @sql varchar(1000)
insert #test
EXEC ('EXEC ' + @database_name + '.dbo.sp_helprotect')
--set @sql = 'insert #test EXEC [' + @database_name +'].dbo.sp_helprotect'
--exec(@sql)
insert @test select * from #test
select * from @test

Open in new window

0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 120 total points
ID: 34148076
You can nest execs easily enough:  exec('exec(''exec sp_who2'')')

the problem was the table var in 2000
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34148492
y'all excellent!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question