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

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top