2009-09-18 46 views
1

我想这样做不检查场,如果参数为空字符串

(s.Reference = @Reference or @Reference = '') 

,但我得到一个SQL错误,说不能转换为nvarchar为bigint。那里发生了什么。如果输入参数是空字符串,我只想跳过Refernce上的查询。

+1

字段和输入参数的数据类型是什么? – Guffa 2009-09-18 11:24:12

+0

你的RDBMS是什么? – 2009-09-18 11:24:29

+0

引用是一个nvarchar(100),Param也是。似乎MsSql的@reference =''有问题 – Damian 2009-09-18 12:54:58

回答

0

对于SQL Server,最好的决定会是这样:

SELECT * 
FROM mytable 
WHERE s.Reference = @Reference 
UNION ALL 
SELECT * 
FROM mytable 
WHERE @Reference IS NULL 

这将优化查询之一,距离仅执行剩余的一个(如果需要使用索引)。

1

它看起来好像参考是一个数字字段。在这种情况下,你可能希望做:

(s.Reference = @Reference or @Reference is null) 

当你把=“”进入测试状态,你做一个假设,它是一个字符串。