I have stored proc that looks something like this (shortened for clarity)
This doesn't work, but if I change the Where clause to use a specific column (see below), it works. What am I missing?
What I am trying to achieve is to insert the record if the GL period is equal to or greater that the GL period in the CompanyCurrentGLinfo table depending on AP, AP, or GL.
Auguy
Sylvania/Toledo Ohio
Code:
@IsPosted int
,@CompanyMasterFK int
,@GLperiod varchar(10)
,@Amount decimal(12, 2)
,@SourceID varchar(20)
,@NewPK int output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO dbo.GLpost
(IsPosted
,CompanyMasterFK
,GLperiod
,Amount
,SourceID)
SELECT
@IsPosted
,@CompanyMasterFK
,@GLperiod
,@Amount
,@SourceID
FROM dbo.Numbers NBR
-- Make sure GL period is valid
WHERE NBR.Number = 1
AND EXISTS (SELECT 1
FROM dbo.CompanyCurrentGLinfo CGLI
WHERE CGLI.CompanyMasterFK = @CompanyMasterFK
AND @GLperiod >=
CASE
WHEN @SourceID = 'AP' THEN CGLI.APcurrentPeriod
WHEN @SourceID = 'AR' THEN CGLI.ARcurrentPeriod
WHEN @SourceID = 'GL' THEN CGLI.GLcurrentPeriod
ELSE '9999-99'
END
)
SET @NewPK = Scope_Identity()
RETURN @NewPK
END
Code:
-- Make sure GL period is valid
WHERE NBR.Number = 1
AND EXISTS (SELECT 1
FROM dbo.CompanyCurrentGLinfo CGLI
WHERE CGLI.CompanyMasterFK = @CompanyMasterFK
AND @GLperiod >= CGLI.ARcurrentPeriod) )
Auguy
Sylvania/Toledo Ohio