使用另一行中除了一行的值插入行?(Insert row with values from another row except for one?)

编程入门 行业动态 更新时间:2024-10-28 04:20:25
使用另一行中除了一行的值插入行?(Insert row with values from another row except for one?)

我正在尝试创建一个存储过程,该行将使用a,b,c,d列,其中id = @id,并插入一个具有相同a,b,c值的新行,但d将不同。

在这种情况下,大约有50列,但只有一列需要不同。

I am trying to create a stored procedure that will take a row with columns a,b,c,d where id = @id and insert a new row with the same values for a,b,c except d would be different.

In this case there are around 50 columns, though only the one column needs to be different.

最满意答案

假设d是一个INT而你想插入15而不是dbo.oldtable的任何dbo.oldtable ,那么:

INSERT dbo.newtable(a,b,c,d) SELECT a,b,c,d = 15 FROM dbo.oldtable WHERE id = @id;

更可能是变量,所以:

INSERT dbo.newtable(a,b,c,d) SELECT a,b,c,d = @whatever FROM dbo.oldtable WHERE id = @id;

对不起,但没有简写说“除了d之外的所有列”......你需要单独列出它们。 如果键入是问题,有一个简单的方法来处理。 打开对象资源管理器,展开您的服务器,数据库,表和相关表,然后将列节点拖到查询窗口:

在此处输入图像描述

现在,您只需从该列表中删除d列。

Assuming d is an INT and you want to insert, say, 15 instead of whatever is in dbo.oldtable, then:

INSERT dbo.newtable(a,b,c,d) SELECT a,b,c,d = 15 FROM dbo.oldtable WHERE id = @id;

More likely it is from a variable, so:

INSERT dbo.newtable(a,b,c,d) SELECT a,b,c,d = @whatever FROM dbo.oldtable WHERE id = @id;

Sorry, but there is no shorthand to say "all the columns except d"... you need to list them out separately. If typing is the problem, there is an easy way to deal with that. Open Object Explorer, expand your server, database, tables, and the table in question, then drag the columns node onto the query window:

enter image description here

Now you'll just have to delete the d column from that list.

更多推荐

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

发布评论

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

>www.elefans.com

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