English 中文(简体)
自动启动问题
原标题:Auto Increment Problem

利用服务器

表1

ID Name

001 Raja
002 Ravi
003 Suresh
004 Kamal
...

我需要设立一个新的表格,为每个栏目确定身份

Tried query

insert into table2 
  select * from table1

预期产出

S.No ID Name

1 001 Raja
2 002 Ravi
3 003 Suresh
4 004 Kamal
...

I create the new table and made the S.No column is identity in the table. It is creating the identity number for each row, then I delete all the row, again inserted, but identity is created from last row next number onwards.

S.No ID Name

    1 001 Raja
    2 002 Ravi
    3 003 Suresh
    4 004 Kamal
    ...

删除所有各行,我再次插入。

S.No ID Name

    5 001 Raja
    6 002 Ravi
    7 003 Suresh
    8 004 Kamal
    ...

预期产出

身份应从第1号开始,只要我从表格中删除所有各行。

问询

最佳回答

这并不是身份如何发挥作用。 拆除路障后,重新打造了身份领域。 你们必须重新看到:

DBCC CHECKIDENT ( ‘databasename.dbo.yourtable’,RESEED, 0) -- will start from 1
问题回答

Or just truncate the table? That will reset the primary key, too.
(marc_s: actually, it s the IDENTITY value that it s resetting)

TRUNCATE TABLE tablename

如果您想要S.No.R.R.R.R.R.R.R.R.R.R.R.R.R.R.R.R.R.,N.R.C.R.R.R.R.R.R.R.R.R.R.R.R.R.M.,P.R.R.R.R.R.R.R.R.R.M.,P.R.R.R.R.,

因此,你可以发挥cal的作用,找到这一价值,将DefaultValue列列到这一功能中,并设定identity to false

查阅这一样本:

CREATE FUNCTION [dbo].[getmaxno]()
RETURNS int
AS
BEGIN
    DECLARE @ResultVar int
    SELECT @ResultVar = MAX(no)+1 from dbo.table_1
    RETURN @ResultVar
END

ALTER TABLE [dbo].[Table_1] ADD  CONSTRAINT [DF_Table_1_no]  DEFAULT ([dbo].[getmaxno]()) FOR [no]




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签