【发布时间】:2010-09-06 09:44:22
【问题描述】:
我有以下使用条件 where 子句的 sp - 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# 代码隐藏标记?
抱歉,如果这没有意义。
【问题讨论】:
-
还要注意,这种方法有时会导致使用不正确的查询计划...
标签: sql parameters query-string where-clause