[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help SQL SP If Null Statement

Posted on 2008-06-22
2
Medium Priority
?
1,735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
MikeWalsh earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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