How to store query result into new table

  • Dear Members,

    [font="Courier New"]SELECT a.name, b.type, MAX(b.backup_finish_date) LastSuccessfulBackup,

    CAST((GETDATE() - MAX(b.backup_finish_date)) AS NUMERIC(5, 2)) IntervalInDays

    FROM master..sysdatabases a

    LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name

    GROUP BY a.name, b.type

    ORDER BY a.name, b.type[/font]

    i need to store result of above query into new table (which is not already created)?

  • SELECT a.name, b.type, MAX(b.backup_finish_date) LastSuccessfulBackup,

    CAST((GETDATE() - MAX(b.backup_finish_date)) AS NUMERIC(5, 2)) IntervalInDays

    INTO NewTable

    FROM master..sysdatabases a

    LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name

    GROUP BY a.name, b.type

    ORDER BY a.name, b.type

  • Hi,

    Use the INTO statement just before the FROM.

    SELECT a.name, b.type, MAX(b.backup_finish_date) LastSuccessfulBackup,

    CAST((GETDATE() - MAX(b.backup_finish_date)) AS NUMERIC(5, 2)) IntervalInDays INTO TableName

    FROM master..sysdatabases a

    LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name

    GROUP BY a.name, b.type

    ORDER BY a.name, b.type

    The query output will be sent into the table



    If you need to work better, try working less...

  • can we write this into sub query?

    what i mean is

    select * into <new table> from ( <sub query>)

  • konuridinesh (9/27/2012)


    can we write this into sub query?

    what i mean is

    select * into <new table> from ( <sub query>)

    yes,

    SELECT * INTO TableName FROM (SELECT a.name, b.type, MAX(b.backup_finish_date) LastSuccessfulBackup,

    CAST((GETDATE() - MAX(b.backup_finish_date)) AS NUMERIC(5, 2)) IntervalInDays

    FROM master..sysdatabases a

    LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name

    GROUP BY a.name, b.type) t

    Have to remove ORDER BY since it's not supported on sub queries...



    If you need to work better, try working less...

  • declare one temp table and then insert the data which u r selecting

    insert into @temp

    (

    name,

    LastSuccessfulBackup,

    IntervalInDays

    )

    SELECT a.name, b.type, MAX(b.backup_finish_date) LastSuccessfulBackup,

    CAST((GETDATE() - MAX(b.backup_finish_date)) AS NUMERIC(5, 2)) IntervalInDays

    FROM master..sysdatabases a

    LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name

    GROUP BY a.name, b.type

    ORDER BY a.name, b.type

  • konuridinesh (9/27/2012)


    can we write this into sub query?

    what i mean is

    select * into <new table> from ( <sub query>)

    Why do you want to do that?

  • what my exact requirement is

    i need record backup details for each database on instance into table

  • thanks pimane

  • Have a look at sp_MSForEachDB.

    It's an undocumented sp which runs code against each db. There are plenty of examples if you search this site.

  • thanx laurie

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply