Author |
Topic |
Ali.M.Habib
Yak Posting Veteran
54 Posts |
Posted - 2009-01-29 : 02:16:46
|
the code not display errors except when running which are :Line 1: Incorrect syntax near '~'. while the code not have any ~ in itCould not complete cursor operation because the table schema changed after the cursor was declared.the code is :set nocount oncreate table #spaceused ( name nvarchar(120), rows char(11), reserved varchar(18), data varchar(18), index_size varchar(18), unused varchar(18))declare Tables cursor for select name from sysobjects where type='U' --> stands for user table order by name ascOPEN TablesDECLARE @table varchar(128)FETCH NEXT FROM Tables INTO @tableWHILE @@FETCH_STATUS = 0BEGIN insert into #spaceused exec sp_spaceused @table FETCH NEXT FROM Tables INTO @tableENDCLOSE TablesDEALLOCATE Tables select * from #spaceused--drop table #spaceused declare RENAMETABLE cursor for select [name] from #spaceused where rows=0 open RENAMETABLE DECLARE @@RTABLE varchar(128) DECLARE @@RTABLEnew varchar(128)FETCH NEXT FROM RENAMETABLE INTO @@RTABLEWHILE @@FETCH_STATUS = 0BEGINcreate table #DISTINCTC( [name] nvarchar(120))insert into #DISTINCTC SELECT Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = @@RTABLE--------------------------select * from #DISTINCTCdeclare Dist cursor forselect [name]from #DISTINCTC open Dist DECLARE @z int declare @y nvarchar(120) declare @SQLString nvarchar(4000) fetch next from Dist into @y while @@fetch_status=0 begin SET @SQLString = N'select @n=count(distinct('+@y+N')) from '+@@RTABLE+'' EXEC sp_executesql @SQLString, N'@n int OUTPUT', @z OUTPUT if ((select @z)<= 10) begin create table #DISTINCTC1( DistinctVal varbinary(4000)) insert into #DISTINCTC1 EXEC('select distinct '+@y+' from '+@@RTABLE+'') select * from #DISTINCTC1 --declare Insertion cursor for select * from #DISTINCTC1 --open Insertion --Declare @I varbinary(4000) --FETCH NEXT FROM Insertion INTO @I --WHILE @@FETCH_STATUS = 0 --begin insert into SATIS select @@RTABLE,@y,DistinctVal from #DISTINCTC1 --FETCH NEXT FROM Insertion INTO @I --end --close Insertion --Deallocate Insertion end drop table #DISTINCTC1 fetch next from Dist into @y endclose Dist Deallocate Dist drop table #DISTINCTCdrop table #spaceused FETCH NEXT FROM RENAMETABLE INTO @@RTABLEENDCLOSE RENAMETABLEDEALLOCATE RENAMETABLEselect * from SATIS--delete SATIS any suggestion please |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-29 : 02:44:05
|
Which line produces the error?Is it this line = "insert into #spaceused exec sp_spaceused @table" ? E 12°55'05.63"N 56°04'39.26" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-29 : 02:45:32
|
Also, why are you creating the #distinct temporary table for each iteration? E 12°55'05.63"N 56°04'39.26" |
|
|
Ali.M.Habib
Yak Posting Veteran
54 Posts |
Posted - 2009-01-29 : 02:46:10
|
quote: Originally posted by Peso Which line produces the error?Is it this line = "insert into #spaceused exec sp_spaceused @table" ? E 12°55'05.63"N 56°04'39.26"
I don't know which line because it's a runtime errors appear after running the query |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-29 : 02:49:17
|
I know it does.When reading the red error text after execution and you read the error message, there is also a line number in the beginning of row indicating which line is producing the error. E 12°55'05.63"N 56°04'39.26" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-29 : 02:51:06
|
Msg 16915, Level 16, State 1, Line 53A cursor with the name 'Dist' already exists.Msg 16905, Level 16, State 1, Line 55The cursor is already open.Msg 16943, Level 16, State 4, Line 59Could not complete cursor operation because the table schema changed after the cursor was declared.Msg 16943, Level 16, State 4, Line 91Could not complete cursor operation because the table schema changed after the cursor was declared.Msg 3701, Level 11, State 5, Line 98Cannot drop the table '#spaceused', because it does not exist or you do not have permission.Msg 3701, Level 11, State 5, Line 99Cannot drop the table '#renametable', because it does not exist or you do not have permission.Msg 3701, Level 11, State 5, Line 100Cannot drop the table '#distinctc', because it does not exist or you do not have permission. E 12°55'05.63"N 56°04'39.26" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-29 : 03:02:48
|
Also, first you use sp_spaceused to get all tables in current database.Then you create a cursor for those tables having rowcount equal to zero.For those records you count the distinct values.Which values? There are no records in the table! E 12°55'05.63"N 56°04'39.26" |
|
|
Ali.M.Habib
Yak Posting Veteran
54 Posts |
Posted - 2009-01-29 : 03:48:21
|
quote: Originally posted by Peso Also, first you use sp_spaceused to get all tables in current database.Then you create a cursor for those tables having rowcount equal to zero.For those records you count the distinct values.Which values? There are no records in the table! E 12°55'05.63"N 56°04'39.26"
thanx for the attention I solve d the problem |
|
|
tosscrosby
Aged Yak Warrior
676 Posts |
Posted - 2009-01-29 : 14:51:30
|
quote: Originally posted by Ali.M.Habibthanx for the attention I solve d the problem
Care to post the code to show others how you resolved it?? It could help out another poster at some point.Terry-- Procrastinate now! |
|
|
|