这是我的存储过程,它从列表框中获取输入并显示与所选项目相关的记录。但如果我没有从列表框中选择任何内容,则需要显示所有未发生的记录。
这是我的存储过程
USE [MyDb]
GO
/****** Object: StoredProcedure [dbo].[usp_SearchCAMAFunctionalObsolescence] Script Date: 10/18/2016 12:30:08 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_SearchCAMAFunctionalObsolescence]
@section as nvarchar(max),
@quality as nvarchar(max),
@style as nvarchar(max)
As
Begin
set nocount on;
Declare @Where as varchar(max)
Declare @Select as varchar(max)
Set @Select = ' Distinct vi.struct_no as structure,a.assesmt_no as assessment,a.parcel_no as parcel, o.own_last+'' , ''+o.own_first as taxpayer, id.year_built as built, id.effect_age as age, id.mkt_adj as fo, vi.aprais_val as mktvalue
From assessments a
inner join parcel p on a.parcel_no = p.parcel_no
inner join valueimp vi on vi.assesmt_no = a.assesmt_no
inner join owner o on o.id = a.owner_id
inner join imp_details id on id.improvementId = vi.id and (id.isdeleted is null or id.isdeleted = 0)
inner join quality_details qd on qd.quality_id = id.quality_id
inner join section_details sd on sd.section_id = id.section_id
inner join style_details stdl on stdl.style_id = id.style_id'
Set @Where = ' where (' + @section + ' is null or sd.section_id = ' + @section + ') and (' + @quality + ' is null or qd.quality_id = ' + @quality + ') and (' + @style + ' is null or stdl.style_id = ' + @style + ')'
DECLARE @QUERY NVARCHAR(MAX)
SET @QUERY= 'Select '+ @SELECT + @WHERE
print @QUERY
EXEC sp_executesql @QUERY , N'@section as int ,@quality as int,@style as int' ,@section ,@quality,@style
END
if i execute stored procedure in this way
// EXEC usp_SearchCAMAFunctionalObsolescence 'null','null','null'
it display all records.
but i need to execute stored procedure in this way
// EXEC usp_SearchCAMAFunctionalObsolescence null,null,null
and it not display anything
答案 0 :(得分:1)
为此,您不需要动态sql。如果您使用的是mysql,请在WHERE
语句中使用IFNULL。(SQL Server
使用ISNULL使用NVL代替使用np.kron
。
如果输入变量为null,则脚本将返回实际的列值。
USE [MyDb]
GO
/****** Object: StoredProcedure [dbo].[usp_SearchCAMAFunctionalObsolescence] Script Date: 10/18/2016 12:30:08 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_SearchCAMAFunctionalObsolescence]
@section as nvarchar(max),
@quality as nvarchar(max),
@style as nvarchar(max)
As
Begin
set nocount on;
SELECT Distinct vi.struct_no as structure,a.assesmt_no as assessment,a.parcel_no as parcel, o.own_last+'' , ''+o.own_first as taxpayer, id.year_built as built, id.effect_age as age, id.mkt_adj as fo, vi.aprais_val as mktvalue
From assessments a
inner join parcel p on a.parcel_no = p.parcel_no
inner join valueimp vi on vi.assesmt_no = a.assesmt_no
inner join owner o on o.id = a.owner_id
inner join imp_details id on id.improvementId = vi.id and (id.isdeleted is null or id.isdeleted = 0)
inner join quality_details qd on qd.quality_id = id.quality_id
inner join section_details sd on sd.section_id = id.section_id
inner join style_details stdl on stdl.style_id = id.style_id
WHERE sd.section_id =IFNULL(@section,sd.section_id)
AND qd.quality_id = IFNULL(@quality,qd.quality_id)
AND stdl.style_id = IFNULL(@style,stdl.style_id )
END
答案 1 :(得分:0)
为什么不创建一个返回未过滤结果的存储过程,然后在没有选择时在代码中调用它?
CREATE PROCEDURE [dbo].[usp_SearchCAMAFunctionalObsolescenceUnfiltered]
As
Begin
set nocount on;
SELEcT Distinct vi.struct_no as structure,a.assesmt_no as assessment,a.parcel_no as parcel, o.own_last+'' , ''+o.own_first as taxpayer, id.year_built as built, id.effect_age as age, id.mkt_adj as fo, vi.aprais_val as mktvalue
From assessments a
inner join parcel p on a.parcel_no = p.parcel_no
inner join valueimp vi on vi.assesmt_no = a.assesmt_no
inner join owner o on o.id = a.owner_id
inner join imp_details id on id.improvementId = vi.id and (id.isdeleted is null or id.isdeleted = 0)
inner join quality_details qd on qd.quality_id = id.quality_id
inner join section_details sd on sd.section_id = id.section_id
inner join style_details stdl on stdl.style_id = id.style_id
END