Solved

Help SQL SP If Null Statement

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get row count of current SQL query 8 68
Selection from table2 where criteria for table1 10 68
kill process lock Sql server 9 79
Format Date fields 11 62
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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