saving results of exec(@sql) to a variable

  • 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?

  • 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


  • I get the following error

    EXECUTE cannot be used as a source when inserting into a table variable.

  • 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

  • Also read about sp_executesql in SQL Server help file


    Madhivanan

    Failing to plan is Planning to fail

  • 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


  • 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


    Madhivanan

    Failing to plan is Planning to fail

  • 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