Insert columns from another table - pervasive

I'm not sure if I'm going the right way about this but I want to create another table with an auto increment ID.
I created another table but how do I add the columns from the first table into the new table and an auto increment ID?

Basically this is the same as copying any table into another table, just be sure not to specify the ID column.
INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable
See also: Copy data into another table

Related

MSSQL to create temporary identity column when selecting from table

I have a temporary table that I am selecting all the records from. Say it looks like this:
select * from #mytable
I need to add a temporary identity field to that selection. I've looked around and suggestions include using the IDENTITY(1,1) keyword in some fashion or just creating an auto-incrementing field like this:
row_Number() over (order by col1, col2) as myid
But that doesn't make the column an identity, it just creates a uniquely incremented field.
I know there should be a simple solution to this but I just can't find it.
I just want to know if it's possible to create a key identity field
while doing a select
Only by SELECT INTO:
SELECT IDENTITY(INT,1,1) AS IdentityColumn
,*
INTO #Temp
FROM sys.databases
But not via a plain SELECT, since IDENTITY is nothing more than just a column property that involves proprietary sequence generator and it works only on INSERT
You can use SELECT INTO
SELECT
IDENTITY (INT, 1, 1) AS NEW_ID, *
INTO #tempTable
FROM #mytable
SELECT * FROM #tempTable

mssql - Insert multiple rows back into original table from temporary table

If I select multiple rows into temporary table, how to insert these rows back into the same original table?
For example:
Select * into #temp_table from mytable where id=1 - will provide 4 rows for my temp_table.
Now I would like to insert these 4 rows back into the same original table (mytable) and If I will execute the same select statement after insert, I want to see 8 rows in results with just unique id.
What is the best and easiest way to do it? Maybe temporary table isn't good idea at all.
If you just want to add everything straight from the temporary table back into myTable, then you can do it simply by:
Insert into mytable
Select * from #temp_table
If you need to reuse the temporary table, then remember to clear this before adding the next lot of entries:
delete from #temp_table
Of course, it would be easier skipping the temporary table part all together if you just want to add some more entries from one table back into itself:
Insert into mytable
Select * from mytable where id=1
One thing to note here, this won't work if you have an auto incrementing identity column (as a Primary Key for example) in mytable, as select * will try to insert the ID again into the table which isn't allowed (must be unique). therefor, instead of using select *, you are best specifying the column names. This example would be better:
Insert into mytable (column1Name, column2Name)
Select column1Name, column2Name from mytable where id=1
Hope this is of some help. Good luck.

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 INTO Table with SELECT and auto incremented field

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

In an OUTPUT clause in an INSERT trigger, is it possible to reference both INSERTED tables?

On the creation of a new record in a table, I need to create a record in each of two other tables (b and c). The trick is that the two new records need to have the same PK value, which must be a UNIQUEIDENTIFIER and is therefore generated using NEWID() and has no relation to the key of the original record. So, what I want to do in the INSERT TRIGGER looks something like this:
INSERT INTO [b] ([bKey], [Foo])
OUTPUT inserted.[bKey] [cKey], i.[Bar] INTO [c]
SELECT NEWID(), i.[Foo] FROM inserted i
However, this seems to be illegal (In an OUTPUT clause in an INSTEAD OF INSERT trigger, is it possible to reference both INSERTED tables?). Is there any way to do this other than by using a CURSOR and a variable for the result of the NEWID()?
The OUTPUT clause of an INSERT statement cannot reference any tables other than the inserted pseudo table of the output clause - see the notes on from_table_name:
Is a column prefix that specifies a table included in the FROM clause of a DELETE, UPDATE, or MERGE statement that is used to specify the rows to update or delete.
I think you can use a table variable/temporary table to achieve your goal:
DECLARE #tmp TABLE (
[bKey] …,
[Foo] …,
[Bar] …
);
INSERT INTO #tmp ([bKey], [Foo], [Bar])
SELECT NEWID(), [Foo], [Bar] FROM inserted;
INSERT INTO [b] ([bKey], [Foo])
SELECT [bKey], [Foo] FROM #tmp;
INSERT INTO [c] ([cKey], [Bar])
SELECT [bKey], [Bar] FROM #tmp;

Resources