Sophart
Sophart

Reputation: 67

Why using Sql parameters are not supported?

If I use the parameters to set the value in order to update the fields, nothing has been updated, but if I assign the value to the field directly, everything works fine. Here is my sql script with parameters that won't work:

DECLARE @ItemValue  NUMERIC(2,1) = 0.0,
@ItemName   NVARCHAR = NULL,
@RuleID     INT,
@IsValid    BIT

SET @ItemValue  =9
SET @ItemName = 'chbWorkingTimePerDay'
SET @RuleID     = 1
SET @IsValid    = 1

UPDATE hrms.RuleValue  

SET ItemValue   = @ItemValue,
    IsValid     = @IsValid

FROM hrms.RuleItem RI

JOIN hrms.RuleValue RV 
ON RI.RuleItemID = RV.RuleItemID

WHERE RI.ItemName =  @ItemName
        AND RV.RuleID = @RuleID

Here is my sql script without parameters that works:

UPDATE hrms.RuleValue  

SET ItemValue   = 8.0,
    IsValid     = 1

FROM hrms.RuleItem RI

JOIN hrms.RuleValue RV 
ON RI.RuleItemID = RV.RuleItemID

WHERE RI.ItemName =  'chbWorkingTimePerDay'
        AND RV.RuleID = 1

Upvotes: 2

Views: 82

Answers (1)

D-Shih
D-Shih

Reputation: 46219

@ItemName NVARCHAR = NULL will translate to @ItemName NVARCHAR(1) = NULL, which will cut off the string. You need to set NVARCHAR size.

So use:

@ItemName   NVARCHAR(100) = NULL

instead of:

@ItemName   NVARCHAR= NULL

SqlFiddle is available here.

Upvotes: 1

Related Questions