How to INSERT INTO Table with SELECT and auto incremented field - sql-server

I am trying to insert from a table I created into a production table - I was able to do this earlier with a formula that I think caused duplicates and did not populate the primary key(In this case PROSPECT_ID properly. Since it is an auto incremented field and I am using a select from another table to INSERT any help on how to properly auto increment this field in production? MKT_PROSPECT
INSERT INTO MKT_PROSPECT(PROSPECT_ID,RECORD_TYPE,search_name,LABEL_NAME,FIRST_NAME,LAST_NAME,CUSTOMER_CLASS_CODE,CUSTOMER_STATUS_CODE,COMPANY_NAME,JOB_TITLE,FORMATTED_DETAIL,ADDRESS_1,ADDRESS_2,CITY,STATE,POSTAL_CODE,ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,HOME_PHONE,EMAIL,ADDOPER)
select PROSPECT_ID,RECORD_TYPE,SEARCH_NAME,LABEL_NAME,FIRST,LAST,CUSTOMER_CLASS_CODE,CUSTOMER_STATUS_CODE,COMPANY,TITLE,FORMATTED_DETAIL,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,PHONE,EMAIL,ADDOPER from dbo.prospects
where SEARCH_NAME IS not NULL and LABEL_NAME is not null

If you would like to insert into a table with a specific ID use IDENTITY INSERT ON as John Hartsock said in the comments. It is worth noting that the auto increment column will continue all other inserts (outside of the scope of the Identity Insert) above the largest number.
If you meant to insert and have the table auto generate you just don't include the ID column in the insert:
INSERT INTO MKT_PROSPECT(RECORD_TYPE,search_name,
LABEL_NAME,FIRST_NAME,LAST_NAME,CUSTOMER_CLASS_CODE,
CUSTOMER_STATUS_CODE,COMPANY_NAME,JOB_TITLE,
FORMATTED_DETAIL,ADDRESS_1,
ADDRESS_2,CITY,STATE,POSTAL_CODE,
ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,
HOME_PHONE,EMAIL,ADDOPER)
select RECORD_TYPE,SEARCH_NAME,LABEL_NAME,FIRST,LAST,
CUSTOMER_CLASS_CODE,CUSTOMER_STATUS_CODE,COMPANY,TITLE,
FORMATTED_DETAIL,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,
ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,
PHONE,EMAIL,ADDOPER from dbo.prospects
where SEARCH_NAME IS not NULL and LABEL_NAME is not null

Related

SQL Server creating a Table with an IDENTITY COLUMN - uniqueness

In SQL Server, I have created a Table with an ID column that I have made an IDENTITY COLUMN,
EmployeeID int NOT NULL IDENTITY(100,10) PRIMARY KEY
It is my understanding, when I use the IDENTITY feature, it auto increments the EmployeeID. What I don't know/not sure is:
Is that IDENTITY number created, unique?
Does SQL search the entire column in the table to confirm the number created does not already exist?
Can I override that auto increment number manually?
If I did manually override that number, would the number I enter be checked to make sure it is not a duplicate/existing ID number?
Thanks for any help provided.
Is that IDENTITY number created, unique?
Yes, Identity property is unique
Does SQL search the entire column in the table to confirm the number created does not already exist? \
It need not, what this property does is, just incrementing the old value
Can I override that auto increment number manually?
Yes, you can. You have to use SET IDENTITY_INSERT TABLENAME ON
If I did manually override that number, would the number I enter be checked to make sure it is not a duplicate/existing ID number?
No, that won't be taken care by SQL Server, you will have to ensure you have constraints to take care of this
Below is a simple demo to prove that
create table #temp
(
id int identity(1,1)
)
insert into #temp
default values
go 3
select * from #temp--now id column has 3
set identity_insert #temp on
insert into #temp (id)
values(4)
set identity_insert #temp off
select * from #temp--now id column has 4
insert into #temp
default values
go
select * from #temp--now id column has 5,next value from the last highest
Updating info from comments:
Identity column will allow gaps once you reseed them,also you can't update them

Insert from temp table to a table with identity column

I'm grabbing some rows from a table, manipulating them in a temp table, and then looking to insert them as new rows into my original table.
However, I'm running into an issue with the identity column, even when I don't have the identity column on my temp table. The identity column is an auto-incrementing int.
This seems like a simple thing I'm way overthinking.
select top 0 *
into #TestTable
from OriginalTable;
...
--insert and manipulate records
...
ALTER TABLE #TestTable
DROP COLUMN MyIdentityColumn;
DECLARE #InsertedRows TABLE (NewSeqNum INT);
INSERT INTO OriginalTable
OUTPUT MyIdentityColumn INTO #InsertedRows(NewSeqNum)
SELECT * FROM #TestTable
but I get this error:
An explicit value for the identity column in table 'OriginalTable' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I absolutely do not want to set an explicit value, I want it to insert and give me the new identity (via #InsertedRows)
If you don't want to keep the id of inserted records, then you need to specify all your columns but the id column in the select. As general good practice, dont select *, always specify the columns you want to retrieve-insert.
INSERT INTO OriginalTable (col1, col2, col3...)
OUTPUT MyIdentityColumn INTO #InsertedRows(NewSeqNum)
SELECT (col1, col2, col3...) FROM #TestTable
If I'm understanding you, I think your problem is that you're trying to insert '*' into the original table - which means all of your columns from the temp table. Including your ID column (which you don't want to insert, because you're wanting it to auto-generate.)
Instead, I'd suggest doing something like this:
Select [ColumnB],[ColumnC],[ColumnD],[Etc] into your temp table
Select [ColumnB],[ColumnC],[ColumnD],[Etc] into your original table.
... aka, spell out the columns explicitly, and omit the Identity column.

How to insert record into table which is having 6054 records

I am having one table with 3 f_Key and 1 P_Key with 6054 records.
One record is lost from that table. I am trying to insert record into that table.
The record id is 2352 and last record id is 9560 so,if i insert the record then it is taking 9561 id which is next id of before id.If try to delete the others records then because of F_Key it is not allowing to delete also.If i try to update the 9561 id then it also not allowing to update.
You can use the SET IDENTITY INSERT construct to explicitly insert the PK value in a table with auto-numbering, like so:
set identity_insert #your_table on
insert into your_table (PK_COL_IDENTITY, ...) values (2352, ...)
set identity_insert #your_table off
As per my knowledge , if your ID is auto incremented then you cannot update that ID(key) .The only way to do in your case is TRUNCATE.If you will truncate the table then it will allow to generate new sequence.
You can create a temporary table and migrate the data to temporary table and truncate that parent table and again migrate the data from temporary table to parent table.
Hope it will help you.

Insert multiple rows of default values into a table

I have a table with a single column, which is an auto-generated identity
create table SingleIdTable (
id int identity(1,1) not null
)
I can insert a single row with an auto-generated id with:
insert into SingleIdTable default values
I want to insert many rows and use the output syntax to get their ids, something like:
insert into SingleIdTable
output inserted.Id into #TableOfIds
select (default values) from SomeOtherTable where Attribute is null
Where the intention is to insert a row into SingleIdTable for each row in SomeOtherTable where Attribute is null using an auto-generated id. The above doesn't work, but how could I do it. I note that if my table had more than just a single column I could do it, but I can't select empty rows which is what I really want to do.
I can't change the definition of SomeOtherTable.
If SQL Server 2008+ you can use MERGE for this. Example syntax below.
MERGE INTO SingleIdTable
USING (SELECT *
FROM SomeOtherTable
WHERE Attribute IS NULL) T
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT
DEFAULT VALUES
OUTPUT INSERTED.id;
I'm not sure what practical use this single column table has though?
you did not specify which version of SQL Server you are on. If you happen to be on SQL 2012 you probably can replace you SingleIdTable with a sequence: http://msdn.microsoft.com/en-us/library/ff878091.aspx

insert issues in an auto increment column

How can I insert data into a column which has been defined as auto increment column using identity insert?please explain with an example.
If you have an "auto-increment" column - you really shouldn't be inserted specific values into that column yourself - after all, that's why it's an auto-increment column....
If you must do so after all - then you need to do:
SET IDENTITY_INSERT (your table name here) ON
INSERT INTO (your table name here) (IdentityCol, OtherCol1, .....)
VALUES( (new ID value), .......)
SET IDENTITY_INSERT (your table name here) OFF

Resources