从Visual Studio SQL Server

时间:2018-04-14 23:51:40

标签: sql sql-server

我正在尝试使用序列中的下一个值插入表中。这导致Visual Studio的LocalDB出错,但在SQL Developer中有效。

insert into department (id_department, name, abbreviation, dept_head)
values (id_department_seq.nextval, 'School of Communications, Media, Arts and Design', department_abbreviation_seq.nextval, 'Nate Horowitz');

获取此错误

  

Msg 4104,Level 16,State 1,Line 63
  多部分标识符" id_department_seq.nextval"无法受约束。

     

Msg 4104,Level 16,State 1,Line 63
  多部分标识符" department_abbreviation_seq.nextval"无法绑定

2 个答案:

答案 0 :(得分:1)

获取序列的下一个值的语法对我来说很奇怪。我熟悉以下内容:

insert into department (id_department, name, abbreviation, dept_head)
    values (
        NEXT VALUE FOR id_department_seq, 
        'School of Communications, Media, Arts and Design', 
        NEXT VALUE FOR department_abbreviation_seq, 
        'Nate Horowitz'
    );

答案 1 :(得分:0)

将此句放在insert语句之前,以确保您创建了序列。

if not exists(select * from sys.sequences where name ='id_department_seq') then
 begin
  create sequence id_department_seq start with 1000 increment by 1 minvalue 1000 maxvalue 99999;
end

insert into ....