MSSQL nvarchar IF PROCEDURE exec与nvarchar输入

时间:2013-05-28 13:22:00

标签: sql-server if-statement nvarchar

我想知道如何使用更新释放IF以及如何使用nvarchar输入执行过程。 表格词典有2个值('Orig','Translated') 我需要根据输入添加或替换“Trasnslated”的过程。必须有2个输入,不多也不少。例如

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);

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

确保您只是通过GO运行CREATE PROCEDURE作为声明。

答案 2 :(得分: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;