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
|