Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL是否将现有/重复行插入表中,但仅更改一列值?_Sql_Sql Server_Select_Insert - Fatal编程技术网

SQL是否将现有/重复行插入表中,但仅更改一列值?

SQL是否将现有/重复行插入表中,但仅更改一列值?,sql,sql-server,select,insert,Sql,Sql Server,Select,Insert,我有超过50列的审计表,只需更改一列值(column CreatedDate,将值设置为GETDATE()),即可插入所需的行(重复)。我知道这可以通过插入SELECT*FROM来实现,但由于有更多的han 50列,代码看起来很混乱: INSERT INTO Audit_Table (Col1, Col2, .....CreatedDate, ......, Col50, ...ColN) SELECT (Col1, Col2, .....GETDATE(), ......, Col50, ..

我有超过50列的审计表,只需更改一列值(column CreatedDate,将值设置为GETDATE()),即可插入所需的行(重复)。我知道这可以通过插入SELECT*FROM来实现,但由于有更多的han 50列,代码看起来很混乱:

INSERT INTO Audit_Table (Col1, Col2, .....CreatedDate, ......, Col50, ...ColN)
SELECT (Col1, Col2, .....GETDATE(), ......, Col50, ...ColN) FROM Audit_Table 
WHERE  Audit_Table.Id = Desired_Id
如果我不应该更改CreatedDate列的值,那将非常简单:

INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id

是否有其他方法可以复制行并仅更改一个/所需的列值?

您可以将记录插入临时表,将CreatedDate列更新为GETDATE(),然后将其插入Audit_表

你可以在现有想法的基础上再接再厉。只需复制行(我假设您有一个自动递增的主键列),然后在单独的查询中更新时间,即

这样做:

INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
然后:

UPDATE Audit_Table SET CreatedDate = GETDATE() WHERE primaryKeyID = newPrimaryKeyID

希望这有帮助

否。除了SQL中的列\u foo之外,没有其他方法可以说
*

解决方法是生成

SELECT
  col1
, col2
, [...]
, coln
FROM foo;
语句(或其中的一部分),通过查询数据库的系统目录,查找按顺序排列的列名。始终存在一个包含所有表的表和一个包含所有列的表。 然后,确保将必要的逗号放在正确的位置(或者在不需要的地方删除它们,或者在报表的所有行中生成逗号,但第一行除外-使用
ROW\u NUMBER()
OLAP函数并评估它是否返回1或其他值)。最后,编辑正确的日期列,将其替换为当前_日期或数据库在当前日期使用的任何内容

祝你好运-
马珂

UPS,我忘了,它是MSSQL。我不会考虑明确地将列名称为凌乱。如果您使用的是SSMS,您只需将列节点从对象资源管理器拖到您的查询中即可。我正要编辑我的答案,以添加另一种解决方法-这正是您的解决方法,Marko。恭喜!
try below as reference

you can use below statement to copy the all rows,
mysql> insert into newstudent select * from students;


you can use below statement to copy the specific row from table,
mysql> insert into newstudent
    -> select id, name, age, address
    -> from students
    -> where
    -> id = 1248;


you can use below statement to copy the either of the row from table,   
    mysql> insert into newstudent
    -> select id, name, age, address
    -> from students
    -> where
    -> id = 1248 or id=1249;

use limit clause also along with this