简体   繁体   English

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

[英]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 . 附带说明一下,该值具有“额外”位的原因是因为原始数据的格式如下: 0.04 minutes0.008 minutes0.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有条件地将数据从一个数据库插入另一个数据库 - Inserting data from one database to another conditionally 将数据从一个表插入到另一个表中但在另一个数据库中 - Inserting Data from one table into another table but in a different database 将数据从一个表插入数据库中的另一个表(不同的服务器) - Inserting data from one table to another in a database(different servers) 将数据从一台服务器上的一个数据库插入另一台服务器上的另一数据库 - inserting data from one database on one server to another database on other server SQL Server:按特定日期将数据从一个数据库插入到另一个数据库结构 - SQL Server: inserting data from one database to another database structure by specific date 将数据从一个表插入到另一个表? - Inserting data from one table to another? 从另一个数据库更新/插入一个数据库中的表 - Updating/Inserting tables in one database from another database 如果用户选择了一个字段,则将数据从一个表插入到另一个表 - Inserting data from one table to another if one field is chosen by user 在C#中将SQL查询的结果从一个数据库插入另一个数据库 - Inserting result of a SQL query from one database to another in C# 通过将Oracle中的一列分组将数据从一个表插入到另一个表 - Inserting the data from one table to another by grouping one column in Oracle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM