Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL -How to add an auto incremental id in a auto generated temporary table

Tags:

sql

sql-server

I have a query like below and it generate a temporary table automatically based on parameter. So, the number of column of this table can be vary. Now , i need to add an auto incremental id column into this table.How i do it?

SELECT @SourceFields INTO ##StoreSourceInfo FROM testdb.dbo.@SourceTable

Note: 1) Number of source field & name of table pass using the parameter @SourceFields & @SourceTable. 2) So, the number of column can be vary on ##StoreSourceInfo table.

Current Result:

select * from ##StoreSourceInfo shows only the available column.

Expected Result: select * from ##StoreSourceInfo query will show an additional auto incremental id column & all rest of the column available in the temp table.

Hope you get me. Thanks in advance.

like image 305
riad Avatar asked Nov 21 '12 04:11

riad


People also ask

How do I add an auto increment to an existing table?

To add a new AUTO_INCREMENT integer column named c : ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (c); We indexed c (as a PRIMARY KEY ) because AUTO_INCREMENT columns must be indexed, and we declare c as NOT NULL because primary key columns cannot be NULL .

How can insert auto increment value in SQL query?

To obtain the value immediately after an INSERT , use a SELECT query with the LAST_INSERT_ID() function. For example, using Connector/ODBC you would execute two separate statements, the INSERT statement and the SELECT query to obtain the auto-increment value.

How do I set up auto increment?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.

How do you do two auto increments in SQL?

If you do really need to have a second column with "auto increment" type behavior, one way to get that is to add a second dummy table with an auto_increment column, and use a BEFORE INSERT trigger to do an insert into the dummy table, and retrieve the id value that was inserted.

Is it possible to add auto incremental identity column to table?

Question: Is it possible to add an auto incremental identity column to any table in SQL Server after creating a table. Answer: There are two answers – No and Yes. Let us see them one by one. Answer No – If you have an integer column in your table and you want to convert that column to identity table. It is not possible with the help of SQL Server.

How to add auto increment constraint to ID column in SQL?

Now, we will modify the id column to be auto increment, using ALTER TABLE. In the above statement, you need to specify the table_name and column_name. Here’s the SQL statement to add AUTO INCREMENT constraint to id column. Next we will add a couple of rows in sales table.

How do I add an auto increment field to a table?

In the MySQL server, it applies an auto increment field with the keyword AUTO_INCREMENT. By default, it starts with the number one and increases the value by one for each new record. In the example below, you will use the CREATE TABLE command to create a Students table and apply PRIMARY KEY and AUTO_INCREMENT to the ID column.

How to use the auto-increment feature in SQL Server?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).


4 Answers

SELECT
  IDENTITY(INT, 1, 1) AS id
INTO #Temptable
FROM User
like image 169
user2667356 Avatar answered Oct 30 '22 11:10

user2667356


You can use row_number function

Select ROW_NUMBER() over (order by T.field1) rownum
, T.field1, T.field2 into #temp1 
from @Table T
like image 26
Rajender Sehgal Avatar answered Oct 30 '22 12:10

Rajender Sehgal


Use the identity function. See the link for an example. http://msdn.microsoft.com/en-us/library/ms189838.aspx

like image 30
Stuart Ainsworth Avatar answered Oct 30 '22 12:10

Stuart Ainsworth


You have to try with following query to get your excepted result to add a extra auto increment column :

SELECT 
    IDENTITY(INT, 1,1) AS Rank, 
    @SourceFields 
INTO 
    ##StoreSourceInfo 
FROM 
    testdb.dbo.@SourceTable

Means apply IDENTITY function...

like image 25
Vimal bhatt Avatar answered Oct 30 '22 11:10

Vimal bhatt