Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing variable outside of exec stmt 1

Status
Not open for further replies.

sagn

Programmer
Jun 7, 2001
166
US
Hi,

How can I access the value of a variable outside of
an exec statement.
I have tried the following

1:
declare @i int
set @i = exec ('select count(*) from table')

of course this does not work and I understand why
but I thought I would try anyway.

2:
exec ('declare @row int
set @row = (select count(*) from table)')

@row is not recognized outside of the exec stmt.

Declaring @row before the exec stmt does not work either

I want to be able to do this since I need to use an exec statement because the table name is actually a variable
(@tablename) and I want to use @row variable
elsewhere...

Thanks

ds


 
Instead of using EXECute to execute the SQL, use sp_executesql. It allows you to use output parameters.

EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@parm=@parmIN,
@parm1OUT=@parmRET1 OUTPUT,@parm2OUT=@parmRET2 OUTPUT

See the following article for more info.

INF: Using Output Parameters with sp_executesql
Terry

"I shall try to correct errors when shown to be errors, and I shall adopt new views so fast as they shall appear to be true views." - Abraham Lincoln
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top