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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exec statement to and assign it to a variable

Status
Not open for further replies.

FALCONSEYE

Programmer
Joined
Jul 30, 2004
Messages
1,158
Location
US
i have the following

declare @myResult int

set @myResult =
exec ('Select Sum(numShipped) as numshipped from ' + @txtTableName

+ ' Where Shipdate = (Select max(shipdate) from myTable)')

i also tried

exec ('Select Sum(numShipped) as numshipped from ' + @txtTableName

+ ' Where Shipdate = (Select max(shipdate) from myTable)') as @myResult

for some reason, the exec statement runs fine by itself. when i try to assign the result of the exec statement to a variable sql throws an error. does anyone know how to set a variable from an exec statement?
thanks for the help in advance..

 
does this work?

Code:
DECLARE @txtTableName VARCHAR(100),
@myResult INT,
@chvSQL NVARCHAR(100)

SELECT @txtTableName = 'SomeTableName'
SELECT @chvSQL = N'SELECT @intTableCount = Sum(numShipped) as numshipped from ' + @txtTableName + ' Where Shipdate = (Select max(shipdate) from myTable)' 

EXEC sp_executesql @chvSQL, N'@myResult INT OUTPUT', @myResult OUTPUT

SELECT @myResult
GO

2 ways
1 with exec and temp table
2 with sp_executesql

are explained here


Denis The SQL Menace
SQL blog:
Personal Blog:
 
thanks denis but i am getting
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'as'.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'm'.

(1 row(s) affected)

 
this runs in pubs
Code:
DECLARE @txtTableName VARCHAR(100),
@myResult INT,
@chvSQL NVARCHAR(100)

SELECT @txtTableName = 'authors'
SELECT @chvSQL = N'SELECT @myResult = count(contract)  from ' + @txtTableName + ' Where state = (Select min(state) from publishers)' 

EXEC sp_executesql @chvSQL, N'@myResult INT OUTPUT', @myResult OUTPUT

SELECT @myResult
GO

in your code we need to make 2 changes
try this
Code:
DECLARE @txtTableName VARCHAR(100),
@myResult INT,
@chvSQL NVARCHAR(100)

SELECT @txtTableName = 'SomeTableName'
SELECT @chvSQL = N'SELECT @myResult= Sum(numShipped) from ' + @txtTableName + ' Where Shipdate = (Select max(shipdate) from myTable)' 

EXEC sp_executesql @chvSQL, N'@myResult INT OUTPUT', @myResult OUTPUT

SELECT @myResult
GO

Denis The SQL Menace
SQL blog:
Personal Blog:
 
thanks denis, but still no luck. i keep getting

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'from'.

(1 row(s) affected)

 
thats strange, it worked for me too. can we have your implementation code???

Known is handfull, Unknown is worldfull
 
I think that @chvSQL isn't big enough. Try...

@chvSQL NVARCHAR(100[!]0[/!])



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top