sql - not able to retrieve if the table column has null value -
i have stored procedure find customer, working fine. if customer_city_name
null in table not able retrieve row. sp fails . how have result if customer_city_name
or customer_country_code
is null
exec findcustomer null,'%',null,null,
sp code:
create procedure findcustomer @customernumber nvarchar(100), @customernamepattern nvarchar(35), @customercitynamepattern nvarchar(35), @customercountrycode nvarchar(5) begin declare @sql nvarchar(4000) set @sql = ' select c.customer_number, c.customer_name, c.postal_address_identifier, c.customer_street_or_road_name, c.customer_city_name, c.customer_territory_code, c.customer_postal_code, c.customer_country_code, c.telephone_number, c.mobile_telephone_number, c.fax_number, c.email_address customer c c.customer_number ' + case when @customernumber not null '''' + @customernumber + '''' else 'c.customer_number' end + ' , c.customer_name ' + case when @customernamepattern not null '''' + @customernamepattern + '''' else 'c.customer_name' end + ' , c.customer_city_name ' + case when @customercitynamepattern not null '''' +@customercitynamepattern + '''' else 'c.customer_city_name' end + ' , c.customer_country_code ' + case when @customercountrycode not null '''' +@customercountrycode + '''' else 'c.customer_country_code' end exec sp_executesql @sql
@user2218371 should reconsider alternatives instead of dynamic sql. anyway, alternative code.
create procedure findcustomer @customernumber nvarchar(100), @customernamepattern nvarchar(35), @customercitynamepattern nvarchar(35), @customercountrycode nvarchar(5) begin declare @sql nvarchar(4000) set @sql = ' select c.customer_number, c.customer_name, c.postal_address_identifier, c.customer_street_or_road_name, c.customer_city_name, c.customer_territory_code, c.customer_postal_code, c.customer_country_code, c.telephone_number, c.mobile_telephone_number, c.fax_number, c.email_address customer c ' + case when @customernumber not null ' c.customer_number ''' + @customernumber + '''' + case when @customernamepattern not null ' , ' else '' end else '' end + case when @customernamepattern not null ' c.customer_name ''' + @customernamepattern + '''' + case when @customercitynamepattern not null ' , ' else '' end else '' end + case when @customercitynamepattern not null ' c.customer_city_name ''' + @customercitynamepattern + '''' + case when +@customercountrycode not null ' , ' else '' end else '' end + case when @customercountrycode not null ' c.customer_country_code ''' + @customercountrycode + '''' else '' end exec sp_executesql @sql
Comments
Post a Comment