?
Solved

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

Posted on 2014-03-13
4
Medium Priority
?
447 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 41

Accepted Solution

by:
Sharath earned 2000 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 41

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

830 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