Solved

SQL Server 2005 Stored Procedure to insert records where ID is greater than Max ID

Posted on 2014-03-13
4
426 Views
Last Modified: 2014-03-13
Hello,

I'm pretty new at SQL Server.  What I'm trying to do I imagine is pretty simple.  I have 2 identical tables, TableA and TableB.  I want to insert the records from TableA into TableB where the ID is greater than the max ID already in TableB

INSERT INTO [Server1].DB1.dbo.tblOrderItemSize
                      (QCID, JoxID, Parent, Whs, OrderNum, LineNum, [Assembly], GlassSize, SashSize, FrameSize, AssyLoc, QNADesc, DateStamp, GridType, GridLayout)
SELECT     QCID, JoxID, Parent, Whs, OrderNum, LineNum, [Assembly], GlassSize, SashSize, FrameSize, AssyLoc, QNADesc, DateStamp, GridType, 
                      GridLayout
FROM         tblImportSize AS tblImportSize_1
WHERE     (Whs = 140) AND (QCID > MaxID)
                          ((SELECT MAX (QCID)AS MaxID
                            FROM          [Server1].DB1.dbo.tblOrderItemSize)))

Open in new window


Thanks for your help!
0
Comment
Question by:jeff-h
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39927514
like this.
INSERT INTO [Server1].DB1.dbo.tblOrderItemSize 
            (QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout) 
SELECT QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout 
  FROM tblImportSize AS tblImportSize_1 
 WHERE ( Whs = 140 ) 
   AND QCID > (SELECT MAX (QCID)AS MaxID 
                 FROM [Server1].DB1.dbo.tblOrderItemSize) 

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 39927521
you can add table alias in the sub-query as well.
INSERT INTO [Server1].DB1.dbo.tblOrderItemSize 
            (QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout) 
SELECT QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout 
  FROM tblImportSize AS tblImportSize_1 
 WHERE ( Whs = 140 ) 
   AND QCID > (SELECT MAX (t1.QCID) 
                 FROM [Server1].DB1.dbo.tblOrderItemSize t1) 

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39927522
Being on mobile makes it difficult to write long sql....
I would retrieve the max Id into a variable
Select @var =max(id) from table

Anduse row_number function

Insert into tableb select @var + row_number() over (order by somefield) , ... from tablea
0
 

Author Closing Comment

by:jeff-h
ID: 39927620
Thanks, this worked great!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now