插入具有相同数据类型的列中
有没有办法在SQL中“排序”数据类型。例如,我有两个这样构建的表:
create table test1
(
columnone varchar(max),
columntwo text,
columnthree datetime,
columnfour int
);
create table test2
(
column1 text,
column2 varchar(max),
column3 int,
column4 datetime
);
insert into test2 (column1, column2, column3, column4)
values ('foo', 'suit3333', 7, 7/4/24)
insert into test2 (column1, column2, column3, column4)
values ('bar', 'person24', 9, 6/31/22)
我需要将Test2中的列与Test 1中具有相同数据类型的列中的列匹配,然后插入数据。
例如,我需要在test2
中插入varchar
(column2
)中的空中中的 colugnnone in test1
。所有列都需要完成此操作。
在我的尝试中,我尝试选择第一列,使用一段循环使用“魔法”动态SQL与相同的数据类型匹配列,插入它,然后丢弃第一列。重复。
这是不起作用的,因为我不知道如何正确使用动态SQL。
Is there a way to "sort" data types in sql. For example, I have two tables built like this:
create table test1
(
columnone varchar(max),
columntwo text,
columnthree datetime,
columnfour int
);
create table test2
(
column1 text,
column2 varchar(max),
column3 int,
column4 datetime
);
insert into test2 (column1, column2, column3, column4)
values ('foo', 'suit3333', 7, 7/4/24)
insert into test2 (column1, column2, column3, column4)
values ('bar', 'person24', 9, 6/31/22)
I need to match the columns in test2 with the columns with the same datatypes in test1, and insert the data.
For example, I would need to insert the varchar
(column2
) in test2
into the empty columnone
in test1
. This needs to be done for all the columns.
In my attempt, I tried selecting the first column, using a while loop to use "magical" dynamic SQL to match the column with the same datatype, insert it, and then drop that first column. Repeat.
This didn't work because I don't know how to properly use dynamic SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是为
test2
创建一个与test1
相同的列名称的视图。然后,您可以使用与test1
中使用的相同名称插入test2
。看起来像这样:
那么我们可以
One way is to create a view for
test2
that has the same column names astest1
. Then you can insert intotest2
using the same names as used intest1
.It would look something like this:
Then we can