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
 General SQL Server Forums
 New to SQL Server Programming
 parameterized query !!

Author  Topic 

ann06
Posting Yak Master

171 Posts

Posted - 2008-11-29 : 03:20:35
i want to do this parameterized query


Declare @strQuery nvarchar(1000)

Select @strQuery = 'Select E.EmpName, TS.Day, TS.HrsPut
from dbo.empdetails E, dbo.EmpTimeSheet TS
where E.EmpID = @emppid and TS.EmpID = @EmpID'
Exec sp_executesql @strQuery, N'@emppid int',N'@EmpID int',@emppid=2,@EmpID=1

it is giving this error
Must declare the variable '@EmpID'.

if i have one parameter its working for 2 its not

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-11-29 : 03:53:11
I don't think that qualifies as a "parametrized query".
Let me ask you one thing; why have you even considered dynamic sql for this query?
SELECT		e.EmpName,
ts.[Day],
ts.HrsPut
FROM dbo.empDetails AS e
INNER JOIN dbo.EmpTimeSheet AS ts ON ts.EmpID = @EmpID
WHERE e.EmpID = @emppid




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

ann06
Posting Yak Master

171 Posts

Posted - 2008-11-29 : 04:25:03
just for information :D but i figured it out it should be in this form

Declare @strQuery nvarchar(1000)

Select @strQuery = 'Select E.EmpName, TS.Day, TS.HrsPut
from dbo.empdetails E, dbo.EmpTimeSheet TS
where E.EmpID = @emppid and TS.EmpID = @EmpID'
Exec sp_executesql @strQuery, N'@emppid int, @EmpID int',@emppid=2,@EmpID=1

N'@emppid int, @EmpID int' ---- not N'@emppid int',N'@EmpID int'

thanks for your time
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-11-29 : 07:46:05
you have completely missed the point. Look at Peso's post.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-29 : 08:16:44
quote:
Originally posted by ann06

just for information :D but i figured it out it should be in this form

Declare @strQuery nvarchar(1000)

Select @strQuery = 'Select E.EmpName, TS.Day, TS.HrsPut
from dbo.empdetails E, dbo.EmpTimeSheet TS
where E.EmpID = @emppid and TS.EmpID = @EmpID'
Exec sp_executesql @strQuery, N'@emppid int, @EmpID int',@emppid=2,@EmpID=1

N'@emppid int, @EmpID int' ---- not N'@emppid int',N'@EmpID int'

thanks for your time


there's no condition which demands use of dynamic sql in above case. Thats exactly what peso suggested. The code given by Peso is what you exactly want.
Go to Top of Page
   

- Advertisement -