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)
 why not insert

Author  Topic 

Ali.M.Habib
Yak Posting Veteran

54 Posts

Posted - 2009-01-28 : 09:05:44
the code run without erro but not insert any data in SATIS table
set nocount on 
create table #DISTINCTC(
[name] nvarchar(120)
)
insert into #DISTINCTC SELECT Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'AGEN'
------------------------
--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 count(distinct('+@y+N')) from AGEN'
EXEC sp_executesql @SQLString, N'@n int OUTPUT', @z OUTPUT
if ((select @z)<= 10)
begin
create table #DISTINCTC1( DistinctVal nvarchar(4000))
insert into #DISTINCTC1 EXEC('select distinct '+@y+' from AGEN')
select * from #DISTINCTC1
declare Insertion cursor for select * from #DISTINCTC1
open Insertion
Declare @I nvarchar(4000)
FETCH NEXT FROM Insertion INTO @I
WHILE @@FETCH_STATUS = 0
begin
insert into SATIS values('AGEN',@y,@I)
FETCH NEXT FROM Insertion INTO @I
end
close Insertion
Deallocate Insertions
end
fetch next from Dist into @y
end
close Dist
Deallocate Dist

drop table #DISTINCTC1
drop table #DISTINCTC

select * from SATIS


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 09:09:32
where are you grabbing value of @z? you've just declared it in sp_executesql but havent used it at all in query so it will still be NULL and hence block under if wont get executed at all.
probably, you meant below query

SET @SQLString = N'select @z=count(distinct('+@y+N')) from AGEN'
Go to Top of Page

Ali.M.Habib
Yak Posting Veteran

54 Posts

Posted - 2009-01-28 : 09:24:14
quote:
Originally posted by visakh16

where are you grabbing value of @z? you've just declared it in sp_executesql but havent used it at all in query so it will still be NULL and hence block under if wont get executed at all.
probably, you meant below query

SET @SQLString = N'select @z=count(distinct('+@y+N')) from AGEN'



it worked but after the first insertion it give this error
A cursor with the name 'Insertions' does not exist. what shall I do please
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 09:35:37
can you explain what you're trying to do here?
Go to Top of Page

Ali.M.Habib
Yak Posting Veteran

54 Posts

Posted - 2009-01-28 : 09:36:01
quote:
Originally posted by Ali.M.Habib

quote:
Originally posted by visakh16

where are you grabbing value of @z? you've just declared it in sp_executesql but havent used it at all in query so it will still be NULL and hence block under if wont get executed at all.
probably, you meant below query

SET @SQLString = N'select @z=count(distinct('+@y+N')) from AGEN'



it worked but after the first insertion it give this error
A cursor with the name 'Insertions' does not exist. what shall I do please


thanx a lot ur so helpful I solved it and now work very well
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 09:45:31
cheers
Go to Top of Page

Ali.M.Habib
Yak Posting Veteran

54 Posts

Posted - 2009-01-28 : 09:47:20
quote:
Originally posted by visakh16

can you explain what you're trying to do here?


it's a smal question yes it's work but when modifying by
exec('insert into SATIS values(''AGEN'','+@y+','+@I+')')

it raise an error do u have any suggestion for that
agin thanx u taughtme alot of things today
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 09:48:44
raise what error?
Go to Top of Page

Ali.M.Habib
Yak Posting Veteran

54 Posts

Posted - 2009-01-28 : 09:54:37
quote:
Originally posted by visakh16

raise what error?


Server: Msg 128, Level 15, State 1, Line 1
The name 'ACCT_NA_BYTE_4' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.

'ACCT_NA_BYTE_4' is a column name
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 09:57:34
so you want to insert values from column. then statement should be

exec('insert into SATIS SELECT ''AGEN'','+@y+','+@I+' FROM Yourtable')

Go to Top of Page
   

- Advertisement -