Solved

Help SQL SP If Null Statement

Posted on 2008-06-22
2
1,728 Views
Last Modified: 2012-08-13
Could someone please advise why this sql sp receives this error "Msg 156, Level 15, State 1, Procedure qry_Invoice_To_Run, Line 22
Incorrect syntax near the keyword 'HAVING'."
I'm having trouble with the if is null code at the end.

Thank you.

ALTER PROCEDURE [dbo].[qry_Invoice_To_Run]
       @startdate datetime,                  -->[forms]![frminvoicecreator]![startdate]
       @enddate datetime,                        -->[forms]![frmInvoiceCreator]![enddate]
       @clientnumber int =  null,
       @subnumber int = null
AS
BEGIN
      SET NOCOUNT ON
set identity_insert tbx_invoice_run on
INSERT INTO tbx_Invoice_run ( ID, [CLIENT NUMBER], SubNumber, OrganizationName )
SELECT     CLIENTS.ID, CLIENTS.[CLIENT NUMBER], CLIENTS.SubNumber, CLIENTS.OrganizationName
FROM         CLIENTS INNER JOIN
                      MATTERS ON CLIENTS.SubNumber = MATTERS.SubNumber AND CLIENTS.[CLIENT NUMBER] = MATTERS.[CLIENT NUMBER]
WHERE     (MATTERS.[Actual Closing Date] BETWEEN @startdate AND @enddate)
GROUP BY CLIENTS.ID, CLIENTS.[CLIENT NUMBER], CLIENTS.SubNumber, CLIENTS.OrganizationName
If (@clientnumber) = null
      HAVING (CLIENTS.[CLIENT NUMBER]  <> 888
else
      HAVING (CLIENTS.[CLIENT NUMBER]  = @clientnumber and clients.subnumber = @subnumber)
end
ORDER BY CLIENTS.[CLIENT NUMBER], CLIENTS.SubNumberset identity_insert tbx_invoice_run off
0
Comment
Question by:dleads
2 Comments
 
LVL 13

Accepted Solution

by:
MikeWalsh earned 500 total points
ID: 21840466
I am fairly certain that you can't put an IF statement into you where clause like that. If you need to have a conditional having clause like that you will need to use dynamic sql or take care of the variable assignment before you get into the select.
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 21841162
The HAVING clause is only needed when testing the result of an expression in the GROUP BY.
Put all the selection in the Where clause as in the attached snippet
WHERE (MATTERS.[Actual Closing Date] BETWEEN @startdate AND @enddate)
AND  (
      ((@clientnumber is null) AND (CLIENTS.[CLIENT NUMBER]  <> 888)) OR
      ((CLIENTS.[CLIENT NUMBER]  = @clientnumber) AND (clients.subnumber = @subnumber))
     )

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question