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 2000 Forums
 SQL Server Development (2000)
 two errors not have meaning

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 it
Could not complete cursor operation because the table schema changed after the cursor was declared.


the code is :
set nocount on
create 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 asc

OPEN Tables
DECLARE @table varchar(128)

FETCH NEXT FROM Tables INTO @table

WHILE @@FETCH_STATUS = 0
BEGIN
insert into #spaceused exec sp_spaceused @table
FETCH NEXT FROM Tables INTO @table
END

CLOSE Tables
DEALLOCATE 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 @@RTABLE

WHILE @@FETCH_STATUS = 0
BEGIN



create table #DISTINCTC(
[name] nvarchar(120)
)
insert into #DISTINCTC SELECT Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = @@RTABLE
------------------------
--select * from #DISTINCTC

declare Dist cursor for
select [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
end
close Dist
Deallocate Dist
drop table #DISTINCTC
drop table #spaceused

FETCH NEXT FROM RENAMETABLE INTO @@RTABLE
END

CLOSE RENAMETABLE
DEALLOCATE RENAMETABLE






select * 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"
Go to Top of Page

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"
Go to Top of Page

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
Go to Top of Page

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"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-29 : 02:51:06
Msg 16915, Level 16, State 1, Line 53
A cursor with the name 'Dist' already exists.
Msg 16905, Level 16, State 1, Line 55
The cursor is already open.
Msg 16943, Level 16, State 4, Line 59
Could not complete cursor operation because the table schema changed after the cursor was declared.
Msg 16943, Level 16, State 4, Line 91
Could not complete cursor operation because the table schema changed after the cursor was declared.
Msg 3701, Level 11, State 5, Line 98
Cannot drop the table '#spaceused', because it does not exist or you do not have permission.
Msg 3701, Level 11, State 5, Line 99
Cannot drop the table '#renametable', because it does not exist or you do not have permission.
Msg 3701, Level 11, State 5, Line 100
Cannot 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"
Go to Top of Page

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"
Go to Top of Page

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
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2009-01-29 : 14:51:30
quote:
Originally posted by Ali.M.Habib

thanx 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!
Go to Top of Page
   

- Advertisement -