473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Storing result of EXEC into a variable

Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000

I wish to store the numeric result of a query into a variable, as
described below. This does not work, it fails on the last line with
syntax error. Is there a way to do it?
(The reason I want to do this is to store the count into a table,
therefore eventually do an update table with the variable.)

declare @tmp_select char(500)
declare @mycount int
set @tmp_select = 'select count(*) from dptest'
exec(@tmp_select)
set @mycount=exec(@tmp_select)
Jul 23 '05 #1
6 51825
Try to use:
set @tmp_select = (select count(*) from dptest)

Yury Jhol
Chief Systems Architect
Manager of Database Department
Database Migration Tool
www.ispirer.com
"Dipak Patel" <di*****@hotmail.com> wrote in message
news:30**************************@posting.google.c om...
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000

I wish to store the numeric result of a query into a variable, as
described below. This does not work, it fails on the last line with
syntax error. Is there a way to do it?
(The reason I want to do this is to store the count into a table,
therefore eventually do an update table with the variable.)

declare @tmp_select char(500)
declare @mycount int
set @tmp_select = 'select count(*) from dptest'
exec(@tmp_select)
set @mycount=exec(@tmp_select)

Jul 23 '05 #2
On 10 Jan 2005 04:39:44 -0800, Dipak Patel wrote:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000

I wish to store the numeric result of a query into a variable, as
described below. This does not work, it fails on the last line with
syntax error. Is there a way to do it?
(The reason I want to do this is to store the count into a table,
therefore eventually do an update table with the variable.)

declare @tmp_select char(500)
declare @mycount int
set @tmp_select = 'select count(*) from dptest'
exec(@tmp_select)
set @mycount=exec(@tmp_select)


The resultset from an exec can be put into a table directly:

declare @tmp_select char(500)
set @tmp_select = 'select count(*) from dptest'

CREATE TABLE ResultSet (mycount int)

INSERT INTO ResultSet
EXEC @tmp_select

To put it into a variable from there, you'd have to add another select.

For this specific example, though, it would be far easier to do

declare @tmp_select char(500)
declare @mycount int
select @mycount=count(*) from dptest

But I suspect you knew that.
Jul 23 '05 #3

"Dipak Patel" <di*****@hotmail.com> wrote in message
news:30**************************@posting.google.c om...
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000

I wish to store the numeric result of a query into a variable, as
described below. This does not work, it fails on the last line with
syntax error. Is there a way to do it?
(The reason I want to do this is to store the count into a table,
therefore eventually do an update table with the variable.)

declare @tmp_select char(500)
declare @mycount int
set @tmp_select = 'select count(*) from dptest'
exec(@tmp_select)
set @mycount=exec(@tmp_select)


Is there some reason you can't do this?

select @tmp_select = count(*)
from dptest

If you really need dynamic SQL (and normally you should avoid it), then this
is one way:

create table #t (num int)
exec('insert into #t select count(*) from dbo.sysobjects')
select num from #t

But before using this solution, see here for all the reasons why dynamic SQL
is a bad idea:

http://www.sommarskog.se/dynamic_sql.html

Simon
Jul 23 '05 #4
Dipak Patel (di*****@hotmail.com) writes:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000

I wish to store the numeric result of a query into a variable, as
described below. This does not work, it fails on the last line with
syntax error. Is there a way to do it?
(The reason I want to do this is to store the count into a table,
therefore eventually do an update table with the variable.)

declare @tmp_select char(500)
declare @mycount int
set @tmp_select = 'select count(*) from dptest'
exec(@tmp_select)
set @mycount=exec(@tmp_select)


Use sp_executesql. For details
http://www.sommarskog.se/dynamic_sql.html#sp_executesql

Don't use the method with a temp table suggested by others. That's
an extreme kludge.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5

Erland Sommarskog wrote:
Dipak Patel (di*****@hotmail.com) writes:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000

I wish to store the numeric result of a query into a variable, as
described below. This does not work, it fails on the last line with
syntax error. Is there a way to do it?
(The reason I want to do this is to store the count into a table,
therefore eventually do an update table with the variable.)

declare @tmp_select char(500)
declare @mycount int
set @tmp_select = 'select count(*) from dptest'
exec(@tmp_select)
set @mycount=exec(@tmp_select)


Use sp_executesql. For details
http://www.sommarskog.se/dynamic_sql.html#sp_executesql

Don't use the method with a temp table suggested by others. That's
an extreme kludge.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Thanks for the help guys, i got it to work with sp_executesql.

Jul 23 '05 #6
[quote=Yury Jhol]Try to use:
set @tmp_select = (select count(*) from dptest)
================

That worked for me, thanks Yury!
Apr 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
8
by: Mario Pranjic | last post by:
Hi! Can anybody give me a hint how to put sa resut from EXEC into a variable. EXEC is called: EXEC(@TmpQuery) and it returns a single int value (SELECT COUNT(*) ....) Thanks!
4
by: Greg | last post by:
I need to send the result of a procedure to an update statement. Basically updating the column of one table with the result of a query in a stored procedure. It only returns one value, if it didnt...
1
by: Karl.Auer | last post by:
Hello group! i have a table "group_code" wich relates the names of nt-(domain)-groups to codes. now i want use the stored procedure xp_logininfo (asking for the group-membership of the current...
0
by: Dipak Patel | last post by:
Thanks for everyone's help. I used sp_executesql and that worked a treat. I used the following code inside a cursor to go round all the user tables and to store the counts in a separate table. ...
3
by: Mark Miller | last post by:
I have an sp that outputs multiple xml fragments w/ no root. The sp calls individual sp's to output the correct set of data and each "type" has different fields. ex.: <LEADERBOARD...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
9
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.