insert into patient_record(patient_no, patient_name, datetime_of_birth,
GP_unique_no, GP_name, GP_address,complaint,
treatment, doctor, datetime_treatment_started,
datetime_treatment_ended)
values(13, 'Sophie', '1955-05-13', 'G202',
**select name_of_doctor from gp_doctor where unique_no=G202**,
'Yorkshire', 'Broken right arm', 'Plaster Arm', 'Peter Parker',
'2012-01-25','2012-01-29')
我不知道如何添加G202
。任何想法?
答案 0 :(得分:5)
您无法在INSERT
声明中间进行子选择....您需要在运行INSERT
之前执行此操作:
DECLARE @GPName VARCHAR(100)
SELECT @GPName = name_of_doctor
FROM dbo.gp_doctor
WHERE unique_no = 'G202' -- I assume this is really a *string* here for the "unique_no"
INSERT INTO
dbo.patient_record(patient_no, patient_name, datetime_of_birth,
GP_unique_no, GP_name, GP_address, complaint,
treatment, doctor, datetime_treatment_started,
datetime_treatment_ended)
VALUES
(13, 'Sophie', '1955-05-13',
'G202', @GPName, 'Yorkshire', 'Broken right arm',
'Plaster Arm', 'Peter Parker', '2012-01-25','2012-01-29')
或类似的东西。
答案 1 :(得分:5)
假设unique_no是唯一的,您可以选择该记录以获取名称
insert into patient_record(patient_no, patient_name, datetime_of_birth,
GP_unique_no, GP_name, GP_address,complaint,
treatment, doctor, datetime_treatment_started,
datetime_treatment_ended)
SELECT
13,
'Sophie',
'1955-05-13',
'G202',
name_of_doctor,
'Yorkshire',
'Broken right arm',
'Plaster Arm',
'Peter Parker',
'2012-01-25','2012-01-29'
FROM gp_doctor
where unique_no = 'G202'