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 |
|
dwjongbloed
Starting Member
9 Posts |
Posted - 2009-10-29 : 11:19:02
|
| What I'm trying to do is create a table that has the tables with in a certain Database on my server. This table will contain the table names, active rows, and expired rows. The active and expired rows will be done by performing a select count(*) where xxxx = ' '. I have a cursor started to do this but an running into problems. The code I have looks like this:truncate table damon_work.dbo.DW_Table_Rowcount_statsdeclare @table_name varchar(100)declare @exp_rows numeric(18,0)declare @active_rows numeric(18,0)declare objects CURSORfor select name from Dw_data_warehouse.dbo.sysobjectswhere Dw_data_warehouse.dbo.sysobjects.xtype = 'U'order by dw_data_warehouse.dbo.sysobjects.nameopen objectsFETCH NEXT From objects into @table_nameWHILE (@@FETCH_STATUS=0) BEGINselect count(*) as @exp_rowsfrom dw_data_warehouse.dbo.@tablenamewhere DW_Eff_End_Dt = '12/31/3030'select count(*) as @active_rowsfrom dw_data_warehouse.dbo.@tablenamewhere DW_Eff_End_Dt <> '12/31/3030'beginupdate damon_work.dbo.DW_Table_Rowcount_statsset table_name = @table_name,expired_rows = @exp_rows,Active_rows = @active_rowsendFETCH NEXT From objects into @table_nameendclose objectsdeallocate objectsThe problem I'm having is with the select statements within the OPEN/ Fetch area. I get the following error msg's:Msg 170, Level 15, State 1, Line 30Line 30: Incorrect syntax near '@exp_rows'.Msg 170, Level 15, State 1, Line 34Line 34: Incorrect syntax near '@active_rows'. |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-10-29 : 11:49:33
|
| you can't do as Select COUNT(*) AS @coffeebreaktimedo select @exp_rows = count(*)from dw_data_warehouse.dbo.@tablenamewhere DW_Eff_End_Dt = '12/31/3030'select @active_rows = count(*) from dw_data_warehouse.dbo.@tablenamewhere DW_Eff_End_Dt <> '12/31/3030'<><><><><><><><><><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-10-29 : 11:51:50
|
| Cursors are slow, i hate!try using tables variable or temp tables to do batch process.the problem in ur case is select count(*) as @exp_rowsfrom dw_data_warehouse.dbo.@tablenamewhere DW_Eff_End_Dt = '12/31/3030'select count(*) as @active_rowsfrom dw_data_warehouse.dbo.@tablenamewhere DW_Eff_End_Dt <> '12/31/3030'trying using select @active_rows = count(*) .....========================================Project Manager who loves to code.===============Ask to your self before u ask someone |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2009-10-29 : 11:53:19
|
You also can't update a table if there are no rows in it (truncate table with no insert afterwards).Here's a snippet that should do the trick:truncate table damon_work.dbo.DW_Table_Rowcount_statsexec dw_data_warehouse.dbo.sp_msforeachtable 'insert damon_work.dbo.DW_Table_Rowcount_stats(table_name, expired_rows, Active_rows)select parsename(''?'',1), sum(case when a.DW_Eff_End_Dt=''12/31/3030'' then 1 end) expired, sum(case when a.DW_Eff_End_Dt<>''12/31/3030'' then 1 end) activefrom dw_data_warehouse.? as a'Warning: not tested on actual data. If it works first time, you owe me a beer. |
 |
|
|
dwjongbloed
Starting Member
9 Posts |
Posted - 2009-10-29 : 14:21:44
|
| Thanks robvolk - worked like a charm |
 |
|
|
|
|
|
|
|