473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 51830
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
nejem666
1 New Member
[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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.