2016-10-19 3 views
0

以下のストアドプロシージャが動作しますが、効率的でもエレガントではありません。 3つのクエリではなく、1つのクエリで@_score、@_comment、および@_noteの3つの値を取得する方法がありますか?SQL行からストアドプロシージャの既存の値を選択する:これを行うより効率的な方法はありますか?


ALTER PROCEDURE [dbo].[updateScore] 
      @sqaeid nvarchar(30), 
      @username nvarchar(30), 
      @scoreid nvarchar(30), 
      @score nvarchar(30), 
      @comment nvarchar(max), 
      @note nvarchar(max) 
AS 
/* Capture the existing three values that will get 
    overwritten with new data */ 
declare @_score as nvarchar(30) 
declare @_comment as nvarchar(max) 
declare @_note as nvarchar(max) 

/******************************************************************/ 
/* Is there a more elegant, and efficient, way to do this?  */ 
/******************************************************************/ 
Set @_score = (Select score from tScore where [email protected]) 
Set @_comment = (Select comment from tScore where [email protected]) 
Set @_note = (Select note from tScore where [email protected]) 
/******************************************************************/ 

/* Update the table row with the new data */ 
UPDATE tScore Set [email protected], [email protected], [email protected] WHERE [email protected] 

/* Generate a log entry that will capture the old data and the new data in 
    case we want to rollback to the previous values */ 
INSERT into tLog 
VALUES (@sqaeid, @username, GETDATE(), 'updateScore', 
     'score_id', @scoreid, 'new score', @score, 
     'new comment', @comment, 'new note', @note, 
     'old score', @_score, 'old comment', @_comment, 
     'old note', @_note); 

答えて

0
ALTER PROCEDURE [dbo].[updateScore] 
      @sqaeid nvarchar(30), 
      @username nvarchar(30), 
      @scoreid nvarchar(30), 
      @score nvarchar(30), 
      @comment nvarchar(max), 
      @note nvarchar(max) 
AS 

UPDATE tScore 
    Set score = @score 
     , comment = @comment 
     , note = @note 
Output 
    @sqaeid, @username, GETDATE(), 'updateScore', 
     'score_id', @scoreid, 
     'new score', inserted.score, 
     'new comment', inserted.comment, 
     'new note', inserted.note, 
     'old score', deleted.score, 
     'old comment', deleted.comment, 
     'old note', deleted.note 
Into tlog 
WHERE score_id = @scoreid 
+0

。 tLogファイルにはIDフィールドがあるため、最初のソリューションは完全に機能しますが、2番目のソリューションは最もエレガントで効率的です。 – Jmarien

1

はい、とあなたの3 SET秒を置き換える:提供どちらのソリューションは良いです

SELECT @_score  = score, 
     @_comment = comment 
     @_note  = note 
FROM tScore 
WHERE score_id = @scoreid; 
関連する問題