简体   繁体   中英

Inserting parsed data from one database into another

The following query pulls specific data from an existing database:

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

This creates a table that looks like this:

| 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 |

As a side note, the reason that value has the "extra" bits is because the format of the original data looks like this: 0.04 minutes , 0.008 minutes , and 0.066 minutes . I wanted to get rid of the minutes and cast as a float.

How can I insert that data into another table (I already have this table in the same column format as the table I want to insert it into).

Or in other words, how can I move that information into an insert statement like this:

INSERT INTO [Database].dbo.SQLTable3(File_Name,Version,Category,Value,Date)
    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM