简体   繁体   English

T-SQL查询以从一个表中选择并插入到另一个表中

[英]T-SQL query to select from one table and insert into another

Please read the question before marking this as a duplicate. 在将此问题标记为重复之前,请先阅读问题。

I am trying to insert a row into a table with 5 columns. 我试图将行插入到具有5列的表中。 I want 3 of those columns to come from another table, which I will read using a select query. 我希望其中三列来自另一个表,我将使用选择查询来读取该表。

INSERT INTO tbl.Log (
     LogDate,
     Object,
     Count,
     Creator,
     LastUpdate)
SELECT TOP 1 
     GETUTCDATE(),
     Object,
     Count, -- I need to +1 to this
     'John Smith',
     LastUpdate
FROM tbl.Objects
WHERE ObjectID = '123'
ORDER BY LastUpdate DESC

I can see what I am doing wrong here: 我可以在这里看到我做错了什么:

  • GETUTCDATE() should not go in the SELECT because it is not a column name. GETUTCDATE()不应进入SELECT,因为它不是列名。 Where does it go instead? 它去哪儿了?
  • The same is true for 'John Smith' as creator 作为创作者的'John Smith'也是如此
  • The Count column is an int; Count列是一个整数; I need to read the existing number and add one to it before inserting it. 我需要先读取现有号码,然后再添加一个。

I could do this manually by performing two separate queries and parsing the data from the SELECT into the INSERT query, but I am trying to improve my SQL. 我可以通过执行两个单独的查询并将SELECT的数据解析为INSERT查询来手动完成此操作,但是我正在尝试改进SQL。

You can select values that are not a part of your table and do arithmetic on columns during insert . 您可以选择不属于表的值,并在insert过程中对列进行算术运算。

INSERT INTO tbl.Log (
     LogDate,
     Object,
     Count,
     Creator,
     LastUpdate)
SELECT TOP 1 
     GETUTCDATE(),
     Object,
     Count+1
     'John Smith',
     LastUpdate
FROM tbl.Objects
WHERE ObjectID = '123'
ORDER BY LastUpdate DESC

Understand that the idea of doing the insert with two separate queries would result in 2 rows. 了解使用两个单独的查询进行insert的想法将导致两行。

There is nothing wrong with selecting values that doesn't exist on the table, every RDBMS accepts it. 选择表中不存在的值没有错,每个RDBMS都接受它。

Also, what do you mean by count+1 ? 另外,count + 1是什么意思? The count that is inside the log table? 日志表中的计数是多少? If so: 如果是这样的话:

INSERT INTO tbl.Log (
     LogDate,
     Object,
     Count,
     Creator,
     LastUpdate)
SELECT TOP 1 
     GETUTCDATE(),
     Object,
     (SELECT MAX(Count) FROM tbl.log), 
     'John Smith',
     LastUpdate
FROM tbl.Objects
WHERE ObjectID = '123'
ORDER BY LastUpdate DESC

If you meant the count that on the Objects table, that replace the sub select with count+1 , but that just seems like you could have done it your self. 如果要表示Objects表中的计数,则用count+1替换子选择,但这似乎可以自己完成。

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

相关问题 从一个表中选择,插入到另一个表中 oracle sql 查询 - select from one table, insert into another table oracle sql query T-SQL,用于更改一个表中的数据并插入到另一个表中 - T-SQL for changing data from one table and insert into another table 从一个表访问SQL查询以进行SELECT并插入另一个表 - Access SQL query to SELECT from one table and INSERT into another 从服务器复制表并插入另一台服务器:此T-SQL查询出了什么问题? - Copy Table from a Server and Insert into another Server: What is wrong with this T-SQL query? T-SQL:在没有光标的情况下将数据从一个表插入到另一个表 - T-SQL: Insert data from one table to another without curosrs T-SQL:从一个表中选择外键,然后在另一个表中找到对应的行 - T-SQL: Select foreign key from one table and find corresponding rows in another T-SQL用于将数据从一个表合并到另一个表 - T-SQL for merging data from one table to another 使用T-Sql,如何从远程服务器上的一个表插入本地服务器上的另一个表? - Using T-Sql, how can I insert from one table on a remote server into another table on my local server? SQL如果在一个表中不存在,则从另一表中选择/插入行 - SQL Select/insert a row from another table if it doesn't exist in one table SQL query INSERT SELECT COUNT 从一个表逐行到另一个表 - SQL query INSERT SELECT COUNT from one table to another row by row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM