The Table Variable in SQL Server


30/03/2021- duocnt    943 Views    

NỘI DUNG

  1. Khai báo biến kiểu table.
  2. Insert dữ liệu vào biến table.
  3. Kiểm tra dữ liệu trong biến table.


THỰC HIỆN.

1 - Khai báo biến kiểu table.

       DECLARE @tReturn table
       (
             NCRDate date,
             NCRWeek int,
             NCRProblem nvarchar(100),
             DefectQty int,
             NCRType nvarchar(50),
             NCRTypeName nvarchar(100),
             ErrorOf nvarchar(50),
             ErrorOfFactory int,
             ErrorOfSupplier int
       )

2 - Insert dữ liệu vào biến table.

       Insert into @tReturn
       select
       a.NONCONFORMANCEDATE,
       DATEPART(WK,a.NONCONFORMANCEDATE) as 'Week',
       a.INVENTTESTPROBLEMTYPEID,
       a.TESTDEFECTQTY,
       a.INVENTNONCONFORMANCETYPE,
       case a.INVENTNONCONFORMANCETYPE 
             when 2 then 'Vendor'
             when 4 then 'Production'
       end,
       a.ERROROF,
       Case when a.ERROROF !='Supplier' then a.TESTDEFECTQTY else 0 end,
       Case when a.ERROROF ='Supplier' then a.TESTDEFECTQTY else 0 end
       from dbo.RSVNInventNonConformanceTableStaging a
       where YEAR(a.NONCONFORMANCEDATE) >= 2020
       and a.INVENTNONCONFORMANCETYPE in (2,4)
       order by Week

3 - Kiểm tra dữ liệu trong biến table.

    select * from @tReturn



Góp ý kiến

;
;