Solved

Help SQL SP If Null Statement

Posted on 2008-06-22
2
1,722 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now