SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


IF clause, cannot get rid of the red squiggle...


IF clause, cannot get rid of the red squiggle...

Author
Message
JJR333
JJR333
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1038 Visits: 395
Hi,
I can't get rid of the red squiggle under the IF statement.
The code is part of a Create Stored Procedure construct.
Any thoughts?
The code worked fine until I added the IF clause to test for the date.
Cheers,
Julian



WITH myCTE as
( SELECT COUNT(*) OVER (PARTITION BY DATEPART(ISO_WEEK, H.BackupDatum), H.zzpclient_id ) AS ROWSperWEEKperBEW,
CONVERT(DATE,H.BackupDatum) AS BACKUPDATUM,
H.zzpclient_id,
ISNULL(H.[Spiegel+TOT],0)-ISNULL(H.ZPTHrsMeerzorg,0) AS SpiegelUren

FROM Bewoners_HISTORY H
WHERE CONVERT(DATE,H.BackupDatum) BETWEEN @DatumBegin AND @DatumEnd
AND H.ZPTIDHuis = @IDHuis
AND H.ZPTIDAfd = @IDAfd
AND LEFT(H.zzpafdeling_oms,3) <> 'GRZ'
AND H.AfdMeetellen <> 'NEE'
AND (H.ZPTCALC <> 'N' OR H.ZPTCALC IS NOT NULL)
)
, myGoodCTE as
(
SELECT C.BackupDatum,
C.zzpclient_id,
-- Sum all the hours of the Bewoners per BackupDatum, after dividing by RowsPerWeekPerBew
SUM(C.[SpiegelUren])/ROWSperWEEKperBEW AS [UrenSpiegel+]
FROM myCTE C
GROUP BY C.BackupDatum, C.zzpclient_id, C.ROWSperWEEKperBEW
)

--> IF dbo.ISOyear(@DatumBegin) >=2018
BEGIN
(SELECT M.BACKUPDATUM, SUM(M.[UrenSpiegel+])
FROM myGoodCTE M
GROUP BY M.BACKUPDATUM);
END
ELSE
BEGIN
(SELECT DatumData, SUM(SpiegelNetto) + SUM(HV2NormHrs) as SpiegelPlus
FROM LocatieFaktor_Historie
WHERE IDHuis = @IDHuis And
IDAfd = @IDAfd AND
DatumData Between @DatumBegin And @DatumEnd
GROUP BY DatumData);
END


GilaMonster
GilaMonster
SSC Guru
SSC Guru (902K reputation)SSC Guru (902K reputation)SSC Guru (902K reputation)SSC Guru (902K reputation)SSC Guru (902K reputation)SSC Guru (902K reputation)SSC Guru (902K reputation)SSC Guru (902K reputation)

Group: General Forum Members
Points: 902305 Visits: 48716
A CTE must be followed immediately by a query. Yours is not, it's followed by an IF statement.
The IF needs to go outside of the CTE.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


JJR333
JJR333
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1038 Visits: 395
Okay, thank you.
J.
drew.allen
drew.allen
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63767 Visits: 16959
GilaMonster - Wednesday, January 24, 2018 9:20 AM
A CTE must be followed immediately by a query. Yours is not, it's followed by an IF statement.
The IF needs to go outside of the CTE.

This makes it sound like the CTE is somehow separate from the query. In fact, the CTE is part of the query in the same way that a WHERE clause or a GROUP BY clause are part of the query. A SELECT statement needs to be atomic, and inserting an IF statement inside of a SELECT statement would potentially invalidate that property.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
jonathan.crawford
jonathan.crawford
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4662 Visits: 1369
flip it around, do IF dbo.ISOyear(@DatumBegin) >=2018 BEGIN <select from the CTE as written with your SELECT right after> END ELSE BEGIN <your other query> END

-------------------------------------------------------------------------------------------------------------------------------------
Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search