西門子Wincc的歸檔資料批量匯出2
阿新 • • 發佈:2022-12-04
TagLoggingExport.sql指令碼如下:
USE [CC_oval_21_10_27_09_20_33R] GO --儲存過程如果存在,則先刪除儲存過程 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_TagLoggingExportDay]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[sp_TagLoggingExportDay] GO CREATE PROCEDURE [dbo].[sp_TagLoggingExportDay] @TableName varchar(50), --解密表名稱 @DatabaseName varchar(50), --解密資料庫名稱 @DatabasePath varchar(50), --解密資料庫檔案儲存路徑 @BackupName varchar(50), --備份檔案的名稱 @BackupPath varchar(50), --備份檔案的路徑 @SelectDate varchar(50), --查詢的日期,如為空,則查詢前一天,格式為2021-10-26@ArchiveTagName varchar(max) --查詢的變數名不能包含中文,可以為ValueID或者TagName,以;分隔,變數不能超出20,如果是TagName則字元長度不能超出128,如1;2;3或者Archvie1/tag1;Archive1/tag2 AS BEGIN declare @sql nvarchar(max) declare @CurDate varchar(50) --備份資料庫日期字尾 declare @StartTime varchar(50) --查詢開始時間 declare @EndTime varchar(50) --查詢結束時間 declare @Count int --查詢valudid數量,20個為一批 declare @TagName nvarchar(50) --儲存ValueID對應的TagName,用於去掉Energy declare @temp nvarchar(max) --資料庫不存在,則先建立資料庫 IF (NOT EXISTS (SELECT * FROM sys.databases WHERE name = @DatabaseName)) BEGIN SET @sql = N'CREATE DATABASE <NAME> ON (NAME= ''<NAME>'',FILENAME= ''<PATH>\<NAME>.mdf'',SIZE=5mb,MAXSIZE=unlimited,FILEGROWTH=10%)' SET @sql = REPLACE(@sql, '<NAME>', @DatabaseName) SET @sql = REPLACE(@sql, '<PATH>', @DatabasePath) exec(@sql) END --Archive表存在,先刪除 SET @sql = N'[<DataBaseName>].[dbo].[Archive]' SET @sql = REPLACE(@sql, '<DataBaseName>', @DatabaseName) IF OBJECT_ID(@sql,N'u') iS NOT NULL BEGIN SET @sql = 'DROP TABLE [<DataBaseName>].[dbo].[Archive]' SET @sql = REPLACE(@sql, '<DataBaseName>', @DatabaseName) exec(@sql) END --插入Archive到資料庫,方便ValueID與ValueName的關聯 SET @sql = 'Select * into [<DataBaseName>].[dbo].[Archive] FROM [Archive]' SET @sql = REPLACE(@sql, '<DataBaseName>', @DatabaseName) exec(@sql) --表不存在,則先建立表 SET @sql = N'[<DataBaseName>].[dbo].[<TableName>]' SET @sql = REPLACE(@sql, '<DataBaseName>', @DatabaseName) SET @sql = REPLACE(@sql, '<TableName>', @TableName) IF OBJECT_ID(@sql,N'u') iS NULL --表不存在,先建立表 BEGIN SET @sql = 'CREATE TABLE [<DATABASENAME>].[dbo].[<TABLENAME>]( [ValueID] [int] NOT NULL, [Timestamp] [datetime] NOT NULL, [RealValue] [float] NOT NULL, [Quality] [int] NOT NULL, [Flags] [int] NOT NULL )' SET @sql = REPLACE(@sql, '<DATABASENAME>', @DatabaseName) SET @sql = REPLACE(@sql, '<TABLENAME>', @TableName) exec(@sql) END ELSE --表存在,刪除所有資料 BEGIN SET @sql = 'DELETE FROM [<DATABASENAME>].[dbo].[<TABLENAME>]' SET @sql = REPLACE(@sql, '<DataBaseName>', @DatabaseName) SET @sql = REPLACE(@sql, '<TableName>', @TableName) exec(@sql) END --時間處理 IF @SelectDate is NULL --引數為NULL,則查詢前一天歸檔資料 BEGIN --獲取前一天日期時間 SET @StartTime = CONVERT(varchar(100), DATEADD(DAY, -1, GETDATE()),23) + ' 00:00:00.000' SET @EndTime = CONVERT(varchar(100), DATEADD(DAY, -1, GETDATE()),23) + ' 23:59:59.999' --變數記錄時間為UTC,需減8小時 SET @StartTime = CONVERT(varchar(100),DATEADD(HOUR, -8, @StartTime), 25) SET @EndTime = CONVERT(varchar(100),DATEADD(HOUR, -8, @EndTime), 25) SET @CurDate = CONVERT(varchar(100), DATEADD(DAY,-1, GETDATE()),112) END ELSE IF @SelectDate = '0000-00-00' --引數為0000-00-00,則查詢前10分鐘資料,用於測試 BEGIN SET @StartTime = '0000-00-00 00:10:00.000' SET @EndTime = '0000-00-00 00:00:00.000' SET @CurDate = CONVERT(varchar(100), DATEADD(MINUTE,-10, GETDATE()),112) + REPLACE(CONVERT(varchar(100), DATEADD(MINUTE,-10, GETDATE()),108),':','') END ELSE --引數為實際日期時間,則查詢指定日期歸檔資料 BEGIN SET @StartTime = @SelectDate + ' 00:00:00.000' SET @EndTime = @SelectDate + ' 23:59:59.999' --變數記錄時間為UTC,需減8小時 SET @StartTime = CONVERT(varchar(100),DATEADD(HOUR, -8, @StartTime), 25) SET @EndTime = CONVERT(varchar(100),DATEADD(HOUR, -8, @EndTime), 25) SET @CurDate = REPLACE(@SelectDate, '-' ,'') END IF UPPER(@ArchiveTagName) = 'ALL' --查詢Archive表中的所有歸檔資料 BEGIN SET @Count = 0 SET @ArchiveTagName = '' DECLARE RS CURSOR FOR SELECT ValueID FROM [Archive] OPEN RS FETCH NEXT FROM RS INTO @temp WHILE @@Fetch_Status = 0 BEGIN SELECT @TagName = ValueName FROM [Archive] Where ValueID = @temp --如ValueName包含Energy,則略過 IF CHARINDEX('Energy', @TagName) = 0 BEGIN SET @Count = @Count + 1 IF @Count = 20 BEGIN SET @ArchiveTagName = @ArchiveTagName + ';' + @temp Set @sql = 'Insert Into <database>.[dbo].<tableName> Select * from OpenQuery(WINCCOLEDB,N''Tag:R,(<tagName>),''''<startTime>'''',''''<endTime>'''''')' SET @sql = REPLACE(@sql, '<database>', @DatabaseName) SET @sql = REPLACE(@sql, '<tableName>', @TableName) SET @sql = REPLACE(@sql, '<tagName>', @ArchiveTagName) SET @sql = REPLACE(@sql, '<startTime>', @StartTime) SET @sql = REPLACE(@sql, '<endTime>', @EndTime) print('SQL:' + @sql) exec( @sql) --PRINT @ArchiveTagName SET @Count = 0 SET @ArchiveTagName = '' END ELSE BEGIN IF @ArchiveTagName = '' BEGIN SET @ArchiveTagName = @temp END ELSE BEGIN SET @ArchiveTagName = @ArchiveTagName + ';' + @temp END END END FETCH NEXT FROM RS INTO @temp END CLOSE RS DEALLOCATE RS --處理剩餘Tag IF @Count <> 0 BEGIN Set @sql = 'Insert Into <database>.[dbo].<tableName> Select * from OpenQuery(WINCCOLEDB,''Tag:R,(<tagName>),''''<startTime>'''',''''<endTime>'''''')' SET @sql = REPLACE(@sql, '<database>', @DatabaseName) SET @sql = REPLACE(@sql, '<tableName>', @TableName) SET @sql = REPLACE(@sql, '<tagName>', @ArchiveTagName) SET @sql = REPLACE(@sql, '<startTime>', @StartTime) SET @sql = REPLACE(@sql, '<endTime>', @EndTime) print('SQL:' + @sql) END END ELSE --查詢指定ValueID或者ValueName BEGIN Set @sql = 'Insert Into <database>.[dbo].<tableName> Select * from OpenQuery(WINCCOLEDB,''Tag:R,(<tagName>),''''<startTime>'''',''''<endTime>'''''')' SET @sql = REPLACE(@sql, '<database>', @DatabaseName) SET @sql = REPLACE(@sql, '<tableName>', @TableName) SET @sql = REPLACE(@sql, '<tagName>', @ArchiveTagName) SET @sql = REPLACE(@sql, '<startTime>', @StartTime) SET @sql = REPLACE(@sql, '<endTime>', @EndTime) print('SQL:' + @sql) exec( @sql) END --備份資料庫 SET @sql = 'BACKUP DATABASE <DATABASE> TO DISK = ''<PATH>\<NAME><DATE>.bak'' WITH INIT' SET @sql = REPLACE(@sql,'<DATABASE>', @DatabaseName) SET @sql = REPLACE(@sql,'<PATH>', @BackupPath) SET @sql = REPLACE(@sql,'<NAME>', @BackupName) SET @sql = REPLACE(@sql,'<DATE>', @CurDate) exec(@sql) print @sql END GO --示例1:匯出前一天編號為893/894/895;896;897的歸檔資料,儲存在c:\temp\database資料庫TagLoggingDecode中的data表中,並備份到C:\temp\database\backup資料夾 --EXEC sp_TagLoggingExportDay 'data','TagLoggingDecode','c:\temp\database','893;894;895;896;897',null,'TagLogging','c:\temp\database\backup' --示例2:匯出前10分鐘編號為893/894/895;896;897的歸檔資料,儲存在c:\temp\database資料庫TagLoggingDecode中的data表中,並備份到C:\temp\database\backup資料夾 --EXEC sp_TagLoggingExportDay 'data','TagLoggingDecode','c:\temp\database','893;894;895;896;897','0000-00-00','TagLogging','c:\temp\database\backup' --示例3:匯出前一天所有歸檔的歸檔資料,儲存在c:\temp\database資料庫TagLoggingDecode中的data表中,並備份到C:\temp\database\backup資料夾 --EXEC sp_TagLoggingExportDay 'data','TagLoggingDecode','c:\temp\database','ALL',null,'TagLogging','c:\temp\database\backup' --示例4:匯出2021年10月11日的所有歸檔資料,儲存在c:\temp\database資料庫TagLoggingDecode中的data表中,並備份到C:\temp\database\backup資料夾 --EXEC sp_TagLoggingExportDay 'data','TagLoggingDecode','c:\temp\database','ALL','2021-10-11','TagLogging','c:\temp\database\backup'