Avatar of Anthony Cardullo
Anthony CardulloFlag for United States of America

asked on 

HOw to add table name to insert result wiht sp_foreachtable

I am using the sp_Foreachtable method to grab records from a group of tables and insert them into an existing table in same db. What i would like to do is add the table name of the record in which it was found.  i can add the tablename field to the exisitn table sprintdb.dbo.EbtSprintLookup to accomodate the added tablename value.

THanks

This is what i have now:

USE SPRINTDB

declare @mycommand varchar(128)
declare @mywhereand varchar(128)
declare @RETURN_VALUE varchar(255)



set @mycommand = 'select * from ? where Field9 = ''9995554323'' and Field1 = ''030'''
set @mywhereand =  'and o.name like ''EbtSprint2%'''

insert INTO sprintdb.dbo.EbtSprintLookup

exec @RETURN_VALUE=sp_MSforeachtable
               @command1 = @mycommand,
              @whereand = @mywhereand
             
             
             select *
             from sprintdb.dbo.EbtSprintLookup
             order by abs(field3)
            truncate table sprintdb.dbo.EbtSprintLookup
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Anthony Cardullo
ASKER CERTIFIED SOLUTION
Avatar of chubby_informer
chubby_informer
Flag of Trinidad and Tobago image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Anthony Cardullo

ASKER

Just pointing to an article. No code example.
Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo