[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-03-27
6
Medium Priority
?
359 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:pauldes
6 Comments
 
LVL 7

Expert Comment

by:fefo_33065
ID: 16305434
Remove the Go
0
 
LVL 15

Accepted Solution

by:
DonKronos earned 2000 total points
ID: 16305565
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
 
LVL 10

Expert Comment

by:StephenCairns
ID: 16305653
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:pauldes
ID: 16305691
THANKS!!
0
 

Author Comment

by:pauldes
ID: 16305708
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
 
LVL 10

Expert Comment

by:StephenCairns
ID: 16307108
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

872 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