Sqlserver如何把查詢的資料insert進入新表
首先建立新表:
然後用SQL實現把查詢的資料insert進入新表:
Insert into test.dbo.tmp_tj001(server_principal_name,object_name,maxdate)
select s1.server_principal_name,s1.object_name,max(s1.event_time) as maxdate from
(SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_920000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_520000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_450000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_650000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_980000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_730000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_430000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_670000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_380000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U')s1
group by s1.server_principal_name,s1.object_name
然後用SQL實現把查詢的資料insert進入新表:
Insert into test.dbo.tmp_tj001(server_principal_name,object_name,maxdate)
select s1.server_principal_name,s1.object_name,max(s1.event_time) as maxdate from
(SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_920000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_520000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_450000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_650000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_980000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_730000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_430000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_670000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U'
union all
SELECT distinct server_principal_name,object_name,event_time
FROM sys.fn_get_audit_file('\\10.0.2.116\Audit\Audit_380000.sqlaudit ', NULL, NULL)
WHERE database_name = 'HR'and action_id='CR' and class_type='U')s1
group by s1.server_principal_name,s1.object_name
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22996654/viewspace-2146465/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sqlserver查詢資料的所有表名和行數SQLServer
- 【sqlserver】查詢 表的大小SQLServer
- SQLSERVER查詢某個資料庫有幾張表SQLServer資料庫
- sqlserver表結構查詢SQLServer
- SQLServer查詢所有資料庫大小SQLServer資料庫
- sqlserver查詢長括號[資料SQLServer
- 如何把資料匯入不同的表空間
- 用Navicat把SQLServer資料匯入MySQLServerMySql
- 把txt文字匯入sqlserver表內SQLServer
- Sqlserver資料寫入表測試SQLServer
- 如何使用Java Streams進行資料庫查詢?Java資料庫
- 資料庫基礎查詢--單表查詢資料庫
- Sqlserver查詢出所有表的大小使用情況SQLServer
- SqlServer查詢資料改動歷史記錄SQLServer
- sqlServer的資料庫回縮與表大小檢查。SQLServer資料庫
- mysql查詢表的資料體積MySql
- Mysql 大資料表 資料匯入到SqlServer 中的方法MySql大資料Server
- sqlserver查詢一個庫所有表的記錄數SQLServer
- 用儲存過程把表裡的資料導成insert語句儲存過程
- 把Excel表資料匯入到mysqlExcelMySql
- 資料庫中單表查詢資料庫
- 【資料庫】sql連表查詢資料庫SQL
- 查詢資料庫表是否存在資料庫
- /*列轉行查詢表資料*/
- 把資料匯入到不同的表空間
- 查詢MySQL資料庫,MySQL表的大小MySql資料庫
- 資料庫資料的查詢----連線查詢資料庫
- 查詢SQLSERVER版本號SQLServer
- SqlServer遞迴查詢SQLServer遞迴
- 把TXT文字匯入SQLServer 出錯:資料轉換失敗SQLServer
- 資料庫全表查詢之-分頁查詢優化資料庫優化
- MySql、SqlServer、Oracle 三種資料庫查詢分頁方式MySqlServerOracle資料庫
- sqlserver新增查詢 表、欄位註釋,組合查詢所有的使用者、表名、表註釋SQLServer
- efcore 跨表查詢,實現一個介面內查詢兩個不同資料庫裡各自的表資料資料庫
- 優化mysql資料字典表查詢優化MySql
- INSERT...SELECT語句對查詢的表加鎖嗎
- MySQL(三) 資料庫表的查詢操作【重要】MySql資料庫
- sql查詢一張表的重複資料SQL