SQL Server-How can I make a varchar auto increment

A. Gisbert

I am trying to run the following script but it is failing and I do not know why:

DROP TABLE table;
CREATE SEQUENCE seq START WITH 0;
CREATE TABLE table (
    id VARCHAR(512) PRIMARY KEY AS ('0000000000' + CAST(NEXT VALUE FOR seq AS VARCHAR(512)), 10),
    field VARCHAR(512),
    field VARCHAR(512)
);

I want to cast the number to string in order to match the requirements of other part of the code

Gordon Linoff

How about a different approach? Just use an identity column for the table. Then add a generated column that provides your string representation:

CREATE TABLE t (
    t_id int identity(1, 1) primary key,
    t_id_str as (right('0000000000' + CAST(t_id AS VARCHAR(512)), 10))
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can i make an ID auto increment and hidden from user?

How change auto increment statement in SQL Server?

How to reset auto increment column in SQL Server

How to auto increment INSERT INTO in SQL Server

SQL Server : how can I ignore the last 5 characters of a varchar

How can I replace varchar to table SQL Server

How Can Make sql server 2008 auto complete?

How can I make my query set order by descending in django based on auto increment id field

How do I auto-increment primary key from a custom number in SQL Server?

Salesforce migration to SQL Server: SF's primary key is alpha numeric so how do I auto increment?

How can I remove the ID column from a local sql table or change it to AUTO INCREMENT?

How can I make the loop increment?

How do I make an auto increment integer field in Django?

How do I make a primary key without auto-increment?

how to add primary key column with auto increment in sql server

SQL Server : how to alter already existing auto_increment for a column

How can I set AUTO_INCREMENT for @PrimaryColumn()?

How can I set no auto increment in migration laravel 5.3?

How can I auto-increment an MVC 6 version number?

how can I create a auto-increment field in mongoose

How can I auto-increment the value in a dictionary when there is a difference?

How can I auto increment numbered results with the DOM in javaScript?

How can I make SQL throw an error when the VARCHAR variable is too small for the assigned value?

How can i make an auto changing photo?

How can i make this auto play?

how can I make this auto multiple line

How can I make large IN clauses more efficient in SQL Server?

How can I make this SQL Server script dynamic through the years?

Sql server auto increment going random