0

I have a stored procedure as follows:

CREATE Procedure [dbo].[GetDrugPackID]
    @Search as nvarchar
As
Begin
    SELECT DrgPack.ID, DrgPack.PackSize, Drg.BrandName, Drg.DIN, Drg.Strength
  FROM [Pharmacy].[dbo].[DrgPack] INNER JOIN
  Drg ON Drg.ID = DrgPack.DrgID INNER JOIN
  DrgPackUPc ON DrgPack.ID = DrgPackUpc.DrgPackId
  WHERE DrgPackUpc.Upc = '@Search'
End

When I run the above by passing the search variable as 04029125070527 I get no results.

But if I edit my stored procedure as follows, I get a result. The only difference is in the above I'm trying to send the variable and in the second its hard coded (not what I want to do, just trying to debug).

CREATE Procedure [dbo].[GetDrugPackID]
    @Search as nvarchar
As
Begin
    SELECT DrgPack.ID, DrgPack.PackSize, Drg.BrandName, Drg.DIN, Drg.Strength
  FROM [Pharmacy].[dbo].[DrgPack] INNER JOIN
  Drg ON Drg.ID = DrgPack.DrgID INNER JOIN
  DrgPackUPc ON DrgPack.ID = DrgPackUpc.DrgPackId
  WHERE DrgPackUpc.Upc = '04029125070527'

What am I doing wrong here?

2
  • you forgot to set the length of your var "@Search as nvarchar" it needs to be "@Search as nvarchar(50)" or what ever length you need Commented May 24, 2021 at 21:43
  • The datatype of @Search should match exactly what DrgPackUpc.Upc is. So if that is varchar(20) for example then you should use that rather than nvarchar(50) Commented May 24, 2021 at 21:54

1 Answer 1

4

You need to declare the size of your nvarchar parameter. Without a size declaration, your parameter is declared implicitly as an nvarchar(1).

In addition, your parameter should not be in single quotes.

WHERE DrgPackUpc.Upc = @Search
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you!! This was it, I missed both of those
Please mark as an answer if it solved your question.
Will do, I'm required to wait 10 minutes to select as solved.

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.