December 11, 2020 at 11:40 am
Hello,
I am not great with SQL and the statement I wrote is in TSQL and it is working. I would like this same statement to run in MSSQL, can you please restate it correctly or put me on the right track?
It is the With statement that is killing me! I think it needs to be inserted in a query...
Thanks
WITH ret AS(
SELECT
Rel.PrtID,
Rel.ChdId,
Rel.DTd,
Rel.DimId
FROM Rel
WHERE ParentId = 12
AND Rel.DTd = 0
AND Rel.DimId = 0
UNION ALL
SELECT
SecondRel.PrtID,
SecondRel.ChdId,
SecondRel.DTd,
SecondRel.DimId
FROM Rel SecondRel INNER JOIN
ret r ON SecondRel.PrtID = r.ChdId
)
SELECT
ParentId
,ChdId
, ret.DTd
, MB.MBId
,MB.Name
FROM ret
INNER JOIN dbo.MB MB
ON ret.ChdId = MB.MBId
AND ret.DTd = MB.DTd
AND ret.DimId = MB.DimId
December 11, 2020 at 11:44 am
What do you mean by MSSQL? T-SQL is MSSQL, as far as I know.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 11, 2020 at 11:57 am
Hi Phil!
Thanks for your answer! I like the "as far as I know"... and I am sure you know a lot more than me 🙂
I think I am not on the right track then... I need rethink that.
Thanks Phil!
December 11, 2020 at 11:58 am
What application or database are you trying to make this work in?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 11, 2020 at 12:50 pm
Are you terminating the preceding statement with a semicolon?
You can start statements that begin with "WITH" with a semicolon:
;WITH ret AS(
December 11, 2020 at 10:53 pm
Are you terminating the preceding statement with a semicolon?
You can start statements that begin with "WITH" with a semicolon:
;WITH ret AS(
Now, there's an interview question if I ever heard one. Name one place where you CANNOT prefix a WITH for a CTE with a semi-colon?
--Jeff Moden
Change is inevitable... Change for the better is not.
December 11, 2020 at 10:56 pm
Jonathan AC Roberts wrote:Are you terminating the preceding statement with a semicolon?
You can start statements that begin with "WITH" with a semicolon:
;WITH ret AS(Now, there's an interview question if I ever heard one. Name one place where you CANNOT prefix a WITH for a CTE with a semi-colon?
At least within a function. I suspect there might be another place or two as well.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
December 11, 2020 at 10:58 pm
Hello,
I am not great with SQL and the statement I wrote is in TSQL and it is working. I would like this same statement to run in MSSQL, can you please restate it correctly or put me on the right track? It is the With statement that is killing me! I think it needs to be inserted in a query...
Thanks
WITH ret AS(
SELECT
Rel.PrtID,
Rel.ChdId,
Rel.DTd,
Rel.DimId
FROM Rel
WHERE ParentId = 12
AND Rel.DTd = 0
AND Rel.DimId = 0
UNION ALL
SELECT
SecondRel.PrtID,
SecondRel.ChdId,
SecondRel.DTd,
SecondRel.DimId
FROM Rel SecondRel INNER JOIN
ret r ON SecondRel.PrtID = r.ChdId
)
SELECT
ParentId
,ChdId
, ret.DTd
, MB.MBId
,MB.Name
FROM ret
INNER JOIN dbo.MB MB
ON ret.ChdId = MB.MBId
AND ret.DTd = MB.DTd
AND ret.DimId = MB.DimIdPlease describe what you mean by "It is the With statement that is killing me!" What errors are you getting? Where are you actually trying to run this code? And what is the difference between ParentId and PrtID (which is what I think your problem may be)?
--Jeff Moden
Change is inevitable... Change for the better is not.
December 14, 2020 at 7:55 am
Hi Guys!
I start the WITH statement without the semi column ; then I will test also starting with the semi column.
I had to rewrite all of it and now it is working, but without the WITH. I need to investage further on that.
Thanks gys!
December 14, 2020 at 10:45 pm
Jeff Moden wrote:Jonathan AC Roberts wrote:Are you terminating the preceding statement with a semicolon?
You can start statements that begin with "WITH" with a semicolon:
;WITH ret AS(Now, there's an interview question if I ever heard one. Name one place where you CANNOT prefix a WITH for a CTE with a semi-colon?
At least within a function. I suspect there might be another place or two as well.
That's the one I was thinking of (iTVFs that is... I'm pretty sure that you can use it in mTVFs but I don't use those and I've never tried in a Scalar UDF because I try to avoid doing anything that might require such a thing in those). As you say, there may be a couple of more places but haven't run into them, yet.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 15, 2020 at 12:52 am
Hi Guys!
I start the WITH statement without the semi column ; then I will test also starting with the semi column. I had to rewrite all of it and now it is working, but without the WITH. I need to investage further on that.
Thanks gys!
You have not yet told us what you're writing the code in. Are you using SSMS to write code against tables in SQL Server or something else? And, very specifically, what is/are the precise error(s) that you're getting?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply