将解析后的数据从一个数据库插入另一个数据库

时间:2017-04-17 18:51:57

标签: sql sql-server insert-into

以下查询从现有数据库中提取特定数据:

SELECT (
   SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'File Name') AS File_Name,
  (SELECT AttributeData FROM SQLTable WHERE AttributeName = 'Version Number') AS Version,
  (SELECT 'Category Name') AS Category,
   CAST(LEFT((SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'), 
   CHARINDEX('m', (SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'),1 )-2) AS float) AS Value,
   GETDATE() AS Date
FROM SQLTable2
GROUP BY FinalDisposition

UNION

SELECT (
   SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'File Name') AS File_Name,
  (SELECT AttributeData FROM SQLTable WHERE AttributeName = 'Version Number') AS Version,
  (SELECT 'Category Name') AS Category,
   CAST(LEFT((SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'), 
   CHARINDEX('m', (SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'),1 )-2) AS float) AS Value,
   GETDATE() AS Date
FROM SQLTable2
GROUP BY FinalDisposition

UNION

SELECT (
   SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'File Name') AS File_Name,
  (SELECT AttributeData FROM SQLTable WHERE AttributeName = 'Version Number') AS Version,
  (SELECT 'Category Name') AS Category,
   CAST(LEFT((SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'), 
   CHARINDEX('m', (SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'),1 )-2) AS float) AS Value,
   GETDATE() AS Date
FROM SQLTable2
GROUP BY FinalDisposition

这会创建一个如下所示的表:

| File_Name | Version |  Category  | Value |           Date          |
|:---------:|:-------:|:----------:|:-----:|:-----------------------:|
| File1     | 1.0.1   | Duration 1 |  0.04 | 2017-04-17 12:00:00.000 |
| File2     | 1.0.1   | Duration 2 | 0.008 | 2017-04-17 12:00:00.000 |
| File3     | 1.0.1   | Duration 3 | 0.066 | 2017-04-17 12:00:00.000 |

作为旁注,价值具有"额外" bits是因为原始数据的格式如下所示:0.04 minutes0.008 minutes0.066 minutes。我想摆脱这些分钟并投下一个漂浮物。

如何将该数据插入到另一个表中(我已将此表格与我要插入的表格的列格式相同)。

或者换句话说,如何将该信息移动到这样的插入语句中:

INSERT INTO [Database].dbo.SQLTable3(File_Name,Version,Category,Value,Date)

1 个答案:

答案 0 :(得分:0)

    INSERT INTO SQLTable3
    SELECT *
    FROM(   

            SELECT (
               SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'File Name') AS File_Name,
              (SELECT AttributeData FROM SQLTable WHERE AttributeName = 'Version Number') AS Version,
              (SELECT 'Category Name') AS Category,
               CAST(LEFT((SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'), 
               CHARINDEX('m', (SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'),1 )-2) AS float) AS Value,
               GETDATE() AS Date
            FROM SQLTable2
            GROUP BY FinalDisposition

            UNION

            SELECT (
               SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'File Name') AS File_Name,
              (SELECT AttributeData FROM SQLTable WHERE AttributeName = 'Version Number') AS Version,
              (SELECT 'Category Name') AS Category,
               CAST(LEFT((SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'), 
               CHARINDEX('m', (SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'),1 )-2) AS float) AS Value,
               GETDATE() AS Date
            FROM SQLTable2
            GROUP BY FinalDisposition

            UNION

            SELECT (
               SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'File Name') AS File_Name,
              (SELECT AttributeData FROM SQLTable WHERE AttributeName = 'Version Number') AS Version,
              (SELECT 'Category Name') AS Category,
               CAST(LEFT((SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'), 
               CHARINDEX('m', (SELECT AttributeData FROM SQLTable1 WHERE AttributeName = 'Duration 1'),1 )-2) AS float) AS Value,
               GETDATE() AS Date
            FROM SQLTable2
            GROUP BY FinalDisposition)X