Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.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 server 插入到mssql中_Sql Server_Insert - Fatal编程技术网

Sql server 插入到mssql中

Sql server 插入到mssql中,sql-server,insert,Sql Server,Insert,我在mssql数据库中有两个表,horse\u data和trainer\u data。horse_数据包含以下列:- 马id、马名、马国籍、马yof、马性别、马公、马坝、马训练员id 和trainer_数据的列为:- trainer\u id, 培训师姓名 驯马师id和驯马师id之间存在外键关系 如何将新记录插入到horse_数据表中,而不是为horse_trainer_id的值输入int,输入培训师的姓名,然后让insert查询在trainer_数据中查找该值,并将正确的培训师id插入hor

我在mssql数据库中有两个表,
horse\u data
trainer\u data
。horse_数据包含以下列:-

马id、马名、马国籍、马yof、马性别、马公、马坝、马训练员id

和trainer_数据的列为:-
trainer\u id,
培训师姓名

驯马师id和驯马师id之间存在外键关系

如何将新记录插入到horse_数据表中,而不是为horse_trainer_id的值输入int,输入培训师的姓名,然后让insert查询在trainer_数据中查找该值,并将正确的培训师id插入horse_数据表的horse_trainer_id列中?

尝试以下操作:

INSERT INTO horse_data (horse_id, horse_name, horse_nationality, 
                        horse_yof, horse_sex, horse_sire, horse_dam, horse_trainer_id)
VALUES (horse_id, horse_name, horse_nationality, horse_yof, 
        horse_sex, horse_sire, horse_dam, 
        (SELECT TOP 1 trainer_id FROM  trainer_data WHERE trainer_name = 'name'))
试试这个:

INSERT INTO horse_data (horse_id, horse_name, horse_nationality, 
                        horse_yof, horse_sex, horse_sire, horse_dam, horse_trainer_id)
VALUES (horse_id, horse_name, horse_nationality, horse_yof, 
        horse_sex, horse_sire, horse_dam, 
        (SELECT TOP 1 trainer_id FROM  trainer_data WHERE trainer_name = 'name'))

我能行。最终它将通过winforms应用程序实现,但首先我只想学习如何在mssql中实现,在mssql设计研究中编写查询。最终它将通过winforms应用程序实现,但首先我只想学习如何在mssql中实现,在mssql设计研究中编写查询!像做梦一样工作。谢谢,太棒了!像做梦一样工作。非常感谢。