Question

I have the following query:

-- VARIABLE DECLARATION AND INITIALIZATION
DECLARE @BNP   VARCHAR;

SET @BNP   = '00408500';

/* 
BNP RESULTS
*/
DECLARE @T2 TABLE (
    VISIT              VARCHAR(20)
    , [BNP ORDER #]    VARCHAR(20)
    , [ORDER NAME]     VARCHAR(100)
    , VALUE            VARCHAR(150)
)

INSERT INTO @T2
SELECT
B.episode_no
, B.ord_seq_no
, B.obsv_cd_ext_name
, B.dsply_val

FROM (
    SELECT episode_no
    , ord_seq_no
    , obsv_cd_ext_name
    , dsply_val
    , ROW_NUMBER() OVER (
        PARTITION BY EPISODE_NO ORDER BY ORD_SEQ_NO DESC
        ) AS ROWNUMBER

    FROM smsmir.sr_obsv_curr

    WHERE obsv_cd = @BNP --'00408500'  -- BNP
    AND episode_no = '123456789dummy'
)B
WHERE ROWNUMBER = 1

SELECT * FROM @T2

When I do not use the variable @BNP and instead allow for the '00408500' I get an answer, why not with the variable? Also when I do this I also get an answer DECLARE @BNP VARCHAR(16);

Thank you

Was it helpful?

Solution

The usual suspect I see is the absence of size in the VARCHAR declaration for @BNP.

Could you maybe change

DECLARE @BNP VARCHAR

to

DECLARE @BNP VARCHAR(10)

and check if it works?

EDIT: Reason why I think this might be the problem ISTR that declaring a variable VARCHAR allots it size=1, so it stores just the first character. By explicitly specifying the appropriate size, we ensure the entire string is stored in the variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top