Question

I have a stored procedure that accepts parameter and return tuple with matching values. If no parameter is passed, then return every tuple in the table

create procedure getScore
(
    @clinicCode varchar = null,
)
as
begin
    select * from myTable
    where ClinicCode = isnull(@clinicCode, ClinicCode)
end

so I executte it

exec getScore
exec getScore 'PSH'

both of them return no tuple. I did try select * from myTable, and they returns all tuples. Not sure why the statement from ... isnull(expression, replacement) get messed up

Was it helpful?

Solution

You need to change the declaration of

@clinicCode varchar = null,

to the actual size you require.

So something like

@clinicCode varchar(50) = null,

The reason for this is that

@clinicCode varchar

is the same as

@clinicCode varchar(1)

Which then casts your field isnull(@clinicCode, ClinicCode) to only the first letter of ClinicCode

Have a look at this example

SQL Fiddle DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top