Solved

table variable within sp_msforeachdb

Posted on 2010-09-07
3
808 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
[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
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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
'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 …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

734 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