Heidisql: AUTO_INCREMENT does not work on MSSQL server

Created on 30 Oct 2018  ·  5Comments  ·  Source: HeidiSQL/HeidiSQL

Steps to reproduce this issue

  1. Create a table on an SQL server;
  2. Add an ID column;
  3. Set the ID column to be auto incrementing by setting default to AUTO_INCREMENT;
  4. Save the table. It may result in creating or updating the table. This fails.

Current behavior

It does not create or modify the table.
It gives an error on the AUTO_INCREMENT part of the statement.

Expected behavior

It should create or update the table.
It should not give an error.

Possible solution

Instead of using AUTO_INCREMENT, SQL server requires IDENTITY(1,1).
The first number between brackets is the starting value, the second is the increment.
Example:
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
Name varchar(255) NOT NULL
);

Environment

  • HeidiSQL version:
    9.5.0.5315, latest nightly build. Actually, I don't actually want to use a nightly build, but a stable build.
  • Database system and version:
    SQL server, all versions.
  • Operating system:
    Windows 10 on client, unknown on server.

All 5 comments

Is this a big change? Would it be easy for a first PR?

I got the same problem.
Is there a solution to this problem?

Oh God! It's 2020 and this is still open. Please guyz, fix this ASAP.
FYI: I am using HeidiSql v10.3

Also running into this today. One of the only reasons I have to open SSMS :(

Here is a script to generate a table on MS SQL 12.0.5223.6 that illustrates the problem:

CREATE TABLE [dbo].[User](
    [Username] [nvarchar](20) NOT NULL,
    [FullName] [nvarchar](50) NOT NULL,
    [EmailAddress] [nvarchar](100) NULL,
    [LastLoginDate] [datetime] NULL,
    [IsAdministrator] [bit] NULL,
    [Roles] [int] NOT NULL,
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CreatedAt] [datetime2](7) NOT NULL,
    [CreatedBy] [nvarchar](20) NOT NULL,
    [ModifiedAt] [datetime2](7) NULL,
    [ModifiedBy] [nvarchar](20) NULL,
    [LastPortfolioId] [int] NULL,
 CONSTRAINT [PK__User__536C85E5B3CDBFE3] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[User] ADD  DEFAULT (getdate()) FOR [CreatedAt]
GO

ALTER TABLE [dbo].[User] ADD  DEFAULT (suser_sname()) FOR [CreatedBy]
GO

HeidiSQL 10.3.0.5838 does not detect the primary key:
image

Here is a script that selects primary key indices on MS SQL (probably not very early versions of MS SQL):

select schema_name(tab.schema_id) as [schema_name], 
    tab.[name] as table_name, 
    pk.[name] as pk_name,
    col.name as col_name
from 
  sys.tables tab
  left outer join sys.indexes pk on tab.object_id = pk.object_id and pk.is_primary_key = 1
  left join sys.index_columns ic on ic.index_id = pk.index_id and ic.object_id = tab.object_id
  inner join sys.columns col on ic.object_id = col.object_id and ic.column_id = col.column_id
order by schema_name(tab.schema_id),
    tab.[name]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cammudito picture cammudito  ·  3Comments

rkmaier picture rkmaier  ·  5Comments

naoma123 picture naoma123  ·  3Comments

dzintb picture dzintb  ·  3Comments

BraveOtter picture BraveOtter  ·  4Comments