在列名旁边编写带有值的 INSERT 语句?

发布于 2024-10-10 10:32:51 字数 389 浏览 0 评论 0原文

当编写包含很多列的 INSERT 语句时,最好将值放在列名旁边,就像在 UPDATE 语句中一样。比如:

insert into myTable
set
  [col1] = 'xxx',
  [col2] = 'yyy',
  [col3] = 42
  etc...

有什么技巧可以模仿吗?

我想我对此有所了解:

insert into myTable
select
  [col1] = 'xxx',
  [col2] = 'yyy',
  [col3] = 42
  etc...

但别名实际上并不与插入表的列相关联,如果有人向表中添加新列,它真的可能会把事情搞砸。有人对如何做到这一点有任何其他想法吗?

When writing an INSERT statement with a lot of columns, it would be nice to have the value next to the column name like in an UPDATE statement. Something like:

insert into myTable
set
  [col1] = 'xxx',
  [col2] = 'yyy',
  [col3] = 42
  etc...

Are there any tricks to mimic this?

I thought I was onto something with this:

insert into myTable
select
  [col1] = 'xxx',
  [col2] = 'yyy',
  [col3] = 42
  etc...

But the aliases aren't actually being associated with the insert table's columns and if someone added a new column to the table it could really screw things up. Anyone have any other ideas of how one could do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

等你爱我 2024-10-17 10:32:51

您将得到的最接近的结果是在插入中指定列(这将保护您免受添加新列的担忧)并为选择上的值起别名(这为您提供了某种程度的自记录代码)。

insert into myTable
    ([col1], [col2], [col3])
    select 'xxx' as [col1], 'yyy' as [col2], 42 as [col3]

The closest you'll get would be to specify the columns on the insert (this will protect you from your concern about a new column being added) and alias the values on the select (which gives you some degree of self-documenting code).

insert into myTable
    ([col1], [col2], [col3])
    select 'xxx' as [col1], 'yyy' as [col2], 42 as [col3]
绳情 2024-10-17 10:32:51

对于大的插入,我做了花哨的(也许是过于挑剔的)布局。一些示例:

INSERT MyTable ( MyTableId,        Name,             Description,      SomeStringData1
                ,SomeStringData2,  SomeStringData3,  SomeStringData4,  MoreStringData1
                ,MoreStringData2,  MoreStringData3,  MoreStringData4,  SomeNumericData1
                ,SomeNumericData2, SomeNumericData3, SomeNumericData4, MoreNumericData1
                ,MoreNumericData2, MoreNumericData3, MoreNumericData4, BigBlobAA
                ,BigBlobBB,        EnteredAtDate,    UpdatedAtDate,    RevisedAtDate
                ,NeedAnotherDate )
 values
               ( @MyTableId,        @Name,             @Description,      @SomeStringData1
                ,@SomeStringData2,  @SomeStringData3,  @SomeStringData4,  @MoreStringData1
                ,@MoreStringData2,  @MoreStringData3,  @MoreStringData4,  @SomeNumericData1
                ,@SomeNumericData2, @SomeNumericData3, @SomeNumericData4, @MoreNumericData1
                ,@MoreNumericData2, @MoreNumericData3, @MoreNumericData4, @BigBlobAA
                ,@BigBlobBB,        @EnteredAtDate,    @UpdatedAtDate,    @RevisedAtDate
                ,@NeedAnotherDate )

如果您非常确定您不会插入列或以其他方式修改正在插入的内容,则此方法有效。它可以在一个屏幕上显示所有内容,并且可以非常简单地选择哪个值进入哪一列。

如果插入的值可能会更改或很复杂(例如 case 语句),我会执行以下操作(除每五个项目外的所有项目都进行缩进):(

INSERT MyTable
  (
     MyTableId
    ,Name
    ,Description
    ,SomeStringData1
   ,SomeStringData2
    ,SomeStringData3
    ,SomeStringData4
    ,MoreStringData1
    ,MoreStringData2
   ,MoreStringData3
    ,MoreStringData4
    ,SomeNumericData1
    ,SomeNumericData2
    ,SomeNumericData3
   ,SomeNumericData4
    ,MoreNumericData1
    ,MoreNumericData2
    ,MoreNumericData3
    ,MoreNumericData4
   ,BigBlobAA
    ,BigBlobBB
    ,EnteredAtDate
    ,UpdatedAtDate
    ,RevisedAtDate
   ,NeedAnotherDate
  )
 values
  (
     MyTableId
    ,Name
    ,Description
    ,SomeStringData1
   ,SomeStringData2
    ,SomeStringData3
    ,SomeStringData4
    ,MoreStringData1
    ,MoreStringData2
   ,MoreStringData3
    ,MoreStringData4
    ,case
       when something then 'A'
       when orOther   then 'B'
       else 'Z'
     end
    ,SomeNumericData2
   ,SomeNumericData3
    ,SomeNumericData4
    ,MoreNumericData1
    ,MoreNumericData2
    ,MoreNumericData3
   ,MoreNumericData4
    ,BigBlobAA
    ,BigBlobBB
    ,EnteredAtDate
    ,UpdatedAtDate
   ,RevisedAtDate
    ,NeedAnotherDate
  )

添加 CASE 语句后,我“计算缩进数”以确保所有内容都已排列整齐正确地布置。)

需要付出一些努力才能正确地布置事物,但它可以使维护、支持和后续修改变得更简单。

For big inserts, I've done fancy (and perhaps overfly fussy) layouts. Some examples:

INSERT MyTable ( MyTableId,        Name,             Description,      SomeStringData1
                ,SomeStringData2,  SomeStringData3,  SomeStringData4,  MoreStringData1
                ,MoreStringData2,  MoreStringData3,  MoreStringData4,  SomeNumericData1
                ,SomeNumericData2, SomeNumericData3, SomeNumericData4, MoreNumericData1
                ,MoreNumericData2, MoreNumericData3, MoreNumericData4, BigBlobAA
                ,BigBlobBB,        EnteredAtDate,    UpdatedAtDate,    RevisedAtDate
                ,NeedAnotherDate )
 values
               ( @MyTableId,        @Name,             @Description,      @SomeStringData1
                ,@SomeStringData2,  @SomeStringData3,  @SomeStringData4,  @MoreStringData1
                ,@MoreStringData2,  @MoreStringData3,  @MoreStringData4,  @SomeNumericData1
                ,@SomeNumericData2, @SomeNumericData3, @SomeNumericData4, @MoreNumericData1
                ,@MoreNumericData2, @MoreNumericData3, @MoreNumericData4, @BigBlobAA
                ,@BigBlobBB,        @EnteredAtDate,    @UpdatedAtDate,    @RevisedAtDate
                ,@NeedAnotherDate )

This works if you're pretty darn certain that you wont ever be inserting columns or otherwise modifying what is being inserted. It gets gets everything on one screen, and makes it fairly simple to pick out what value goes into which column.

If inserted values are likely to change or are complex (such as case statements), I do the following (outdent all but every fifth item):

INSERT MyTable
  (
     MyTableId
    ,Name
    ,Description
    ,SomeStringData1
   ,SomeStringData2
    ,SomeStringData3
    ,SomeStringData4
    ,MoreStringData1
    ,MoreStringData2
   ,MoreStringData3
    ,MoreStringData4
    ,SomeNumericData1
    ,SomeNumericData2
    ,SomeNumericData3
   ,SomeNumericData4
    ,MoreNumericData1
    ,MoreNumericData2
    ,MoreNumericData3
    ,MoreNumericData4
   ,BigBlobAA
    ,BigBlobBB
    ,EnteredAtDate
    ,UpdatedAtDate
    ,RevisedAtDate
   ,NeedAnotherDate
  )
 values
  (
     MyTableId
    ,Name
    ,Description
    ,SomeStringData1
   ,SomeStringData2
    ,SomeStringData3
    ,SomeStringData4
    ,MoreStringData1
    ,MoreStringData2
   ,MoreStringData3
    ,MoreStringData4
    ,case
       when something then 'A'
       when orOther   then 'B'
       else 'Z'
     end
    ,SomeNumericData2
   ,SomeNumericData3
    ,SomeNumericData4
    ,MoreNumericData1
    ,MoreNumericData2
    ,MoreNumericData3
   ,MoreNumericData4
    ,BigBlobAA
    ,BigBlobBB
    ,EnteredAtDate
    ,UpdatedAtDate
   ,RevisedAtDate
    ,NeedAnotherDate
  )

(After adding that CASE statement, I "counted indents" to make sure I had everything lined up properly.)

It takes a bit of effort to get things layed out properly, but it can make maintenance, support, and subsequent modification simpler.

轻拂→两袖风尘 2024-10-17 10:32:51

基本上,没有。 SQL INSERT 的语法是列出所有列,然后列出所有值。即使您可以找到一种技巧来按照您想要的方式表达语法,它也将是不可移植的,并且会让下一个人不得不维护您的代码。

如果您想要列到值的可视化映射,请使用上面菲利普的答案中的内容。但请不要花费任何宝贵的时间让您的代码避免标准语法。

Basically, no. The syntax for SQL INSERT is to the list all the columns, then all the values. Even if you could find a trick to express the syntax the way you want it would be non-portable and confusing to the next person to have to maintain your code.

If you want a visual mapping of columns to values use something along the lines of Philip's answer, above. But please don't spend any of your valuable time to make your code avoid the standard syntax.

感受沵的脚步 2024-10-17 10:32:51

一旦您想要同时插入多行,使用 2008 风格的行构造函数或使用较旧的 union all 语法,您往往会庆幸您不必指定每一行的列名称。

2008 行构造函数:

insert into myTable (col1,col2,col3)
values ('xxx','yyy',42),
('abc','def',19),
('HJK','www',-4)

UNION all:

insert into myTable (col1,col2,col3)
select 'xxx','yyy',42 union all
select 'abc','def',19 union all
select 'HJK','www',-4

Once you want to insert multiple rows simultaneously, using either 2008 style row constructors, or using the older union all syntax, you tend to be thankful that you don't have to specify the column names for every row.

2008 row constructors:

insert into myTable (col1,col2,col3)
values ('xxx','yyy',42),
('abc','def',19),
('HJK','www',-4)

UNION all:

insert into myTable (col1,col2,col3)
select 'xxx','yyy',42 union all
select 'abc','def',19 union all
select 'HJK','www',-4
南…巷孤猫 2024-10-17 10:32:51

我想出的快速解决方案如下:

insert into MyTable
(ColumnName1, ColumnName2, ColumnName3)
values
(/*ColumnName1*/'value1', /*ColumnName2*/'value2', /*ColumnName3*/'value3')

The quick solution I came up with was the following:

insert into MyTable
(ColumnName1, ColumnName2, ColumnName3)
values
(/*ColumnName1*/'value1', /*ColumnName2*/'value2', /*ColumnName3*/'value3')
梦里°也失望 2024-10-17 10:32:51

INSERT INTO myTable (col1, col2, col3)
 SELECT 'xxx' [col1], 'yyy' [col2], '42' [col3]

INSERT INTO myTable (col1, col2, col3)
 SELECT 'xxx' [col1], 'yyy' [col2], '42' [col3]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文