Skip to content
Advertisement

Stored procedure with string as parameters not working -SQL

In my database 'Student_name' is set as varchar. And the stored procedure is:

And I have executed the stored procedure as below:

But it shows an error:

Conversion failed when converting the nvarchar value ‘Yuvan’ to data type int.

Please help to correct.

Advertisement

Answer

Do not concatenate string to build a statement. Use sp_executesql correctly and use parameters (if you really want to use a dynamic statement):

Notes:

  • Define parameters data types carefully. Using nvarchar(max) is not needed. Use the appropriate data type, based on the columns data types.
  • Pass datetime values using an unambiguous datetime format

If possibe, use simple SELECT statement:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement