【问题标题】:Using IDs from table and insert into secondary table使用表中的 ID 并插入到辅助表中
【发布时间】:2021-01-03 10:28:18
【问题描述】:

目前我正在尝试编写一个从一个表中选择然后使用主键插入到辅助表中的 sql 语句。我很难弄清楚我会怎么做。这是我现在的选择和插入。第一个选择将返回多个结果。我需要每晚运行一次,所以我需要让它动态化。

SELECT ParentTypeId FROM Config_OrderParentQueueType
INSERT INTO [dbo].[Config_OrderParentQueueTypeNotes]
       ([ParentTypeId]
       ,[NoteDate]
       ,[NoteText]
       ,[NoteSubmittedById])
 VALUES
       (This is the ID I need to insert from the select
       ,GETDATE()
       ,'Default Note'
       ,6)

我试图弄乱行数,但 ID 并不总是连续的。提前感谢任何有关我将如何执行此操作的帮助。

【问题讨论】:

  • 为什么该查询只返回一行?

标签: sql sql-server tsql select sql-insert


【解决方案1】:

使用insert .. select:

INSERT INTO [dbo].[Config_OrderParentQueueTypeNotes]
       ([ParentTypeId]
       ,[NoteDate]
       ,[NoteText]
       ,[NoteSubmittedById])
SELECT ParentTypeId, getdate(), 'Default Note', 6
FROM Config_OrderParentQueueType

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多