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.
| Author |
Topic |
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 07:09:48
|
| can anybody pls check what is the error here with this common table expressionWITH DataPurgeCountTable2 (Table_Name, Table_Count) AS(select Table_Name, Table_Count from (select 'PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ PROD_SUM] union allselect ' PRODUCTION_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ PRODUCTION_DELAY] union allselect ' PROD_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ PROD_WASTE] union allselect ' SAP_ORDER' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_ORDER] union allselect ' SAP_ORDER_COST' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_ORDER_COST] union allselect ' SAP_ORDER_TM_CONFIRM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_ORDER_TM_CONFIRM] union allselect ' SAP_GOODS_MOVE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_GOODS_MOVE] union allselect ' SAP_NOTIFICATION' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_NOTIFICATION] union allselect ' SAP_ACTIVITY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_ACTIVITY] union allselect ' SAP_CAUSE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_CAUSE] union allselect ' SAP_ITEM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_ITEM] union allselect ' SAP_MEASU_DOCUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ SAP_MEASU_DOCUM] union allselect ' INVALID_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ INVALID_DELAY] union allselect ' INVALID_PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ INVALID_PROD_SUM] union allselect ' INVALID_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ INVALID_WASTE] union allselect ' CRIT_ANLY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ CRIT_ANLY] union allselect ' WORK_HISTORY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ WORK_HISTORY] union allselect ' WORK_HIST_DETL' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ WORK_HIST_DETL] union allselect ' COST_METRICS' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[ COST_METRICS] ) I2) |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 07:11:43
|
| it is just a guess.add semicolon first and try it now.;WITH DataPurgeCountTable2 (Table_Name, Table_Count) ASif it doesn't helpful,please show us the error msg u r getting. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-22 : 07:21:06
|
quote: can anybody pls check what is the error here with this common table expression
Why didn't you post the error you were getting as well?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 07:21:18
|
| nope,still it's showing error as--->Incorrect syntax near ')'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-03-22 : 07:23:31
|
What's the error message ?Does all your table name prefix with a space ?dbo.[_PROD_SUM] Can you also post the full query ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 07:31:02
|
| My full query is WITH DataPurgeCountTable2 (Table_Name, Table_Count) AS(select Table_Name, Table_Count from (select 'PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_SUM] union allselect ' PRODUCTION_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[KC_PRODUCTION_DELAY] union allselect ' PROD_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_WASTE] union allselect ' SAP_ORDER' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER] union allselect ' SAP_ORDER_COST' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_COST] union allselect ' SAP_ORDER_TM_CONFIRM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_TM_CONFIRM] union allselect ' SAP_GOODS_MOVE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_GOODS_MOVE] union allselect ' SAP_NOTIFICATION' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_NOTIFICATION] union allselect ' SAP_ACTIVITY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ACTIVITY] union allselect ' SAP_CAUSE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_CAUSE] union allselect ' SAP_ITEM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ITEM] union allselect ' SAP_MEASU_DOCUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_MEASU_DOCUM] union allselect ' INVALID_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_DELAY] union allselect ' INVALID_PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_PROD_SUM] union allselect ' INVALID_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_WASTE] union allselect ' CRIT_ANLY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[CRIT_ANLY] union allselect ' WORK_HISTORY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HISTORY] union allselect ' WORK_HIST_DETL' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HIST_DETL] union allselect ' COST_METRICS' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[COST_METRICS] ) I2)------------------------------and error message is Msg 102, Level 15, State 1, Line 43Incorrect syntax near ')'. |
 |
|
|
Frank Kalis
Constraint Violating Yak Guru
413 Posts |
Posted - 2010-03-22 : 07:35:39
|
| When you actually query the CTE after declaring it, do you still get the error?--Frank KalisMicrosoft SQL Server MVPWebmaster: http://www.insidesql.org |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-22 : 07:41:07
|
| lol -- yeah -- that works for me also.I get the syntax error if no query immediately after using the CTE.Nice catch Frank.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 07:41:38
|
| yes,for above statement i m still getting an error |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 07:43:15
|
| try this;WITH DataPurgeCountTable2 (Table_Name, Table_Count) AS(select Table_Name, Table_Count from (select 'PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_SUM] union allselect ' PRODUCTION_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[KC_PRODUCTION_DELAY] union allselect ' PROD_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_WASTE] union allselect ' SAP_ORDER' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER] union allselect ' SAP_ORDER_COST' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_COST] union allselect ' SAP_ORDER_TM_CONFIRM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_TM_CONFIRM] union allselect ' SAP_GOODS_MOVE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_GOODS_MOVE] union allselect ' SAP_NOTIFICATION' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_NOTIFICATION] union allselect ' SAP_ACTIVITY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ACTIVITY] union allselect ' SAP_CAUSE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_CAUSE] union allselect ' SAP_ITEM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ITEM] union allselect ' SAP_MEASU_DOCUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_MEASU_DOCUM] union allselect ' INVALID_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_DELAY] union allselect ' INVALID_PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_PROD_SUM] union allselect ' INVALID_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_WASTE] union allselect ' CRIT_ANLY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[CRIT_ANLY] union allselect ' WORK_HISTORY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HISTORY] union allselect ' WORK_HIST_DETL' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HIST_DETL] union allselect ' COST_METRICS' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[COST_METRICS] ) I2)select * from DataPurgeCountTable2 |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 07:47:31
|
| yes it's working ,last select statement (select * from DataPurgeCountTable2) i was missing also how can I use OPTION (MERGE UNION) at the end like;WITH DataPurgeCountTable2 (Table_Name, Table_Count) AS(select Table_Name, Table_Count from (select 'PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_SUM] union allselect ' PRODUCTION_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[KC_PRODUCTION_DELAY] union allselect ' PROD_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_WASTE] union allselect ' SAP_ORDER' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER] union allselect ' SAP_ORDER_COST' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_COST] union allselect ' SAP_ORDER_TM_CONFIRM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_TM_CONFIRM] union allselect ' SAP_GOODS_MOVE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_GOODS_MOVE] union allselect ' SAP_NOTIFICATION' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_NOTIFICATION] union allselect ' SAP_ACTIVITY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ACTIVITY] union allselect ' SAP_CAUSE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_CAUSE] union allselect ' SAP_ITEM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ITEM] union allselect ' SAP_MEASU_DOCUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_MEASU_DOCUM] union allselect ' INVALID_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_DELAY] union allselect ' INVALID_PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_PROD_SUM] union allselect ' INVALID_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_WASTE] union allselect ' CRIT_ANLY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[CRIT_ANLY] union allselect ' WORK_HISTORY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HISTORY] union allselect ' WORK_HIST_DETL' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HIST_DETL] union allselect ' COST_METRICS' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[COST_METRICS] OPTION (MERGE UNION)) I2)select * from DataPurgeCountTable2 |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 07:55:51
|
| <<how can I use OPTION (MERGE UNION) at the end like>> try this.with cte(a,b) as(select '1','2' union allselect '1','3' union all select '1','2' union allselect '1','2' union allselect '1','2' union all select '1','2' )select * from cte where b=3OPTION (MERGE JOIN); |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 08:15:10
|
| excellant !!! it's working thanks haroon2k9 |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 08:17:08
|
quote: Originally posted by qutesanju excellant !!! it's working thanks haroon2k9
Welcome. |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 08:21:43
|
| also can I use Is exists in order to improve performance betterlike below;WITH DataPurgeCountTable2 (Table_Name, Table_Count) AS(select Table_Name, Table_Count from (if exists(select 'PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_SUM] )union allselect ' PRODUCTION_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[KC_PRODUCTION_DELAY] union allselect ' PROD_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[PROD_WASTE] union allselect ' SAP_ORDER' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER] union allselect ' SAP_ORDER_COST' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_COST] union allselect ' SAP_ORDER_TM_CONFIRM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ORDER_TM_CONFIRM] union allselect ' SAP_GOODS_MOVE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_GOODS_MOVE] union allselect ' SAP_NOTIFICATION' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_NOTIFICATION] union allselect ' SAP_ACTIVITY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ACTIVITY] union allselect ' SAP_CAUSE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_CAUSE] union allselect ' SAP_ITEM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_ITEM] union allselect ' SAP_MEASU_DOCUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[SAP_MEASU_DOCUM] union allselect ' INVALID_DELAY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_DELAY] union allselect ' INVALID_PROD_SUM' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_PROD_SUM] union allselect ' INVALID_WASTE' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[INVALID_WASTE] union allselect ' CRIT_ANLY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[CRIT_ANLY] union allselect ' WORK_HISTORY' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HISTORY] union allselect ' WORK_HIST_DETL' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[WORK_HIST_DETL] union allselect ' COST_METRICS' as 'Table_Name' ,count (*) as 'Table_Count' from dbo.[COST_METRICS] OPTION (MERGE UNION))I2) |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-03-22 : 08:34:36
|
| If you just want the number of rows in some specific tables (and only if those tables exist) wouldn't it be easier to query the system tables?(Unless this is SQL 2000 were the system tables were rarely accurate in this regard!) |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 08:57:39
|
| pls give an example for this |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-03-22 : 09:46:02
|
quote: Originally posted by Kristen(Unless this is SQL 2000 were the system tables were rarely accurate in this regard!)
You do know that CTE is not supported in SQL 2000 right ?  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-22 : 09:55:01
|
| i m using sql 2005 |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-03-22 : 10:18:02
|
I think KHTan's comment was aimed at me - he likes to point out how old I am whenever the chance arises |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-22 : 11:30:34
|
quote: Originally posted by Kristen I think KHTan's comment was aimed at me - he likes to point out how old I am whenever the chance arises 
But thats a fact isnt it? ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Next Page
|
|
|
|
|