| Author |
Topic |
|
lanti_p
Starting Member
15 Posts |
Posted - 2011-12-23 : 06:20:55
|
Hello gurus,I need some help with these query of mineThe overall structure is with tblshn as(select fileds...from tbl1where conditions1)select fields...from tbl2where conditions2and id not in (select id from tblshn )union select tblshn.*from tblshn now, I want to join all the data set from above results, with another table, so I go and applyselect *, tbl3.*from (with tblshn as(select fileds...from tbl1where conditions1)select fields...from tbl2where conditions2and id not in (select id from tblshn )union select tblshn.*from tblshn ) as masterdatainner join tbl3 on ..... I am getting this error: quote: Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'with'.Msg 319, Level 15, State 1, Line 5Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.Msg 102, Level 15, State 1, Line 39Incorrect syntax near ')'.
can anyone help me on this?thank you all and regards,lanti_p |
|
|
lanti_p
Starting Member
15 Posts |
Posted - 2011-12-23 : 06:24:32
|
quote: Originally posted by GilaMonster Duplicate post. No replies to this thread please. Direct replies to:[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=169509[/url]--Gail ShawSQL Server MVP
Sorry for incovenience ... I already deleted the dublicated topicthnx |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 06:25:16
|
You can't embed a CTE inside a SQL statement. It's the other way around. So, more something like thisWITH tblshn AS ( SELECT fields FROM tbl1 WHERE conditions1 ) SELECT * , tbl3.* FROM ( SELECT fields FROM tbl2 WHERE conditions2 AND id NOT IN ( SELECT id FROM tblshn ) ) AS masterdataINNER JOIN tbl3 on .... --Gail ShawSQL Server MVP |
 |
|
|
lanti_p
Starting Member
15 Posts |
Posted - 2011-12-23 : 06:36:28
|
Thank you for your response, I got the idea now ...many thanks again quote: What about the performance of this query?
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-23 : 06:37:29
|
| whats the need of a cte here? I feel like you can dispense with it and join to tbl1 directly in main query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
lanti_p
Starting Member
15 Posts |
Posted - 2011-12-23 : 06:54:59
|
The overall idea is to get all ID from the CTE part and UNION them with other ID found in second part ... but we do not want dublicatesso I go and apply AND id NOT IN ( SELECT id FROM tblshn ) Can I avoid usage of CTE? Because query is taking so much execution time ... |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 06:56:44
|
| You can substitute the name of the CTE in that subquery with the definition, but it'll do little to nothing for execution time. CTEs are inlined during parsing anyway, they're for readability, not performance.--Gail ShawSQL Server MVP |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-23 : 06:59:14
|
| [code]select *, tbl3.*from (select fields...from tbl2 t2where conditions2and not exists(select 1from tbl1where conditions1and t2.id= yourcorrespondingfield)union select tblshn.*from tblshn ) as masterdatainner join tbl3 on .......[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|