日期:2014-05-17 浏览次数:20599 次
---------------临时表##tb --CREATE TABLE ##t (stockcode int) --drop table ##t DECLARE @CurrStockCode VARCHAR(50) SET @CurrStockCode = '' DECLARE @StockCode VARCHAR(50) DECLARE stock_cursor CURSOR FOR SELECT STOCKCODE from ( -----查询数据 select * from stockinfo ) as tbyyb OPEN stock_cursor FETCH NEXT FROM stock_cursor INTO @StockCode WHILE @@FETCH_STATUS=0 --上一个fetch是否读取到了数据 BEGIN IF @CurrStockCode <> @StockCode BEGIN IF LEN(@CurrStockCode) > 0 BEGIN --PRINT 'code' + @CurrStockCode insert into ##t select @CurrStockCode END SET @CurrStockCode = @StockCode END FETCH next FROM stock_cursor INTO @StockCode END CLOSE stock_cursor DEALLOCATE stock_cursor ------------ 表和数据脚本 create table stockinfo ( stockcode int ) insert into stockinfo values(1) insert into stockinfo values(2) insert into stockinfo values(3) insert into stockinfo values(4) insert into stockinfo values(5)
insert into ##t select @CurrStockCode 改成 insert into ##t select @StockCode