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 2005 Forums
 Transact-SQL (2005)
 Problem in Exec(dynamic query)

Author  Topic 

Thiyagu_04
Starting Member

37 Posts

Posted - 2008-07-22 : 08:17:46
declare @ID int
exec('SELECT * from tablen where id='+cast(@id as varchar(100)))

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-22 : 08:19:53
declare @ID int
declare @sql varchar(1000)
set @sql = 'SELECT * from tablen where id = ' + cast(@id as varchar(100))

exec(@sql)


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-22 : 08:20:25
[code]
declare @ID int,
@sql nvarchar(4000)
select @sql = 'SELECT * from tablen where id='+cast(@id as varchar(100))
exec(@sql)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-22 : 08:21:27



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Thiyagu_04
Starting Member

37 Posts

Posted - 2008-07-22 : 08:22:30
Thanks for the help

May i know why cast giving problem inside Exec()
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-22 : 08:26:11
Books Online says it has to be a command string
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/bc806b71-cc55-470a-913e-c5f761d5c4b7.htm



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -