web-dev-qa-db-ja.com

クエリから一時テーブルに値を挿入しようとしたときに問題が発生しました

以下で私を助けてください。

SQLクエリを使用して一時テーブルに値を挿入しようとしています。しかし、私は以下のエラーに直面しています。

間違いを理解させてください。

私が直面しているエラー:

Tempdbのオブジェクトを参照しているデータベース名 'tempdb'は無視されました。
データベース名 'tempdb'は無視され、tempdbのオブジェクトを参照しています。

Tempdbのオブジェクトを参照しているデータベース名 'tempdb'は無視されました。

メッセージ156、レベル15、状態1、行17キーワード 'use'の近くの不正な構文。

メッセージ3701、レベル11、状態5、行91テーブルが存在しないか、権限がないため、テーブル '#BackupStatus'を削除できません。

これまでに試したクエリ:

IF OBJECT_ID('tempdb..#BackupStatus') IS NOT NULL 
DROP Table tempdb..#BackupStatus

create table tempdb..#BackupStatus(

Server_name VARCHAR(50),
Full_Backup_Status_Weekly VARCHAR(50),
Diff_Backup_Status_Daily VARCHAR(50),
Transaction_log_backup_Hourly VARCHAR(50))

--select * from #BackupStatus

insert into tempdb..#BackupStatus
(Server_name , Full_Backup_Status_Weekly,Diff_Backup_Status_Daily,Transaction_log_backup_Hourly)
--select Server_name , Full_Backup_Status_Weekly,Diff_Backup_Status_Daily,Transaction_log_backup_Hourly from
values
use msdb
go

WITH [MostRecentBackupStatus_CTE]
AS
(
    SELECT  bsfull.[server_name] ,
            bsfull.[database_name] ,
            bsfull.[backup_finish_date] AS [last_full_backup] ,
            bsdiff.[backup_finish_date] AS [last_diff_backup] ,
            bstlog.[backup_finish_date] AS [last_tran_backup] ,
            DATEDIFF(dd, bsfull.[backup_finish_date], CURRENT_TIMESTAMP) AS [days_since_full_backup] ,
            DATEDIFF(dd, bsdiff.[backup_finish_date], CURRENT_TIMESTAMP) AS [days_since_diff_backup] ,
            DATEDIFF(hh, bstlog.[backup_finish_date], CURRENT_TIMESTAMP) AS [hours_since_tranlog_backup] 

    FROM    [msdb]..[backupset] AS bsfull
            LEFT JOIN [msdb]..[backupset] AS bstlog ON bstlog.[database_name] = bsfull.[database_name]
                                                       AND bstlog.[server_name] = bsfull.[server_name]
                                                       AND bstlog.[type] = 'L'
                                                       AND bstlog.[backup_finish_date] = ( (SELECT  MAX([backup_finish_date])
                                                                                            FROM    [msdb]..[backupset] b2
                                                                                            WHERE   b2.[database_name] = bsfull.[database_name]
                                                                                                    AND b2.[server_name] = bsfull.[server_name]
                                                                                                    AND b2.[type] = 'L') )
            LEFT JOIN [msdb]..[backupset] AS bsdiff ON bsdiff.[database_name] = bsfull.[database_name]
                                                       AND bsdiff.[server_name] = bsfull.[server_name]
                                                       AND bsdiff.[type] = 'I'
                                                       AND bsdiff.[backup_finish_date] = ( (SELECT  MAX([backup_finish_date])
                                                                                            FROM    [msdb]..[backupset] b2
                                                                                            WHERE   b2.[database_name] = bsfull.[database_name]
                                                                                                    AND b2.[server_name] = bsfull.[server_name]
                                                                                                    AND b2.[type] = N'I') )
    WHERE   bsfull.[type] = N'D'
            AND bsfull.[backup_finish_date] = ( (SELECT MAX([backup_finish_date])
                                                 FROM   [msdb]..[backupset] b2
                                                 WHERE  b2.[database_name] = bsfull.[database_name]
                                                        AND b2.[server_name] = bsfull.[server_name]
                                                        AND b2.[type] = N'D') )
            AND EXISTS ( SELECT [name]
                         FROM   [master].[sys].[databases]
                         WHERE  [name] = bsfull.[database_name] )
            AND bsfull.[database_name] NOT IN  (N'tempdb' , N'Master' ,N'Model' , N'MSDB')
)
SELECT  c.[server_name]  ,

       -- c.[database_name] ,
       -- d.[recovery_model_desc] ,
        --c.[last_full_backup] ,
        --c.[last_diff_backup] ,
        --c.[last_tran_backup] ,
        CASE 
            WHEN c.[days_since_full_backup] <= 7 THEN 'Success'
            WHEN c.[days_since_full_backup] >= 7 THEN 'Failed'
        ELSE NULL END AS Full_Backup_Status_Weekly,
            CASE 
            WHEN c.[days_since_diff_backup] <= 1 THEN 'Success'
            WHEN c.[days_since_diff_backup] >= 1 THEN 'Failed'
        ELSE NULL END AS Diff_Backup_Status_Daily,
        CASE 
            WHEN c.[hours_since_tranlog_backup] <= 1 THEN 'Success'
            WHEN c.[hours_since_tranlog_backup] >= 1 THEN 'Failed'
        ELSE NULL END AS Transaction_log_backup_Hourly


       --c.[days_since_full_backup] ,
       -- c.[days_since_diff_backup] ,
       -- c.[hours_since_tranlog_backup] 
        --c.[full_backup_location] ,
        --c.[diff_backup_location] ,
        --c.[tlog_backup_location]
FROM    [MostRecentBackupStatus_CTE] c
        INNER JOIN [master].[sys].[databases] d ON c.[database_name] = d.[name];


        DROP Table tempdb..#BackupStatus
2
DBOn

Tempdbのオブジェクトを参照しているデータベース名 'tempdb'は無視されました。

これはエラーではなく、単なる情報メッセージです。あなたのコードが原因ですinsert into tempdb..#BackupStatus

insert into #BackupStatustempdbを指定せずに)#はすでにtempdbを指しています。

エラー

キーワード「use」付近の構文が正しくありません

によって引き起こされます

insert into tempdb..#BackupStatus
(Server_name , Full_Backup_Status_Weekly,Diff_Backup_Status_Daily,Transaction_log_backup_Hourly)
--select Server_name , Full_Backup_Status_Weekly,Diff_Backup_Status_Daily,Transaction_log_backup_Hourly from
values
use msdb
go

valuesinsert句に値が指定されていませんが、代わりにuseがあります。

値を指定するだけです。

[〜#〜]更新[〜#〜]

私はあなたのコードをレビューし、valuesを挿入したくないがselect from msdbの結果を挿入したい CTEを使用しているので、コードを書き直したので、このような:

use msdb
go


IF OBJECT_ID('tempdb..#BackupStatus') IS NOT NULL 
DROP Table #BackupStatus

create table #BackupStatus(

Server_name VARCHAR(50),
Full_Backup_Status_Weekly VARCHAR(50),
Diff_Backup_Status_Daily VARCHAR(50),
Transaction_log_backup_Hourly VARCHAR(50));

--select * from #BackupStatus

WITH [MostRecentBackupStatus_CTE]
AS
(
    SELECT  bsfull.[server_name] ,
            bsfull.[database_name] ,
            bsfull.[backup_finish_date] AS [last_full_backup] ,
            bsdiff.[backup_finish_date] AS [last_diff_backup] ,
            bstlog.[backup_finish_date] AS [last_tran_backup] ,
            DATEDIFF(dd, bsfull.[backup_finish_date], CURRENT_TIMESTAMP) AS [days_since_full_backup] ,
            DATEDIFF(dd, bsdiff.[backup_finish_date], CURRENT_TIMESTAMP) AS [days_since_diff_backup] ,
            DATEDIFF(hh, bstlog.[backup_finish_date], CURRENT_TIMESTAMP) AS [hours_since_tranlog_backup] 

    FROM    [msdb]..[backupset] AS bsfull
            LEFT JOIN [msdb]..[backupset] AS bstlog ON bstlog.[database_name] = bsfull.[database_name]
                                                       AND bstlog.[server_name] = bsfull.[server_name]
                                                       AND bstlog.[type] = 'L'
                                                       AND bstlog.[backup_finish_date] = ( (SELECT  MAX([backup_finish_date])
                                                                                            FROM    [msdb]..[backupset] b2
                                                                                            WHERE   b2.[database_name] = bsfull.[database_name]
                                                                                                    AND b2.[server_name] = bsfull.[server_name]
                                                                                                    AND b2.[type] = 'L') )
            LEFT JOIN [msdb]..[backupset] AS bsdiff ON bsdiff.[database_name] = bsfull.[database_name]
                                                       AND bsdiff.[server_name] = bsfull.[server_name]
                                                       AND bsdiff.[type] = 'I'
                                                       AND bsdiff.[backup_finish_date] = ( (SELECT  MAX([backup_finish_date])
                                                                                            FROM    [msdb]..[backupset] b2
                                                                                            WHERE   b2.[database_name] = bsfull.[database_name]
                                                                                                    AND b2.[server_name] = bsfull.[server_name]
                                                                                                    AND b2.[type] = N'I') )
    WHERE   bsfull.[type] = N'D'
            AND bsfull.[backup_finish_date] = ( (SELECT MAX([backup_finish_date])
                                                 FROM   [msdb]..[backupset] b2
                                                 WHERE  b2.[database_name] = bsfull.[database_name]
                                                        AND b2.[server_name] = bsfull.[server_name]
                                                        AND b2.[type] = N'D') )
            AND EXISTS ( SELECT [name]
                         FROM   [master].[sys].[databases]
                         WHERE  [name] = bsfull.[database_name] )
            AND bsfull.[database_name] NOT IN  (N'tempdb' , N'Master' ,N'Model' , N'MSDB')
)


insert into #BackupStatus
(Server_name , Full_Backup_Status_Weekly,Diff_Backup_Status_Daily,Transaction_log_backup_Hourly)
SELECT  c.[server_name]  ,

       -- c.[database_name] ,
       -- d.[recovery_model_desc] ,
        --c.[last_full_backup] ,
        --c.[last_diff_backup] ,
        --c.[last_tran_backup] ,
        CASE 
            WHEN c.[days_since_full_backup] <= 7 THEN 'Success'
            WHEN c.[days_since_full_backup] >= 7 THEN 'Failed'
        ELSE NULL END AS Full_Backup_Status_Weekly,
            CASE 
            WHEN c.[days_since_diff_backup] <= 1 THEN 'Success'
            WHEN c.[days_since_diff_backup] >= 1 THEN 'Failed'
        ELSE NULL END AS Diff_Backup_Status_Daily,
        CASE 
            WHEN c.[hours_since_tranlog_backup] <= 1 THEN 'Success'
            WHEN c.[hours_since_tranlog_backup] >= 1 THEN 'Failed'
        ELSE NULL END AS Transaction_log_backup_Hourly


       --c.[days_since_full_backup] ,
       -- c.[days_since_diff_backup] ,
       -- c.[hours_since_tranlog_backup] 
        --c.[full_backup_location] ,
        --c.[diff_backup_location] ,
        --c.[tlog_backup_location]
FROM    [MostRecentBackupStatus_CTE] c
        INNER JOIN [master].[sys].[databases] d ON c.[database_name] = d.[name];

select * from #BackupStatus;

DROP Table #BackupStatus;
4
sepupic

#テーブルに直接挿入することもできますか?

SELECT *
INTO #TempTable
FROM dbo.table1

次に、最初にテーブルを作成することを心配する必要はありません(その後のSELECTステートメントを変更する場合は、定義を変更する必要があります)。

これがあなたのコードの書き直しです

IF OBJECT_ID('#BackupStatus') IS NOT NULL 
DROP Table #BackupStatus

use msdb
go

WITH [MostRecentBackupStatus_CTE]
AS
(
    SELECT  bsfull.[server_name] ,
            bsfull.[database_name] ,
            bsfull.[backup_finish_date] AS [last_full_backup] ,
            bsdiff.[backup_finish_date] AS [last_diff_backup] ,
            bstlog.[backup_finish_date] AS [last_tran_backup] ,
            DATEDIFF(dd, bsfull.[backup_finish_date], CURRENT_TIMESTAMP) AS [days_since_full_backup] ,
            DATEDIFF(dd, bsdiff.[backup_finish_date], CURRENT_TIMESTAMP) AS [days_since_diff_backup] ,
            DATEDIFF(hh, bstlog.[backup_finish_date], CURRENT_TIMESTAMP) AS [hours_since_tranlog_backup] 

    FROM    [msdb]..[backupset] AS bsfull
            LEFT JOIN [msdb]..[backupset] AS bstlog ON bstlog.[database_name] = bsfull.[database_name]
                                                       AND bstlog.[server_name] = bsfull.[server_name]
                                                       AND bstlog.[type] = 'L'
                                                       AND bstlog.[backup_finish_date] = ( (SELECT  MAX([backup_finish_date])
                                                                                            FROM    [msdb]..[backupset] b2
                                                                                            WHERE   b2.[database_name] = bsfull.[database_name]
                                                                                                    AND b2.[server_name] = bsfull.[server_name]
                                                                                                    AND b2.[type] = 'L') )
            LEFT JOIN [msdb]..[backupset] AS bsdiff ON bsdiff.[database_name] = bsfull.[database_name]
                                                       AND bsdiff.[server_name] = bsfull.[server_name]
                                                       AND bsdiff.[type] = 'I'
                                                       AND bsdiff.[backup_finish_date] = ( (SELECT  MAX([backup_finish_date])
                                                                                            FROM    [msdb]..[backupset] b2
                                                                                            WHERE   b2.[database_name] = bsfull.[database_name]
                                                                                                    AND b2.[server_name] = bsfull.[server_name]
                                                                                                    AND b2.[type] = N'I') )
    WHERE   bsfull.[type] = N'D'
            AND bsfull.[backup_finish_date] = ( (SELECT MAX([backup_finish_date])
                                                 FROM   [msdb]..[backupset] b2
                                                 WHERE  b2.[database_name] = bsfull.[database_name]
                                                        AND b2.[server_name] = bsfull.[server_name]
                                                        AND b2.[type] = N'D') )
            AND EXISTS ( SELECT [name]
                         FROM   [master].[sys].[databases]
                         WHERE  [name] = bsfull.[database_name] )
            AND bsfull.[database_name] NOT IN  (N'tempdb' , N'Master' ,N'Model' , N'MSDB')
)
SELECT  c.[server_name]  ,

       -- c.[database_name] ,
       -- d.[recovery_model_desc] ,
        --c.[last_full_backup] ,
        --c.[last_diff_backup] ,
        --c.[last_tran_backup] ,
        CASE 
            WHEN c.[days_since_full_backup] <= 7 THEN 'Success'
            WHEN c.[days_since_full_backup] >= 7 THEN 'Failed'
        ELSE NULL END AS Full_Backup_Status_Weekly,
            CASE 
            WHEN c.[days_since_diff_backup] <= 1 THEN 'Success'
            WHEN c.[days_since_diff_backup] >= 1 THEN 'Failed'
        ELSE NULL END AS Diff_Backup_Status_Daily,
        CASE 
            WHEN c.[hours_since_tranlog_backup] <= 1 THEN 'Success'
            WHEN c.[hours_since_tranlog_backup] >= 1 THEN 'Failed'
        ELSE NULL END AS Transaction_log_backup_Hourly


       --c.[days_since_full_backup] ,
       -- c.[days_since_diff_backup] ,
       -- c.[hours_since_tranlog_backup] 
        --c.[full_backup_location] ,
        --c.[diff_backup_location] ,
        --c.[tlog_backup_location]
into #BackupStatus
FROM    [MostRecentBackupStatus_CTE] c
        INNER JOIN [master].[sys].[databases] d ON c.[database_name] = d.[name];

        SELECT * FROM #BackupStatus
        DROP Table #BackupStatus

SQL Server 2016では、DROP TABLE IF EXISTS #BackupStatus https://blogs.msdn.Microsoft.com/sqlserverstorageengine/2015/11/03/drop-if-exists-new-thing-in -sql-server-2016 /

これが役に立てば幸い

B

1
Bertrand Leroy