Inserting new rows in a table in SQL - sql-server

I have a table with 613 rows already. When I want to insert new rows from another table, the new rows are updated correctly in terms of the respective column but the ID column that is IDENTITY shows random numbers after 613. Like it doesn't update according to the count for new rows that are inserted.
I'm using Insert into Select statement.

Related

SQL Insert does column order matter

I have two tables with the same field names and a stored procedure that updates table B with Table A's data by doing a delete from current table and insert into current table from another table that has update values in it:
delete from ac.Table1
insert into ac.Table1
select *
from dbo.OriginalTable
where dtcreate <getdate()-1
I had to recreate Table1 through GIS software which adds GlobalIDs and an Object ID field. The original order had Object ID at the end and the new table has it at the front. Will this impact executing the SQL statement above?
Yes it will. The order of the columns should match for each value to go in desired column
You can try
Insert into ac.Table1 (column1....columnN)

Duplicate records in a table with identity column as PK

One of our developer inserted few million rows from a table to a target table.
He inserted using a while loop in batches and now in the target table there is some 5 million duplicate rows.The issue is that the PK is identity column and while inserting he didn't do
SET IDENTITY_INSERT DBO.TABLE_NAME ON
So now in the table there are duplicate entries with a distinct identity column value.
If i group by as shown below :
group by COL2,COL3,COL4,COL5,COL6,COL7
i can get a unique row.
Can someone help me to create script to delete the duplicate records.
Create #temp table of your final distinct records of your table. After that delete all the records from your table having duplicate id.
After that you may use this #temp table to re-enter this record in your existing table.
set identity insert on/off is upto you.
For hint search for checkident.

Sql query - How to insert values into multiple rows that have same FK

Got two tables here. Order might contains multiple order_lines.
What I'm trying to do is to create rows in order_line table by using the trigger I created in order table whenever I insert a new row in order table. The number of rows in order_line is same with item_quantity field in order table.
The problem is the trigger can only insert order_line_id and order_id columns without order_item_type and order_item_quantity because these two value is entered from GUI and the trigger can't insert them at the same time it creates new row.
So I need a sql query that can let me insert multiple values into multiple rows that have same order_id one by one.

how to get newly added rows in db2 tables after adding column to it

I have fews columns in db2 table and then I have added one more column to it. I then insert few rows in the table. How to get the newly added rows after table alteration?
Note : There is no timestamp related column in table , so the logic based on it cant be used.
Regards,
Without a timestamp there is no "before" or "after" rows, they're all the same. Rows inserted before the change will all have null values in the newly added column, so you could use that fact to distinguish them from those you inserted after the change (if the new rows have non-null values in that column).

SQL Server get delta items

I have a many-million-records table having an integer primary key defined.
I also have a not-so-many-million IDs that are in the "black list". They are stored in memory (read from a file on disk).
I have to select the records that are NOT in the black list, that is, all the records whose ID is not in my black list.
I solved this using a temp table (single column: ID) to insert the unwanted IDs then select all records whose IDs are not in this table.
My main concern is performance:
Inserting so many record in temp table.
Selecting the item not being in temp table.
EDIT
At the moment I use a temp table like this:
create the temp table with a single column (ID)
fill the temp table with IDs
create a nonclustered index on column
get the delta items with a query similar to this:
select m.id from mytable m where m.id not in (seelct id from #tempTable)
The best option you have here is to add a column to flag each row whether it is blacklisted or not ( ex: call it isBlockListed), and keep this column up-to-date.
You can also add an unclustered index to this flag so you can quickly select your data where isBlockListed = ture / false.

Resources