why an int datatype variable accept a string format value in stored procedure in SQL Server.

Why does an integer datatype variable accept a string value in a stored procedure in SQL Server? How to solve it?

Last updated:10/13/2021 3:49:18 AM

1 Answers

Ashutosh Kumar Verma
Ashutosh Kumar Verma

Passing a number in string format to an integer variable in a stored procedure :

    When pass a value to integer datatype variable in the form of string value then that accept it because after accept first that value is implicitly CAST to an integer then the value is assigning to the parameter.

    Ex- If we pass an integer value as ' 145 ' to integer type parameter then it implicitly CAST to an integer type value successfully in 145 then it assigned to the parameter of stored procedure then the statement is work file.

    I have create a Stored Procedure with two parameters,

    create procedure sp_Demo(
@ID int,
@Name varchar(255)
--SQL Statement

    We pass Value to parameter of above stored procedure like-

EXEC sp_Demo
@Name=' Radhe Krishna ' ;

The above procedure work file.

If we pass the value ' 145' instead of 145 to @ID parameter and ' Radhe Krishna ' to @Name parameter then also that procedure execute fine because before accepting the value to parameter that value will CAST first to the parameters datatype then assign to the parameters so stored procedure work fine and execute.

Note -     CAST  is function to convert one datatype value in other datatype value. CONVERT() is also used for same purpose.