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)
 dynamic sql

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-09-20 : 07:13:21
Mr.Tran writes "DECLARE @sT nvarchar(255)

SELECT @sT = ('SELECT SUM(sotien) AS tongtien FROM ' + @vw + ' WHERE idnhanvien=' + @fMaNV + ' AND thangnhan=' + @i)
SET @vThang = EXEC(@sT)

why it don't working ??? Please help me !"

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-20 : 07:43:11
quote:
SET @vThang = EXEC(@sT)


You can't do it that way to get Sum(sotien) assigned to local variable. You will have to use sp_executesql for this:

Declare @sql nvarchar

Set @sql = N'SELECT @vThang = SUM(sotien) AS tongtien FROM ' + @vw + ' WHERE idnhanvien=' + @fMaNV + ' AND thangnhan=' + @i

exec sp_executesql @sql, N'@vThang int output', @vThang Output


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-20 : 07:48:27
Make use of sp_excutesql

DECLARE @sT nvarchar(255),
@Sum float

Select @sT 'SELECT @Sum = SUM(sotien) AS tongtien FROM ' + @vw + ' WHERE idnhanvien=' + @fMaNV + ' AND thangnhan=' + @i

sp_executesql
N @sT
N'@Sum float output',
N' @Sum = @Sum output

Select @Sum



Chirag
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-20 : 07:49:33


Chirag
Go to Top of Page
   

- Advertisement -