今天写的一个存储过程

  1. begin 
  2.     
  3.     create table #table
  4.     (
  5.         theID int identity(1,1),
  6.         PriceName varchar(1000),
  7.         theDate datetime,
  8.         Prices varchar(1000),
  9.         theCTIDs varchar(500)
  10.     )
  11.     
  12.     declare  @MyDate datetime
  13.     DECLARE mycursor CURSOR FOR--创建一个游标
  14.     select distinct TicketDate from  ConceTable where TicketID=5
  15.         OPEN mycursor
  16.         FETCH NEXT FROM mycursor
  17.         INTO @MyDate
  18.         WHILE @@FETCH_STATUS = 0
  19.         BEGIN
  20.     
  21.         declare @Myid1 int,@thePriceNames varchar(1000),@thePrices varchar(5000),@theCTID  varchar(2000)
  22.         set @thePriceNames=''
  23.         set @thePrices=''
  24.         set @theCTID=''
  25.         DECLARE mycursor1 CURSOR FOR--创建一个游标
  26.             select CTID  from dbo.ConceTable where TicketDate =@MyDate order by CTID
  27.             OPEN mycursor1
  28.             FETCH NEXT FROM mycursor1
  29.             INTO @Myid1
  30.             WHILE @@FETCH_STATUS = 0
  31.             BEGIN
  32.             
  33.             declare @theConcessions varchar(100),@thePrice varchar(1000)
  34.             select @theConcessions=Concessions from ConceTable where CTID =@Myid1
  35.             set @thePriceNames = @thePriceNames +'|'+ @theConcessions 
  36.             --set @thePriceNames=@theConcessions
  37.             select @thePrice=[TicketPrice] from ConceTable where CTID =@Myid1
  38.                 --set @thePrices = @thePrices+'|'+@thePrice
  39.             set @theCTID=@theCTID+'|'+CONVERT(varchar(1000),@Myid1)
  40.             set @thePrices= @thePrices+'|'+@thePrice
  41.                 --select @Myid1,@thePriceNames,@thePrices,@theConcessions,@thePrice
  42.                 
  43.            FETCH NEXT FROM mycursor1
  44.            INTO @Myid1
  45.            END
  46.            CLOSE mycursor1
  47.            DEALLOCATE mycursor1
  48.            insert into #table values(@thePriceNames,@MyDate,@thePrices,@theCTID)
  49.             
  50.        
  51.        FETCH NEXT FROM mycursor
  52.        INTO @MyDate
  53.        END
  54.        CLOSE mycursor
  55.        DEALLOCATE mycursor
  56.     select * from #table 
  57.     drop table #table
  58.     
  59. end 
原文地址:https://www.cnblogs.com/dingdingmao/p/3146580.html