循环浏览并显示表格

时间:2019-06-05 02:34:10

标签: mysql sql sql-server-2008

我试图向每个人展示他们自己的动物

它只显示了1个人,而动物是所有人的总数

创建proc AllAnimals     

--declare variables for your cursor     
declare @numCows int, @numSheeps int ,@numHorses int, @numDogs int,@category varchar(30), @fname varchar(30), @lname varchar(30), @numPig int, @numBirds int
declare @numCats int, @numFish int, @numInsect int, @numSnake int

print 'fName' + ' ' + 'lName' + ' ' + ' cow'+ ' '+ 'sheep '+ ' '+ ' horse'+ ' '+ ' dog'+ ' '+ 'pig'+ ' '+ ' bird'+ ' '+ ' cat'+ ' '+ ' fish'+ ' '+ ' insect'+ ' '+ ' snake'

--write the cursor
--declare the name of your cursor
declare vet_app cursor for

--create the statement and retrive the information that you want
select fName, lName,category  from staff s, scheduleSlot sc, appointment ap, animal a
where sc.staff_id = s.staff_id
and sc.appointment_id = ap.appointment_id
and ap.animal_id = a.animal_id


--open the cursor
open vet_app
--fetch the data from the cursor into the separate data
fetch vet_app into @fname, @lname, @category
set @numCows = 0
set @numDogs = 0
set @numHorses = 0
set @numSheeps = 0
set @numPig = 0
set @numBirds = 0
set @numCats = 0
set @numFish = 0
set @numInsect =0
set @numSnake = 0


--Create the while loop 
while @@FETCH_STATUS = 0   
    begin 
        if @category = 'Cow'    set @numCows = @numCows + 1
        if @category = 'sheep'  set @numSheeps = @numSheeps + 1
        if @category = 'Horse'  set @numHorses = @numHorses + 1
        if @category = 'Dog'    set @numDogs = @numDogs + 1
        if @category = 'Pig'    set @numPig = @numPig + 1
        if @category = 'Bird'   set @numBirds = @numBirds +1
        if @category = 'Cat'    set @numCats = @numCats + 1
        if @category = 'Fish'   set @numFish = @numFish + 1
        if @category = 'Insect' set @numInsect = @numInsect + 1
        if @category = 'Snake'  set @numSnake = @numSnake + 1


        --add the new set of data in the loop
        fetch vet_app into @fname, @lname, @category


    end  
print @fname + '  ' + @lname + '     ' + str(@numCows, 3) + '     ' + str(@numSheeps, 3) + '       ' + str(@numHorses, 3) + '       ' + str(@numDogs, 3) 
        + '       ' + str(@numPig, 3) + '       ' + str(@numBirds, 3) + '       ' + str(@numCats, 3) + '       ' + str(@numFish, 3) + '       ' + str(@numInsect, 3)
        + '       ' + str(@numSnake, 3)
deallocate vet_app

它应该显示

fname       lname          cow  sheep   horse   dog      pig    bird    cat     fish    insect  snake
  Mark      Apperley        0    5       2       5       1       2       2       0       1       0
  David     Bainbridge      0    0       0       0       0       0       0       0       0       0
  Sally Jo  Cunningham      2    4       1       0       3       1       0       0       0       1
  Tim       Elphick         4    1       2       1       0       2       0       0       0       0
  Frank     Frank           2    3       0       1       2       0       1       2       0       0
  Tomás     García Ferrari  0    0       0       0       0       0       0       0       0       0
  Annika        Hinze           0    0       0       0       0       0       0       0       0       0
  Nilesh        Kanji           3    3       0       0       0       0       1       0       1       0
  Te Taka   Keegan          0    0       0       0       0       0       0       0       0       0
  Ryan      Ko              1    3       0       2       1       3       1       0       2       1
  Vimal     Kumar           3    4       2       1       3       0       2       2       2       0
  Justin        Kurland         1    5       2       1       1       1       0       1       0       0
  Simon     Laing           0    0       0       0       0       0       0       0       0       0
  Matthew   Luckie          2    1       2       0       5       1       0       1       0       1
  Robi      Malik           0    2       1       0       1       1       0       0       0       0
  Michael   Mayo            8    6       6       4       4       0       1       2       1       0
  Richard   Nelson          1    0       2       1       2       1       0       1       0       0
  David     Nichols         0    4       2       1       1       0       2       3       1       3
  Bernhard  Pfahringer      3    6       3       0       4       1       0       2       2       1
  Bronwyn   Poki            3    2       2       0       1       0       0       0       0       0
  Steve     Reeves          0    0       0       0       0       0       0       0       0       0
  Tania     Robinson        0    0       0       0       0       0       0       0       0       0
  Bill      Rogers          2    4       4       2       1       1       3       2       1       1
  Tony      Smith           0    0       0       0       0       0       0       0       0       0
  Keith     Soo             3    1       1       1       0       1       0       0       0       2
  Claire        Timpany         4    2       1       4       1       2       1       0       2       1
  Phil      Treweek         5    1       0       4       1       0       1       2       0       1
  Emmanuel  Turner          5    2       2       0       1       1       0       0       0       0
  Nic       Vanderschantz   1    1       2       1       1       1       1       1       0       1
  Bronwyn   Webster         0    0       0       0       0       0       0       0       0       0
  Ian       Witten          1    2       1       1       0       2       1       0       0       0
  Shaoqun   Wu              0    0       0       0       0       0       0       0       0       0

0 个答案:

没有答案