kết quả như sau:
code trong sql như sau:
USE [ThiDu] GO /****** Object: StoredProcedure [dbo].[sp_HangCot] Script Date: 11/17/2015 07:16:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[sp_HangCot] --fetch next in sql server --https://msdn.microsoft.com/en-us/library/ms180152.aspx AS BEGIN DECLARE @toan int DECLARE @ly int DECLARE @hoa int CREATE TABLE [dbo].[#BangDiem] ( [masv] nvarchar(50) , [toan] int NOT NULL, [ly] int NOT NULL, [hoa] int NOT NULL ) DECLARE @MaSinhVien nvarchar(50) DECLARE LopmonIDlst CURSOR FOR SELECT DISTINCT masv FROM baithi OPEN LopmonIDlst /**/ FETCH NEXT FROM LopmonIDlst INTO @MaSinhVien WHILE @@FETCH_STATUS = 0 BEGIN SET @toan = (SELECT diem FROM baithi WHERE mon = 'toan' AND masv =@MaSinhVien) SET @ly = (SELECT diem FROM baithi WHERE mon = 'ly' AND masv =@MaSinhVien) SET @hoa = (SELECT diem FROM baithi WHERE mon = 'hoa' AND masv =@MaSinhVien) BEGIN INSERT INTO [#BangDiem] (masv,toan,ly,hoa) VALUES (@MaSinhVien,@toan,@ly,@hoa) END FETCH NEXT FROM LopmonIDlst INTO @MaSinhVien; END CLOSE LopmonIDlst DEALLOCATE LopmonIDlst; SELECT * FROM [#BangDiem] END /* exec [dbo].[sp_HangCot] */chúc các bạn thành công