Question

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);
Was it helpful?

Solution

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

OTHER TIPS

Make sure you are just running the CREATE PROCEDURE through GO as a statement.

There EXEC that works fine. No brackets. And "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; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top