SQL Server:从两个表中选择并插入一个表中

编程入门 行业动态 更新时间:2024-10-24 08:21:18
本文介绍了SQL Server:从两个表中选择并插入一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有两个表变量(@temp和@ temp2)的存储过程.

I have a stored procedure with two table variables (@temp and @temp2).

如何从两个临时表中选择值(两个表变量都包含一行)并将它们全部插入一个表中?

How can I select the values from both temp tables (Both table variables contain one row) and insert them all in one table ?

我尝试了以下操作,但此操作不起作用,并且出现错误,提示SELECT和INSERT语句的数量不匹配.

I tried the following but this didn't work and I got the error that the number of SELECT and INSERT statements does not match.

DECLARE @temp AS TABLE ( colA datetime, colB nvarchar(1000), colC varchar(50) ) DECLARE @temp2 AS TABLE ( colD int )

...

INSERT INTO MyTable ( col1, col2, col3, col4 ) SELECT colD FROM @temp2, colA FROM @temp, colB FROM @temp, colC FROM @temp

非常感谢蒂姆的帮助.

推荐答案

由于两个表变量都只有一行,因此您可以交叉联接它们.

As both table variables have a single row you can cross join them.

INSERT INTO MyTable (col1, col2, col3, col4) SELECT t.colA, t.colB, t.colC, t2.colD FROM @temp t CROSS JOIN @temp2 t2

更多推荐

SQL Server:从两个表中选择并插入一个表中

本文发布于:2023-10-26 09:45:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!