简体   繁体   中英

Sybase Sql query not returning error

While doing a query with multiples tables and multiples variables in a Sybase SQL query, the results weren't what i was expecting.

My table is not complete and is not supposed to be, most of it can recieve NULL as a value (I know NULL is not a value but in let's consider it just for this query, this is not the focus of the question) and it's never going to be completed.

That known, my query is fairly simple, it asks for the user for a few filters that he doesn't need to fill it out, if he just wants a more "open search". While doing tests with this query i realized that one of those filters where not working, even if i filled it, it would still return values as if the user had never filled the filter. Upon closer expection i realized that the function called to make the query was missing that filter so i went and changed it.

    SELECT * FROM 
    CONTROL C, 
    MARKET_CONTROL MC, 
    STORAGE_CONTROL SC
    WHERE c.item_control = mc.item_control
    AND c.item_code = isnull(@itemCode,item_code)  @itemCode is one of the filters
    AND c.item_baseline = isnull(@itemBaseline, item_baseline)
    AND c.item_quantity = sc.item_quantity
    AND c.item_storage = sc.item_storage
    AND others, the list goes on.

While others are working, if i don't give the filter info, it ignores it and searches for himself, returning all possible values right? I mean, the ISNULL function can be translated as this:

    if @itemBaseline IS NULL then
    c.item_baseline = c.item_baseline
    else
    c.item_baseine = @itemBaseline

Right?

Well, i'm not trying to return null or anything, nor am i trying to make it searches for himself so i can return everything. But item_baseline can be added to the control table without any info o it (giving it the NULL value [Again, let's ignore it, just trying to make it easier to understand]). Well, the results of the query, however, instead of returning everything on itemBaseline it works like the user selected IS NOT NULL. I'm not trying to return null in the query, all i want is to make it return every possibility.

PS: I'm using Sybase-based SQLdbx 3.12. PS2: I've tried using alternatives to isnull function like coalesce but it still doesn't work.

It seems that your variables are not defined during execution of this query. I did small experiment on my side :

CREATE TABLE #temp
(
    a INT NULL,
    b INT NULL
)

INSERT #temp 
SELECT 1, NULL
UNION 
SELECT 2, 2

DECLARE @b INt
SELECT @b = 2

SELECT * FROM #temp
WHERE 
b = isnull(@b,b)

Output is filtered properly, if you comment definition of @b ( --SELECT @b = 2 ) you will get entire table. GL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM