Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to put the result of a select in a variable

Author  Topic 

pharvey
Starting Member

20 Posts

Posted - 2006-02-09 : 08:33:29
Hi everybody!

I would like to know how to put the result of a querry in a variable. By example i have this variable:
DECLARE @NbUser int

I would like to put the result of this querry on this variable:
SELECT COUNT(UserID) FROM tblUser

Suppose the querry return 23, i want to put this 23 on the variable.

I need it to do some other operation after. I'm using stored procedure.

Anyone can help?

Thank you!

Pascal

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-09 : 08:34:50
[code]SELECT @NbUser = COUNT(UserID) FROM tblUser[/code]

----------------------------------
'KH'

everything that has a beginning has an end
Go to Top of Page

pharvey
Starting Member

20 Posts

Posted - 2006-02-09 : 08:40:16
Thank you khtan!

Pascal
Go to Top of Page

pharvey
Starting Member

20 Posts

Posted - 2006-02-09 : 08:52:23
A little bit more complex now.

I want to retrieve it from a querry like that:

select @vSQL = 'SELECT COUNT(tp.[ID]) FROM tblPhoto tp '
select @vSQL = @vSQL + 'LEFT JOIN tblPhotoImages AS tpi ON tpi.PhotoID = tp.[ID] '
select @vSQL = @vSQL + 'LEFT JOIN tblImages AS ti ON ti.[ID] = tpi.ImageID '
IF @CategoryID > 0 BEGIN
select @vSQL = @vSQL + ' WHERE tp.CategoryID = ' + convert(varchar, @CategoryID)
END
Execute(@vSQL)

How can i put it on the variable with an Execute behind it.

Pascal
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-09 : 08:59:03
use sp_executesql. It allow passing of parameter

----------------------------------
'KH'

everything that has a beginning has an end
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-09 : 09:03:44
Also refer this
http://www.nigelrivett.net/SQLTsql/sp_executeSQL.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -