Solved

table variable within sp_msforeachdb

Posted on 2010-09-07
3
772 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

17 Experts available now in Live!

Get 1:1 Help Now