1

I want know how to release IF with update and how to exec procedure with nvarchar input. There are table Dictionary with 2 values ('Orig','Translated') I need procedure that adds or replace "Trasnslated' depending on input. There must be 2 inputs, no more and no less. For example

CREATE PROCEDURE Translate_Orig (@Orig nvarchar(32),@Translated nvarchar(32))
AS
BEGIN
UPDATE Dictionary
IF  EXISTS (SELECT * FROM Dictionary WHERE Dictionary.Orig=@Orig)
SET Dictionary.Translated=@Translated
ELSE INSERT INTO Dictionary VALUES (@Orig, @Translated);
END
GO

SET @Orig = N'Orig'
SET @Translated  = N'traslated' 
EXEC Translate_Orig (@Orig,@Translated);
4

3 回答 3

1

确保您只是通过 GO 作为语句运行 CREATE PROCEDURE。

于 2013-05-28T14:00:39.357 回答
1
 CREATE PROCEDURE Translate_Orig (@Orig nvarchar(32),@Translated nvarchar(32))
 AS
 BEGIN

      IF  EXISTS (SELECT * FROM Dictionary WHERE Dictionary.Orig=@Orig)
      BEGIN
           UPDATE Dictionary
           SET Dictionary.Translated=@Translated
      END
      ELSE INSERT INTO Dictionary VALUES (@Orig, @Translated);
 END
 GO


 SET @Orig = N'Orig'
 SET @Translated  = N'traslated' 
 EXEC Translate_Orig (@Orig,@Translated);
于 2013-05-28T13:26:54.940 回答
0

那里的 EXEC 工作正常。没有括号。和“SET Dictionary.Translated=@Translated WHERE Dictionary.Orig=@Orig”

CREATE PROCEDURE Translate_Orig (@Orig nvarchar(32),@Translated nvarchar(32))
AS
BEGIN
IF EXISTS (SELECT * FROM Dictionary WHERE Dictionary.Orig=@Orig)
BEGIN
UPDATE Dictionary
SET Dictionary.Translated=@Translated WHERE Dictionary.Orig=@Orig
END
ELSE INSERT INTO Dictionary VALUES (@Orig, @Translated);
END
GO

DECLARE @Orig nvarchar(32);
DECLARE @Translated nvarchar(32);
SET @Orig = N'Name'
SET @Translated = N'Name_Translated'

EXEC Translate_Orig @Orig,@Translated; 
于 2013-05-29T07:04:58.840 回答