Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

table variable within sp_msforeachdb

Posted on 2010-09-07
3
Medium Priority
?
823 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 27

Accepted Solution

by:
Shaun Kline earned 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

721 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