0

我有客户的名字和医生的名字。我想从数据库中获取 ID 并将 ID 插入表中。

CREATE PROCEDURE AddMedicalCard
@Client nchar,
@Doctor nchar
AS
BEGIN
    SELECT Clients.[ClientCode] AS [Code]
    Into #NewClientCode
    FROM Clients
    WHERE Clients.[ClientName]=@Client

    SELECT Personal.[DoctorCode] AS [Code]
    Into #NewDoctorCode
    FROM Personal
    WHERE Personal.[DoctorName]=@Doctor

    INSERT INTO MedicalCard 
        VALUES (#NewClientCode.[Code].First, #NewDoctorCode.[Code].First)

    DROP TABLE #NewClientCode
    DROP TABLE #NewDoctorCode
END
GO

错误:

无法绑定多部分标识符“#NewDoctorCode.Code.First”。
无法绑定多部分标识符“#NewClientCode.Code.First”。列名或提供的值的数量与表定义不匹配。

医疗卡表设计:

RecordingCode(Key), ClientCode, DoctorCode 

和其他 5 个可为空的列。

4

4 回答 4

1

错误信息说明了一切。没有这样的事情#NewDoctorCode.Code.First。您的工作可以轻松干净地完成,如下所示。

注意:通过名称获取客户代码和医生代码不是很正确,因为可能有超过 1 个同名的客户和医生。

--declare two variables 
declare @clientCode varchar(50), @doctorCode varchar(50)

--assign clientCode
SELECT TOP 1   @clientCode = Clients.[ClientCode]
FROM Clients
WHERE Clients.[ClientName]=@Client

--assign doctorCode
SELECT TOP 1  @doctorCode = Personal.[DoctorCode]
FROM Personal
WHERE Personal.[DoctorName]=@Doctor

--finally insert them to MedicalCard table
INSERT INTO MedicalCard (clientCodeColumn,doctorCodeColumn) --Column Names
VALUES (@clientCode,@doctorCode)
于 2012-12-25T13:14:25.213 回答
0

做这样的事情

DECLARE @i INT;
SELECT @i = id FROM youtable;
BEGIN
    Insert into tablename(column1) values(@i)
END
于 2012-12-25T13:14:12.763 回答
0

试试这个,你应该使用变量而不是表。如果你使用表,那么你应该使用insert into table select col

DECLARE @NewClientCode int
DECLARE @NewDoctorCode int

SELECT @NewClientCode = Clients.[ClientCode]
FROM Clients
WHERE Clients.[ClientName]=@Client

SELECT @NewDoctorCode = Personal.[DoctorCode]
FROM Personal
WHERE Personal.[DoctorName]=@Doctor

INSERT INTO MedicalCard 
    VALUES (@NewClientCode, @NewDoctorCode)
于 2012-12-25T13:15:07.407 回答
0

尝试使用此 SP:

CREATE PROCEDURE AddMedicalCard
@Client nchar,
@Doctor nchar
AS
BEGIN




    INSERT INTO MedicalCard 
        VALUES (
                SELECT TOP 1 Clients.[ClientCode]
                FROM Clients
                WHERE Clients.[ClientName]=@Client,

                SELECT TOP 1 Personal.[DoctorCode]
               FROM Personal
               WHERE Personal.[DoctorName]=@Doctor
              )


END
GO
于 2012-12-25T13:16:46.160 回答