1

Хочу создать триггер который будет автоматически подвязывать поле Id, из таблицы users, с UserID из таблицы profiles

Вот код на T-SQL :

    create table users (
   Id int not null primary key identity,
   Username varchar(30) not null unique,
   Email varchar(30) not null unique,
);
create table profiles (
    Id int primary key identity,
    UserId int not null references users(Id) ,
    Name varchar(30) not null,
);
go
create trigger new_profile on users after insert as
 for each row 
 begin
  insert into profiles(UserId) values (new.Id);
  end

Главная ошибка возле for each row , где при создании триггера получаю такую ошибку :

Incorrect syntax near the keyword 'for'.

Не могли бы вы помочь с решением проблемы , спасибо!

1 ответ 1

2

Не надо пытаться использовать Oracle диалект в SQL Server.

Попробуйте так:

create trigger new_profile
on users
after insert
as
  insert into profiles(UserId) select Id from inserted;
GO
2
  • как раз уже в писал в ответе о оракле и хотел навести пример - сделал подобный код ,действительно помогло,спасибо!
    – clyde
    10 сен 2017 в 11:50
  • @MaxNavrotsky, пожалуйста :) 10 сен 2017 в 11:52

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.