插入具有相同数据类型的列中

发布于 2025-02-09 12:25:29 字数 772 浏览 1 评论 0原文

有没有办法在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中插入varcharcolumn2)中的空中中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2025-02-16 12:25:29

一种方法是为test2创建一个与test1相同的列名称的视图。然后,您可以使用与test1中使用的相同名称插入test2

看起来像这样:

create table test1 (
    columnone varchar(max),
    columntwo text,
    columnthree datetime,
    columnfour int
);

create table test2 (
    column1 text,
    column2 varchar(max),
    column3 int,
    column4 datetime
);

CREATE VIEW test2View
AS
SELECT 
    column1 as columntwo,
    column2 as columnone,
    column3 as columnfour,
    column4 as columnthree
FROM   test2;

那么我们可以

insert into test1 (columnone, columntwo, columnthree, columnfour)
values
( 'one','two', '4/4/2024', 4)

insert into test2View (columnone, columntwo, columnthree, columnfour)
values
( 'one','two', '4/4/2024', 4)

One way is to create a view for test2 that has the same column names as test1. Then you can insert into test2 using the same names as used in test1.

It would look something 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
);

CREATE VIEW test2View
AS
SELECT 
    column1 as columntwo,
    column2 as columnone,
    column3 as columnfour,
    column4 as columnthree
FROM   test2;

Then we can

insert into test1 (columnone, columntwo, columnthree, columnfour)
values
( 'one','two', '4/4/2024', 4)

insert into test2View (columnone, columntwo, columnthree, columnfour)
values
( 'one','two', '4/4/2024', 4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文