Solved

table variable within sp_msforeachdb

Posted on 2010-09-07
3
777 Views
Last Modified: 2012-05-10
the second line loses sight of the declaration in the first line. what can be done to make sure it recognizes it.

thanks
--------------------
declare @DBO table (GName varchar(80),GID int,User_in_DB varchar(80),uid int)
exec sp_msforeachdb 'use [?] insert into @DBOwners exec sp_helpuser db_backupoperator'  
0
Comment
Question by:anushahanna
3 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 250 total points
ID: 33620129
Try using a temporary table instead of a table variable.
From what I recall, table variables can only be used in the current context, while the EXEC command runs in a separate session than the current.

Also, the table you have in your SQL should match your temp table's name.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 33620143
you cannot do that.because the dynamic sql in the msforeachdb is in a separate scope, it cannot "see" the declaration in that "outer" scope.you have to work with either a global temp table (##temptable) or a fixed regular table ...
0
 
LVL 6

Author Comment

by:anushahanna
ID: 33620495
Thanks for the clarification.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now