1

再会!我的存储过程有问题,这是我的代码:

INSERT INTO dbo.CS_RefQuestionChoice(ChoiceDescription, QuestionID, RatingID, SetID)

SELECT @choice, @@IDENTITY,@ratingid,1 FROM CS_RefQuestions

代码运行顺利,但我遇到了麻烦,因为它在我的数据库中保存了 75 条记录,它应该只保存一条,我想要执行的只是当我保存另一个选择时,它还应该保存最新的主键用于关系目的的 CS_RefQuestions。

4

5 回答 5

1

抱歉,如果我误解了您的问题,但您似乎可以使用OUTPUT语句中的子句解决问题INSERT- 您可以在MSDN 博客中找到的描述之一

于 2013-07-10T09:47:31.200 回答
0
DECLARE @ident int    
SET @ident = SCOPE_IDENTITY()

    INSERT INTO dbo.CS_RefQuestionChoice 
    (ChoiceDescription, 
             QuestionID, 
             RatingID, 
             SetID
           )
    SELECT choice, 
           @ident, 
           ratingid,
           1 
      FROM CS_RefQuestions 
于 2013-07-10T06:19:16.640 回答
0
DECLARE @ident int    
SET @ident = IDENT_CURRENT(dbo.CS_RefQuestionChoice) AS Current_Identity
INSERT INTO dbo.CS_RefQuestionChoice(ChoiceDescription, QuestionID, RatingID, SetID)

SELECT @choice, @ident ,@ratingid,1 FROM CS_RefQuestions
于 2013-07-10T06:42:53.850 回答
0

感谢所有的反馈,非常感谢。我知道我的问题很难理解,但是我可以在朋友的帮助下回答我的问题。这是我需要的代码:

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
于 2013-07-10T07:03:32.263 回答
0
 INSERT INTO dbo.CS_RefQuestionChoice
       ( ChoiceDescription, 
         QuestionID, 
         RatingID, 
         SetID
       )
SELECT choice, 
       QuestionChoiceID, 
       ratingid,
       1 
  FROM CS_RefQuestions   
  WHERE QuestionChoiceID = SCOPE_IDENTITY() 

我假设QuestionChoiceIDCS_RefQuestions 表中标识列的名称为 。

于 2013-07-10T05:58:36.947 回答