MaiOM
MaiOM

Reputation: 936

Problems with SQL EXISTS

I'm going mad on this one. I have the following SP:

ALTER PROCEDURE [BankImport].[spBI_getIsBatchUnique]
    @batchName varchar = 500
AS
BEGIN
    SET NOCOUNT ON;
    IF (EXISTS (SELECT 1 FROM [ACN_Main].[BankImport].[tblBI_Jobs]
            WHERE [batchNumber] = @batchName))
        BEGIN
            SELECT 1
        END
    ELSE
        BEGIN
            SELECT 0
        END     
    END
GO

But whatever the request is, I always get back 0!

I tried to modify it in several ways but with no success! What I need to do is check if there is a record with that batch number and return true, otherwise return false.

Any tip?

Thanks

Upvotes: 1

Views: 147

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28990

I think because you compare batchNumber and batchName, try

EXISTS (SELECT 1 FROM [ACN_Main].[BankImport].[tblBI_Jobs]
        WHERE [batchName] = @batchName)

Upvotes: 1

Chris Moutray
Chris Moutray

Reputation: 18379

Your input parameter looks a bit funny

@batchName varchar = 500

Should it have been this?

@batchName varchar(500)

Ie set the length of the varchar datatype, how you currently have it, 500 is the default value for the batchName param.

Upvotes: 8

Related Questions