我有以下sp使用condtional where子句 - ish!
@MemberId varchar(7),
@LocationId varchar(8),
@UUF1 varchar(150),
@UUF2 varchar(50),
@UUF4 varchar(50),
@IDDesc varchar(255),
@OwnBrand nvarchar(50),
@WeightStatus varchar(50),
@MaterialLevel varchar(10),
@BaseMaterialName varchar(15),
@ExtendedMaterialName varchar(50),
@PackagingTypeCode varchar(20),
@Biodegradable nvarchar(50),
@Recyclability varchar(30),
@Feedback varchar(255)
AS
SELECT MemberId, MemberName, LocationId, IDDesc, UnitId, UnitDescription, UnitUserField1, UnitUserField2, UnitUserField4, IsOwnBrand, OwnBrand,
UnitUserField10, SaleDate, SaleQty, LevelNo, WeightStatus, RevisionSourceCode, RevisionDate, MaterialLevel, BaseMaterialID, BaseMaterialName,
ExtendedMaterialId, ExtendedMaterialName, PackagingTypeCode, UnitWeight, WeightUnitCode, IsBiodegradable, Biodegradable, RecycledContent,
Recyclability, Tonnage, ProductPercentage, Productpriority, Feedback, PriKey
INTO #tblSPPartI
FROM tblWeights
WHERE ((MemberId = @MemberID) OR (@MemberId IS NULL))
AND ((LocationId = @LocationID) OR (@LocationID IS NULL))
AND ((UnitUserField1 = @UUF1) OR (@UUF1 IS NULL))
AND ((UnitUserField2 = @UUF2) OR (@UUF2 IS NULL))
AND ((UnitUserField4 = @UUF4) OR (@UUF4 IS NULL))
AND ((IDDesc= @IDDesc) OR (@IDDesc IS NULL))
AND ((OwnBrand = @OwnBrand) OR (@OwnBrand IS NULL))
AND ((WeightStatus = @WeightStatus) OR (@WeightStatus IS NULL))
AND ((MaterialLevel = @MaterialLevel) OR (@MaterialLevel IS NULL))
AND ((BaseMaterialName = @BaseMaterialName) OR (@BaseMaterialName IS NULL))
AND ((ExtendedMaterialName = @ExtendedMaterialName) OR (@ExtendedMaterialName IS NULL))
AND ((PackagingTypeCode = @PackagingTypeCode) OR (@PackagingTypeCode IS NULL))
AND ((IsBiodegradable = @Biodegradable) OR (@Biodegradable IS NULL))
AND ((Recyclability = @Recyclability) OR (@Recyclability IS NULL))
AND ((Feedback = @Feedback) OR (@Feedback IS NULL))
当我在Sql中测试它时,它可以很好地工作。
但是,我在Web应用程序上下文中使用它,其中参数是使用查询字符串设置的。
在此上下文中永远不会将查询字符串作为null发送 - 如果用户未选择参数,则参数的查询字符串将为“未指定”。
因此,当查询字符串参数传递给上述SQL时,返回的参数值为“未指定”,而不是我要求的值,即空值。
我如何操纵(a)上面的代码以便考虑“未指定”或(b)我是否需要更改我的html / c#codebehind标记?
道歉,如果这没有意义。
答案 0 :(得分:0)
您可以使用COALESCE -
COALESCE(@Feedback, 'Not Specified')
当@feedback为NULL时,这将返回'Not Notified'。
对于你的例子,它将是 -
....WHERE ((MemberId = COALESCE(@MemberID, 'Not Specified'))
AND ((LocationId = COALESCE(@LocationID, 'Not Specified'))
AND ((UnitUserField1 = COALESCE(@UUF1, 'Not Specified')) .....