Can't Create Stored Procedure - error Trying to use sp_MSForEachTable

I'm trying to loop through my db and fill the values in a field called ExternalID that exists in each table using the following:

exec sp_MSForEachTable 'exec fillidentity "?" '

First I have to set up the sproc, fillidentity but I get an error. I am trying to run:

create procedure FillIdentity
 @ident int,
 @tble varchar(50)
as
set @ident = 1
Update @tble
set ExternalID = @ident, @ident = @ident + 1
go

BUT, I keep getting the error when creating the procedure, that says:

Server: Msg 137, Level 15, State 2, Procedure FillIdentity, Line 8
Must declare the variable '@tble'.

What's going wrong? Thanks
pauldesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fefo_33065Commented:
Remove the Go
0
DonKronosCommented:
The go is ok.  You cannot use a dynamic table name the way you are trying.

create procedure FillIdentity
 @ident int,
 @tble varchar(50)
as
declare @sql varchar(1000)
set @ident = 1
select @sql = 'Update ' + @tble + ' set ExternalID = ' + cast(@ident as varchar)
exec(@sql)
set @ident = @ident + 1
go

But I don't think this is what you want???  I think you are trying to increment @ident for each update.  I think this will give you what you want.

create procedure FillIdentity
 @ident int,
 @tble varchar(50)
as
declare @sql varchar(1000)
select @sql = 'Update ' + @tble + ' set ExternalID = (select max(ExternalID) from ' + @tble + ')'
exec(@sql)
go
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StephenCairnsCommented:
just curious here, do all your tables have the ExternalID field in them?
if not then you are going to get errors when you run this proc
you can filter the sp_MSForEachTable proc to only give you tables that contain the field
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pauldesAuthor Commented:
THANKS!!
0
pauldesAuthor Commented:
Yes, they all did have that field, but I would like to know how to do what you're suggesting Stephen. I'll open a new question to give you the points if you want.

Lemme know,

Thanks
0
StephenCairnsCommented:
I've not got sql infront of me so I cant confirm this.
also your version may make a difference
but from what I can see
exec @RETURN_VALUE=sp_MSforeachtable @command1, @replacechar, @command2,
  @command3, @whereand, @precommand, @postcommand

is the full syntax

so
exec sp_MSforeachtable
 @command1 = ''exec fillidentity "?" ',
@whereand =' and name in (
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = N"ExternalID")'

should limit what i returns

let me know how it goes
cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.