| 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 tableset nocount on create table #DISTINCTC(  [name] nvarchar(120))insert into #DISTINCTC SELECT Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'AGEN'--------------------------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 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	endclose Dist Deallocate Dist drop table #DISTINCTC1drop 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 querySET @SQLString = N'select @z=count(distinct('+@y+N')) from AGEN'  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     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 querySET @SQLString = N'select @z=count(distinct('+@y+N')) from AGEN'
  it worked but after the first insertion it give this errorA cursor with the name 'Insertions' does not exist. what shall I do please  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     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?  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     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 querySET @SQLString = N'select @z=count(distinct('+@y+N')) from AGEN'
  it worked but after the first insertion it give this errorA 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  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     visakh16 
                                    Very Important crosS Applying yaK Herder 
                                     
                                    
                                    52326 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2009-01-28 : 09:45:31
                                          
  | 
                                         
                                        
                                          | cheers  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     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  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     visakh16 
                                    Very Important crosS Applying yaK Herder 
                                     
                                    
                                    52326 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2009-01-28 : 09:48:44
                                          
  | 
                                         
                                        
                                          | raise what error?  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     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 1The 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  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     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 beexec('insert into SATIS SELECT ''AGEN'','+@y+','+@I+' FROM Yourtable')  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 |