1

このストアドプロシージャを機能させようとしています。

ALTER PROCEDURE [team1].[add_testimonial]
-- Add the parameters for the stored procedure here
@currentTestimonialDate char(10),@currentTestimonialContent varchar(512),@currentTestimonialOriginator varchar(20)
AS
BEGIN
DECLARE
@keyValue int

SET NOCOUNT ON;
--Get the Highest Key Value
SELECT @keyValue=max(TestimonialKey)
FROM Testimonial
--Update the Key by 1
SET @keyValue=@keyValue+1
--Store into table
INSERT INTO Testimonial VALUES (@keyValue, @currentTestimonialDate, @currentTestimonialContent, @currentTestimonialOriginator)

END

それでもそれはただ戻る

Running [team1].[add_testimonial] ( @currentTestimonialDate = 11/11/10, @currentTestimonialContent = this is a test, @currentTestimonialOriginator = theman ).

No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [team1].[add_testimonial].

データベースに何も追加されていませんが、何が問題なのでしょうか。

4

2 に答える 2

1

次の 2 つの場所に問題がある可能性があります。

を。テーブルにデータがないため、max(TestimonialKey)return null、以下はそれを処理する適切な方法です。

--Get the Highest Key Value
SELECT @keyValue= ISNULL(MAX(TestimonialKey), 0)
FROM Testimonial
--Update the Key by 1
SET @keyValue=@keyValue+1

b. 列のデータ型がcurrentTestimonialDateそれであるcharDateTime型であるかを確認してください。このフィールドがテーブルの日時型である場合は、テーブルに挿入する前に変換@currentTestimonialDateしてください。DateTime

また、null が許可されていない列の数を確認し、それらにデータを渡しています。

すべての列のデータを渡さない場合は、次のように列名を指定してみてください。

--Store into table
INSERT INTO Testimonial(keyValue, currentTestimonialDate, 
                       currentTestimonialContent, currentTestimonialOriginator) 
              VALUES (@keyValue, @currentTestimonialDate, 
                     @currentTestimonialContent, @currentTestimonialOriginator)

編集:

marc_s からコメントを取得した後:

keyValueとして作成INT IDENTITYします。複数のユーザーが同時に呼び出しても問題はありませんが、DBMS が処理するため、手順の最終的なクエリは次のようになります。

ALTER PROCEDURE [team1].[add_testimonial]
-- Add the parameters for the stored procedure here
@currentTestimonialDate char(10),
@currentTestimonialContent  varchar(512),@currentTestimonialOriginator varchar(20)
AS
BEGIN

  SET NOCOUNT ON;
  --Store into table
  INSERT INTO Testimonial VALUES (@currentTestimonialDate, 
        @currentTestimonialContent, @currentTestimonialOriginator)

END
于 2011-11-28T03:28:32.937 に答える
0

私が見つけることができる2つの問題:

SELECT @keyValue=max(TestimonialKey)

する必要があります

SELECT @keyValue=ISNULL(max(TestimonialKey), 0)

データベースにレコードがない場合を説明するため

次に、を使用するとNOCOUNT ON、挿入された行の数が呼び出し元に返されないと思います。したがって、INSERTステートメントの前に、

SET NOCOUNT OFF
于 2011-11-28T02:59:33.887 に答える