Link to home
Start Free TrialLog in
Avatar of wppiexperts
wppiexpertsFlag for United States of America

asked on

Return a single value to a variable

I'm trying to get a single value (row count) and write it to a text file.

I'm trying to get the row count value into the variable @spcount and then output it using this code:
set @cmd = 'master..xp_cmdshell "echo Record Count:' + @spcount + '>> d:\file.txt"'
exec (@cmd)

Unfortunately, I can't seem to pass the row count value via the @spcount variable to the file, I always get a value of 0

the code I'm trying to use to assign the value is:
exec @spcount=xdb..sp_executesql N'select count(*) from xtable'

another variation of that line:
exec xdb..sp_executesql N'select count(*) from xtable'

run by themselves, I get the correct output, but when trying to embed the results into the code above, again I get 0

any suggestions?



Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this should work better:
select @spcount=count(*) from xtable
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial