Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Two different return values for same(ish) query

Author  Topic 

tibich72
Starting Member

1 Post

Posted - 2012-03-27 : 12:48:08
I'm using Microsoft SQL Server R2 (version 10.5). I'm using a stored procedure to retrieve some data from a bunch of databases we have internally. This is the script that defines the stored procedure:


USE [VP]
GO
/****** Object: StoredProcedure [ML2].[GetVPInfos_Testing2] Script Date: 03/26/2012 13:32:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [ML2].[GetVPInfos_Testing]
@start bigint,
@range integer,
@modulo integer,
@type nvarchar(50),
@subtype nvarchar(50),
@pkgname nvarchar(100)
AS
BEGIN
SELECT DISTINCT
q.company_id AS VendorPartId,
q.manufacturer as Manufacturer,
q.manuf_pn as ManufacturerPartNumber,
t.main as MainType,
t.sub as SubType,
MAX(CASE WHEN p.parameter_id = 43 THEN p.display END) AS [PackageName],
MAX(CASE WHEN p.parameter_id = 36 THEN p.value END) AS [Depth],
MAX(CASE WHEN p.parameter_id = 37 THEN p.value END) AS [Height],
MAX(CASE WHEN p.parameter_id = 38 THEN p.value END) AS [Diameter],
MAX(CASE WHEN p.parameter_id = 39 THEN p.value END) AS [Length],
MAX(CASE WHEN p.parameter_id = 17 THEN p.value END) AS ShapeType

FROM
company.parameter AS p
join company.parts AS q ON p.company_id = q.company_id
join company.types AS t ON t.type_id = q.company_type

WHERE
q.transim_id > @start
and
q.transim_id % @modulo = 0
and
t.main like @type
and
t.sub like @subtype
and /**** problematic line 1 *****/
p.display like @pkgname /**** problematic line 2 *****/
and
q.company_id = 99645 /**** this is just for debugging purposes, will be removed in the final version ***/

GROUP BY q.company_id, q.manufacturer, q.manuf_pn, q.company_type, t.main, t.sub
ORDER BY t.main asc;
END


When I test the procedure, with the parameters:


USE [VP]
GO

DECLARE @return_value int
EXEC @return_value = [ML2].[GetVPInfos_Testing]
@start = 0,
@range = 2000000,
@modulo = 1,
@type = N'%',
@subtype = N'%',
@pkgname = N'0805'
SELECT 'Return Value' = @return_value
GO


I get the following results:

VendorPartId Manufacturer ManufacturerPartNumber MainType SubType PackageName Depth Height Diameter Length ShapeType
99645 Manuf1 PartNumber1 Capacitor Ceramic 0805 NULL NULL NULL NULL NULL


The problem is that the "Depth","Height" and "Length" values are not NULL in the database. If I remove the two lines with the "problematic line" comments:

and /**** problematic line 1 *****/
p.display like @pkgname /**** problematic line 2 *****/


The result is very different:


VendorPartId Manufacturer ManufacturerPartNumber MainType SubType PackageName Depth Height Diameter Length ShapeType
99645 Manuf1 PartNumber1 Capacitor Ceramic 0805 0.00125 0.0022 NULL 0.002 NULL


The values for "Depth", "Height" and "Length" are the correct ones in this case.

I cannot figure out why adding the extra AND condition would modify the resulting values of the unrelated columns ("Depth", "Height", and "Length"). Any help in figuring this one out is really appreciated. I'm very new to SQL, so it might be something obvious, but could not figure out the solution.

Thanks,
Tibi

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-27 : 14:47:49
Isn't it due to your CASE expressions? You don't have an ELSE in there. What should it do when it doesn't match the CASE expression?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-27 : 14:51:53
to further clarify, i think once you put that extra AND condition on, its causing the record with parameter_id values as 36,37 etc to be filtered out and thats why MAX() returns NULL as it doesnt have any aasociated value satisfying the condition

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -