将大量数据插入到表中,每行只有1个字段不同

时间:2019-03-14 00:36:38

标签: sql

INSERT INTO Date_Pub1 (Tranche, AssessmentId, Date_Published, Datepub)
VALUES
 ('Tranche 26','14010','2019-03-08 00:00:00.000','08 March 2019'),
 ('Tranche 26','14009','2019-03-08 00:00:00.000','08 March 2019'),
 ('Tranche 26','13648','2019-03-08 00:00:00.000','08 March 2019'),
 ('Tranche 26','14026','2019-03-08 00:00:00.000','08 March 2019');

如何简化这一过程,因为您只能看到评估ID更改

2 个答案:

答案 0 :(得分:0)

一种虚拟解决方案是更改表的列默认值。

ALTER TABLE Date_Pub1 
ADD DEFAULT('Tranche 26') FOR Tranche
# same to the 2 other columns

然后,您可以像这样插入记录,

INSERT INTO Date_Pub1 (AssessmentId)
VALUES('14389')

您可以从此帖子中获得更多信息,How to set a default value for an existing column

答案 1 :(得分:0)

这看起来像是一次编写的语句,用于一次执行。

一种简化此方法的方法:以编程方式生成行,而不是按实际方式编写它们。例如,在Python中:

elif Running == "set_int" and Running == "":

产生:

assessment_ids = ['14010', '14009', '13648', '14026']
sql_statement = """\
    INSERT INTO date_pub1 (tranche, assessmentid, date_published, datepub)
    VALUES
    """ + ",\n    ".join(
        "('Tranche 26', '{}', '2019-03-08 00:00:00.000', '08 March 2019')".format(assessment_id)
        for assessment_id in assessment_ids
    ) + ";"
print(sql_statement)