SQL INSERT语句

时间:2019-02-20 18:04:26

标签: sql-server

第一段代码代表一条update语句,该语句将所有现有列的行更新为用户想要的任何内容。因此,然后我编写了一条select语句,该语句抓住了所有为null /不存在的列,并且它返回63行,而没有end where语句,但是带有它,然后返回62行,这是不存在的NULL。因此,现在我需要将选择所有NUll行的sql select转换为插入,而不是将数据放入那些不存在的记录中。到目前为止,我发布了我所拥有的。为什么这不起作用?就是说不存在schoolDay列,我该如何输入这些字段?

update e
set e.type = @dayEvent 
from odb.dayEvent e
inner join odb.day d on e.dayID = d.dayID
inner join MMSD.vSchoolFromCalendar c on d.calendarID = c.calendarID
                                      and d.structureID = c.structureID
inner join odb.PeriodSchedule p on d.periodScheduleID = p.periodScheduleID
                                and d.structureID = p.structureID
where d.[date] between @toDate and @fromDate
  and datepart(dw, d.[date]) not in (1,7)
  and e.[type] != @dayEvent
  and DistrictCode = 'MA'
  and FiscalYear = @FiscalYear
  and summerSchool = '0'
  and left(SchoolCode, 1) = @schoolChoice
  and e.[type] != @dayEvent

select 
    sts = 'success', 
    result = convert(varchar, @@rowcount) + ' rows updated.';

set @rowcount = (select @@rowcount);    

select 
    d.schoolDay,
    d.instruction,
    d.attendance, 
    d.duration,
    d.comments,
    d.startTime, d.endTime,
    e.type
from 
    odb.dayEvent e
right outer join
    odb.day d on e.dayID = d.dayID
inner join 
    MMSD.vSchoolFromCalendar c on d.calendarID = c.calendarID
                               and d.structureID = c.structureID
inner join 
    odb.PeriodSchedule p on d.periodScheduleID = p.periodScheduleID
                         and d.structureID = p.structureID
where 
    d.[date] between '2019-02-12' and '2019-02-12'
    and datepart(dw, d.[date]) not in (1, 7)
    and DistrictCode = 'MA' 
    and FiscalYear = '2019'
    and summerSchool = '0'
    and left(SchoolCode, 1) = '0'
    and e.dayEventID IS NULL

到目前为止的代码

INSERT INTO odb.dayEvent (schoolDay, instruction, attendance, duration, comments, startTime, endTime)
    SELECT 
        d.schoolDay, d.instruction, d.attendance, d.duration, d.comments, 
        d.startTime, d.endTime
    FROM 
        odb.dayEvent e
    RIGHT OUTER JOIN 
        odb.day d ON e.dayID = d.dayID
    INNER JOIN 
        MMSD.vSchoolFromCalendar c ON d.calendarID = c.calendarID
                                   AND d.structureID = c.structureID
    INNER JOIN 
        odb.PeriodSchedule p ON d.periodScheduleID  = p.periodScheduleID
                             AND d.structureID = p.structureID
    WHERE 
        d.[date] BETWEEN '2019-02-12' AND '2019-02-12'
        AND DATEPART(dw, d.[date]) NOT IN (1, 7)
        AND DistrictCode = 'MA' 
        AND FiscalYear = '2019'
        AND summerSchool = '0'
        AND LEFT(SchoolCode, 1) = '0'
        AND e.dayEventID IS NULL

1 个答案:

答案 0 :(得分:0)

这是我现在的答案,看来我的代码的另一和平已经在解决我的质询问题了。...

     insert into odb.DayEvent 
        (dayID, type, duration, instructionalMinutes)
     select
        e.dayID, e.type, e.duration, e.instructionalMinutes
    from odb.dayEvent e
           RIGHT OUTER JOIN odb.day d ON e.dayID = d.dayID
          INNER JOIN MMSD.vSchoolFromCalendar c ON d.calendarID = c.calendarID
                 and d.structureID = c.structureID
          INNER JOIN odb.PeriodSchedule p ON d.periodScheduleID  = p.periodScheduleID
                 and d.structureID = p.structureID
       WHERE d.[date] between '2019-02-12' and '2019-02-12'
          and datepart(dw,d.[date]) not in (1,7)
          and DistrictCode = 'MA'
          and FiscalYear = '2019'
          and summerSchool =  '0'
          and left(SchoolCode,1) = '0'
          and e.type IS NULL
     select sts='success', result= convert( varchar,@@rowcount) + ' rows updated.';
     set @rowcount = (select @@rowcount);