0

I have this stored procedure that doesn't return the record i want and that record exists:

ALTER PROCEDURE [dbo].[Student_SearchByName]
@FName NVarchar
AS
BEGIN
   SELECT 
       StudentID, FName, LName, Student.BirthDate, 
       DATEDIFF(hour, BirthDate, GETDATE()) / 8766 As 'Age' 
   FROM
       Student 
   WHERE 
       Student.FName = @FName
END

I fill the parameter in Visual Studio and also tried:

SELECT 
    StudentID, FName, LName, Student.BirthDate,
    DATEDIFF(hour, BirthDate, GETDATE()) / 8766 As 'Age' 
FROM
    Student 
WHERE
    Student.FName = 'Name'

and it worked, but when I use the parameter I get no result!

2 Answers 2

2

If you specify this:

ALTER PROCEDURE [dbo].[Student_SearchByName]
    @FName NVarchar

then your @FName is exactly ONE character long - you porbably don't want that.

You should always specify an explicit length for any (N)VARCHAR variables and parameters!

Use something like this:

ALTER PROCEDURE [dbo].[Student_SearchByName]
   @FName NVarchar(100)

or whatever makes sense for you.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to specify the length of the NVarchar, otherwise SQL Server truncates to the first char. So it become:

ALTER PROCEDURE [dbo].[Student_SearchByName]
// Student.FName always less than 500 characters
@FName NVarchar(500)
AS
BEGIN
    SELECT StudentID,FName,LName,Student.BirthDate,DATEDIFF(hour,BirthDate,GETDATE())/8766      As 'Age' 
    from Student where Student.FName=@FName
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.