Question : How do I get my cursor value to a variable....

I am trying to assign the cursor value to a variable so I can do an insert using it. The problem I have as soon as I try to do fetch into @variable name i break the loop and it gives me the last one in my list.  Below is the code:

Any help on this would be greatly appreciated.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
ALTER PROCEDURE [dbo].[sp_update_daily_totals]
	
	@Date datetime
	
AS
BEGIN
	DECLARE @storeid nvarchar(MAX), @cashierid nvarchar(max)
	SET NOCOUNT ON;
 
    DECLARE TBLC_StoreInfo CURSOR
	FOR ( SELECT storeid FROM [CustomerMain].[dbo].[StoreInfo] WHERE active = 1 AND cidx = 1)
	
	OPEN TBLC_StoreInfo
	FETCH NEXT FROM TBLC_StoreInfo into @storeid
	WHILE @@FETCH_STATUS = 0
	
	BEGIN
	Select @storeid
			declare @Var nvarchar(4000)
			set @Var = 'SELECT Distinct(cashier) from [dbo].[' + @storeid + '] where cashier !='''''
			declare @get_data3  CURSOR
			
			set @Var = 'set @incursor = CURSOR FOR '+@Var+' OPEN @incursor'
			
			execute sp_executesql @Var,N'@incursor CURSOR OUT',@incursor=@get_data3 OUT
					
						
			WHILE @@FETCH_STATUS = 0
			
-- if i do it like this it breaks and only gives me the last @cashierid from the selection
 
			fetch next from @get_data3 into @cashierid
 
--if i do it like this it will output all of the @cashierid's to the output window when testing the stored proc..
 
			fetch next from @get_data3 
 
 
			select @cashierid
			close @get_data3
			deallocate @get_data3
	
	
	FETCH NEXT FROM TBLC_StoreInfo into @storeid
			
	END
			CLOSE TBLC_StoreInfo
			DEALLOCATE TBLC_StoreInfo
			
			
			
END
Open in New Window Select All

Answer : How do I get my cursor value to a variable....

apologies chrobi, that previous code was really very ordinary and bug ridden. Have tried it out and fixed accordingly...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
ALTER PROCEDURE [dbo].[sp_update_daily_totals] (@Date datetime)
AS
BEGIN
 
    DECLARE @storeid nvarchar(MAX), 
            @cashier nvarchar(max),
            @Var nvarchar(4000),
            @id int
            
    SET NOCOUNT ON;
 
    DECLARE TBLC_StoreInfo CURSOR READ_ONLY FOR  SELECT storeid 
                                                 FROM [CustomerMain].[dbo].[StoreInfo] 
                                                 WHERE active = 1 AND cidx = 1
    
    OPEN TBLC_StoreInfo
    FETCH NEXT FROM TBLC_StoreInfo into @storeid
    WHILE @@FETCH_STATUS = 0
    
    BEGIN
        print @storeid
 
        IF OBJECT_ID('tempdb.dbo.##tmp_store_cashiers', 'U') IS NOT NULL DROP TABLE ##tmp_store_cashiers;
        set @Var = 'SELECT identity(int,1,1) as ID, ''' + @storeid + ''' as storeid, cashier into ##tmp_store_cashiers from [' + @storeid + '] where cashier !='''' group by cashier'
        exec(@Var)
                    
-- now we have a table of non-empty storeid's and cashiers in ##temp
-- should be able to use that in joins and such like to avoid a loop if possible...
        set @id = 0
 
        while @id < (select top 1 id from ##tmp_store_cashiers where storeid = @storeid order by id desc)
        begin
 
-- do something with a cashier
             select top 1 @id = id, @cashier = cashier from ##tmp_store_cashiers where storeid = @storeid and id > isnull(@id,0) order by id asc
 
             print convert(varchar,@id) + ' ' + @cashier + ' ' + @storeid
                         
        end    
    
        FETCH NEXT FROM TBLC_StoreInfo into @storeid
            
    END
    CLOSE TBLC_StoreInfo
    DEALLOCATE TBLC_StoreInfo
            
END
Open in New Window Select All
Random Solutions  
 
programming4us programming4us