感謝所有的反饋,大加讚賞。我知道我的問題很難理解,但我可以在朋友的幫助下回答我的問題。這裏是我所需要的代碼:
USE [CCBBS_Survey_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SaveNewQuestionChoice]
@question VARCHAR(200),
@choice VARCHAR(100),
@qno INT,
@categid int,
@ratingid int
AS
BEGIN
IF NOT EXISTS(SELECT QuestionNo FROM CS_RefQuestions where QuestionNo = @qno)
BEGIN
INSERT INTO CS_RefQuestions(Question,QuestionNo,CategoryID, SetID)
VALUES (@question, @qno, @categid, 1)
END
ELSE
BEGIN
UPDATE CS_RefQuestions
SET Question = @question
,CategoryID = @categid
WHERE QuestionNo = @qno
END
DECLARE @QUI INT
SET @QUI = (SELECT TOP(1) QuestionID FROM CS_RefQuestions where QuestionNo = @qno)
INSERT INTO dbo.CS_RefQuestionChoice(ChoiceDescription, QuestionID, RatingID, SetID)
SELECT @choice, @QUI, @ratingid,1 FROM CS_RefQuestions RQ
WHERE QuestionID = @QUI
END
這是行不通的發現在多用戶環境中:'SELECT TOP'不能保證返回剛纔插入的記錄的ID。您應該使用SCOPE_IDENTITY()。 – Curt