Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 CTE and inner join

Author  Topic 

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2011-01-11 : 07:42:29
i am using a CTE expression, as part of a script that copy data between diffrent db's.
in the middle of the script i have the following code :

;WITH
cteGetNewVSOldGroup (oldGroupId, newGroupId)
AS
(
select t1.oldGroupId,t2.id from #tblGroups t1 Inner join
[DB1].[dbo].Dyn_Group t2 on t1.name=t2.name
where t2.CompanyRef=@newCompanyRef and t2.UserRef=@newUserId
)
INSERT INTO [DB1].[dbo].[Dyn_Group2]
([GrandParent]
,[Parent]
,[Child])
select t2.newGroupId,t3.newGroupId,t4.newGroupId from #tblGroup2Group t1
inner join cteGetNewVSOldGroup as t2 on t1.GrandParent=t2.oldGroupId
inner join cteGetNewVSOldGroup as t3 on t1.Parent=t3.oldGroupId
inner join cteGetNewVSOldGroup as t4 on t1.Child=t4.oldGroupId


i get an error : Invalid object name 'cteGetNewVSOldGroup'.
any idea?
thanks
Peleg

RobertKaucher
Posting Yak Master

169 Posts

Posted - 2011-01-11 : 08:44:21
We probably need to see more of the script. Are you selecting from the CTE at any other point in the script?

===
http://www.ElementalSQL.com/
Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2011-01-11 : 09:34:53
this is the second query in the code that use's the CTE :

INSERT INTO dbo.Con_User2Group(EndUserRef,GroupRef)
SELECT t3.id,t4.newGroupId
FROM .dbo.Con_User2Group t1 INNER JOIN
Users t2 ON t1.EndUserRef = t2.ID INNER JOIN
dbo.Users2 t3 on t2.PhoneNumber=t3.PhoneNumber COLLATE SQL_Latin1_General_CP1_CI_AS
INNER JOIN cteGetNewVSOldGroup t4 on t4.oldGroupId=t1.GroupRef
WHERE (t2.CompanyRef = @oldCompanyRef) and t2.UserRef=@oldUserId
and (t3.CompanyRef = @newCompanyRef) and t3.UserRef=@newUserId


but still it point that the error is with the first query.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-11 : 11:11:04
you cant use CTE in second query. It will be available only for the immediate statement after its creation thats why you get invalid object in second insert statement. I think you can make your CTE into temporary table in your case.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2011-01-11 : 14:21:32
you mean that afer the first time i use it it "expires"?
Go to Top of Page

RobertKaucher
Posting Yak Master

169 Posts

Posted - 2011-01-11 : 15:41:57
quote:
Originally posted by pelegk2

you mean that afer the first time i use it it "expires"?



A CTE can only be refereced by the statement that immediately follows it. It is out of scope after that.

===
http://www.ElementalSQL.com/
Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2011-01-12 : 03:03:03
ok 10X
Go to Top of Page
   

- Advertisement -