我有客户名和医生姓名。我想从DB获取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个可以为空的列。
答案 0 :(得分: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)
答案 1 :(得分:0)
做这样的事情
DECLARE @i INT;
SELECT @i = id FROM youtable;
BEGIN
Insert into tablename(column1) values(@i)
END
答案 2 :(得分: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)
答案 3 :(得分: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