0
행을 삽입 할 때 :실행 저장 프로 시저/UDF X 번호는 PROC 내가처럼 보이는 저장 프로 시저를 가지고
create proc spInsertDrugQuestions
@drugId int
as
begin
declare @drugName varchar(50)= (select distinct drugName
from Drugs
where DrugId = @drugId)
--class question and answer
declare @drugClassQuestion varchar(250) =
'What is the drug class of your ' + @drugName
declare @drugClassAnswer varchar(50) = (select distinct drugClass
from drugs
where drugId = @drugId)
--dosage question and answer
declare @drugDosageQuestion varchar(250) =
'What is the dosage of your ' + @drugName
declare @drugDosageAnswer varchar(50) = (select distinct drugDosage
from drugs
where drugId = @drugId)
--QuizQuestionTypeId is a foreign key to another table not shown
--but indicates the general type of question (dosage, class, etc.)
insert into DrugQuestions(DrugId,DrugQuestion,CorrectAnswer,QuizQuestionTypeId)
values (@drugId,@drugClassQuestion,@drugClassAnswer,3)
,(@drugId,@drugDosageQuestion,@drugDosageAnswer,1)
end
이 저장 프로 시저는 Drugs
에 새로운 정보를 입력 최종 사용자입니다 테이블과 해당 질문이 DrugQuestions
테이블에 추가됩니다. 당분간, Drugs
테이블에 현재 존재하는 모든 DrugId
에 대해이 저장 프로 시저를 실행할 수 있어야합니다. 최선의 방법은 과 CROSS APPLY
이 모두 DrugId
인 숫자 표에 가입하는 것입니다. 그러나, 나는 UDF에 너무 익숙하지 않아요 및
create function fnInsertDrugQuestions(@drugId int)
returns int --think can't be right, but it didn't give me any read squigglies
as begin
declare @drugName varchar(50)= (select distinct drugName
from Drugs
where DrugId = @drugId)
--class question and answer
declare @drugClassQuestion varchar(250) =
'What is the drug class of your ' + @drugName
declare @drugClassAnswer varchar(50) = (select distinct drugClass
from drugs
where drugId = @drugId)
--dosage question and answer
declare @drugDosageQuestion varchar(250) =
'What is the dosage of your ' + @drugName
declare @drugDosageAnswer varchar(50) = (select distinct drugDosage
from drugs
where drugId = @drugId)
insert into DrugQuestions(DrugId,DrugQuestion,CorrectAnswer,QuizQuestionTypeId)
values (@drugId,@drugClassQuestion,@drugClassAnswer,3)
,(@drugId,@drugDosageQuestion,@drugDosageAnswer,1)
end
나에게 오류를 제공하는 다음과 같은 시도 :
Msg 443, Level 16, State 15, Procedure fnInsertDrugQuestions, Line 21
Invalid use of a side-effecting operator 'INSERT' within a function.
Msg 455, Level 16, State 2, Procedure fnInsertDrugQuestions, Line 21
The last statement included within a function must be a return statement.
내가 몇 가지를 알아야합니다 :
1.) Is it possible to insert data like this using a UDF?
2.) Is it possible to used `CROSS APPLY` with a stored procedure?
3.) Do I really want to loop through all the `DrugId`s to do this?
나는'OUTPUT'..INTO' 절을 몰랐다. 신난다, 항상 당신의 인생을 더 쉽게 만들기 위해 새로운 것을 배울 수 있습니다. – wootscootinboogie