Слияние SQL не вставлять дату в таблицу - PullRequest
0 голосов
/ 16 апреля 2020

Я создаю таблицу:

/** Script for SelectTopNRows command from SSMS  **/
SELECT TOP (1000) [StudentIndex]
      ,[Name]
      ,[Surname]
      ,[University Email]
      ,[Private Email]
      ,[Birth date]
      ,[Phone]
  FROM [StudentsScraperDB].[dbo].[Students]
Tabela STUdent
CREATE TABLE [dbo].[Students]
(
    [StudentIndex] INT NOT NULL PRIMARY KEY,
    [Name] VARCHAR(20) NOT NULL,
    Surname VARCHAR(50) NOT NULL,
    [University Email] VARCHAR(100) NOT NULL,
    [Private Email] VARCHAR(100) NULL,
    [Birth date] varchar(10) NOT NULL,
    Phone VARCHAR(9) NULL 
)

И я создаю процедуру для хранения данных в этой таблице:

CREATE PROCEDURE [ImportData]

as

declare @dane as table
(
    StudentIndex int,
    [Name] varchar(20),
    Surname varchar(50),
    Degree varchar(50),
    [University Email] varchar(100),
    [Private Email] varchar(100),
    [Birth date] varchar(10),
    Phone varchar(9),
    IdUniversity int,
    IdDepartment int,
    IdDirection int,
    YearOfUniversityEntrance varchar(10)
)

insert into @dane (StudentIndex, [Name], Surname, Degree, [University Email], [Private Email], [Birth date], Phone, IdUniversity,IdDepartment,IdDirection, YearOfUniversityEntrance)
select cast(StudentIndex as int), [Name], Surname, Degree, EmailUniversity, PersonalEmail, DateOfBirth,
phone, cast(IdUniversity as int), cast(IdDepartment as int), cast(IdDirection as int), YearOfUniversityEntrance from ImportTable

merge Students as t
using (SELECT StudentIndex, [Name], Surname, [University Email], [Private Email], [Birth date], Phone FROM @dane) as s 
on t.StudentIndex = s.StudentIndex and t.[Name] = s.[Name] and t.Surname = s.Surname and
t.[University Email] = s.[University Email] and t.[Private Email] = s.[Private Email] and
t.[Birth date] = s.[Birth date] and t.Phone = s.Phone
WHEN NOT MATCHED THEN 
INSERT VALUES (s.StudentIndex, s.[Name], s.Surname, s.[University Email], s.[Private Email], s.[Birth date], s.Phone);

merge StudentsDirections as sd
using @dane as s
on sd.StudentIndex = s.StudentIndex and sd.IdDirection=s.IdDirection and sd.Degree=s.Degree and sd.YearOfUniversityEntrance=s.YearOfUniversityEntrance 
WHEN NOT MATCHED THEN
INSERT (StudentIndex,IdDirection,Degree,YearOfUniversityEntrance) VALUES (s.StudentIndex,s.IdDirection,s.Degree,s.YearOfUniversityEntrance);

merge Directions as d
using @dane as s
on d.IdDirection=s.IdDirection and d.IdDepartment=s.IdDepartment and d.DegreeOfStudy=s.Degree
WHEN NOT MATCHED THEN
INSERT ( IdDirection, IdDepartment, DegreeOfStudy) VALUES (s.IdDirection,s.IdDepartment,s.Degree);

merge Departments as Da
using @dane as s
on Da.IdDepartment=s.IdDepartment and Da.IdUniversity=s.IdUniversity
WHEN NOT MATCHED THEN
INSERT ( IdDepartment, IdUniversity) VALUES (s.IdDepartment,s.IdUniversity);

merge Universites as u
using @dane as s
on u.IdUniversity=s.IdUniversity
WHEN NOT MATCHED THEN
INSERT (IdUniversity) VALUES (s.IdUniversity);

Проблема заключается в слиянии студентов. Когда в процедуре есть данные и таблица Студенты пуста, объединение не вставляет данные. Почему я не работал? Другое начало слияния. И я не знаю почему, но в Managment Studio эта таблица sql подчеркнута красным цветом

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...