May 8, 2009 at 6:32 am
Hi,
How you do the following? what would be the correct syntax for the last line will not work
declare @DateTime datetime
declare @tablename varchar(100)
declare @sql varchar(8000)
set @tablename = 'perf270409'
set @sql = 'select min(starttime) from ' + @tablename
set @DateTime =exec(@sql) --HOW CAN I DO THIS?
May 8, 2009 at 7:07 am
Something like this should work:
declare @DateTime datetime
declare @tablename varchar(100)
declare @sql varchar(8000)
declare @temp table(starttime datetime)
set @tablename = 'perf270409'
set @sql = 'select min(starttime) from ' + @tablename
insert @temp(starttime)
exec(@sql)
select @DateTime = starttime from @temp
select @DateTime
--Ramesh
May 8, 2009 at 7:46 am
I get the following error
EXECUTE cannot be used as a source when inserting into a table variable.
May 8, 2009 at 7:50 am
hi Its okay, i have used a temp table instead. Thanks for the help you got me going on the right track.
declare @DateTime datetime
declare @tablename varchar(100)
declare @sql varchar(8000)
create table #temp (starttime datetime)
set @tablename = 'perf270409'
set @sql = 'select min(starttime) from ' + @tablename
insert #temp(starttime)
exec(@sql)
select @DateTime = starttime from #temp
select @DateTime
May 8, 2009 at 7:52 am
Also read about sp_executesql in SQL Server help file
Failing to plan is Planning to fail
May 8, 2009 at 8:15 am
It can also be done using sp_executesql system procedure
declare @DateTime DATETIME
declare @tablename varchar(100)
declare @sql nvarchar(4000)
create table #temp (starttime datetime)
-- Using sp_executesql
set @sql = 'select @DateTime = min(starttime) from ' + @tablename
execute sp_executesql @sql, N'@DateTime datetime output', @DateTime = @DateTime output
select @DateTime
--Ramesh
May 8, 2009 at 8:23 am
Ramesh (5/8/2009)
It can also be done using sp_executesql system procedure
declare @DateTime DATETIME
declare @tablename varchar(100)
declare @sql nvarchar(4000)
create table #temp (starttime datetime)
-- Using sp_executesql
set @sql = 'select @DateTime = min(starttime) from ' + @tablename
execute sp_executesql @sql, N'@DateTime datetime output', @DateTime = @DateTime output
select @DateTime
You dont need to create temp table
Failing to plan is Planning to fail
May 8, 2009 at 8:45 am
Madhivanan (5/8/2009)
Ramesh (5/8/2009)
It can also be done using sp_executesql system procedure
declare @DateTime DATETIME
declare @tablename varchar(100)
declare @sql nvarchar(4000)
create table #temp (starttime datetime)
-- Using sp_executesql
set @sql = 'select @DateTime = min(starttime) from ' + @tablename
execute sp_executesql @sql, N'@DateTime datetime output', @DateTime = @DateTime output
select @DateTime
You dont need to create temp table
It was just that "copy and paste" is responsible for it..:-D
--Ramesh
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply