?
Solved

T-SQL - INSERTing range of rows from one table into antoher

Posted on 2008-11-02
5
Medium Priority
?
599 Views
Last Modified: 2012-05-05
Hi!

I'm creating a stored procedure, that will accept a value from a user, check against one table and then copy corresponding rows from that table into another.

I've made the procedure, but ItemID remains all the same in all rows...
What do I do wrong here?



CREATE PROC up_name
	@SerialNo		NVARCHAR(10)
AS
INSERT  ProdCycle
(
		OrderID,
		SerialNum,
		ItemNo
)
SELECT ProdID, SerialNum, ItemNo
FROM Serials
WHERE OrderID = 
(
		SELECT OrderID,
		FROM Serials
		WHERE SerialNum=@SerialNum
)

Open in new window

0
Comment
Question by:Zaurb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22862371
OrderID or ItemNo?

I see no ItemID

Also, you are passing in @SerialNum, but then looking up the OrderID from the same table.  Why not do this because it is essentially what you are telling it to do.:




INSERT  ProdCycle
(
                OrderID,
                SerialNum,
                ItemNo
)
SELECT ProdID, SerialNum, ItemNo
FROM Serials
WHERE SerialNum=@SerialNum

Open in new window

0
 
LVL 1

Author Comment

by:Zaurb
ID: 22862412
Sorry, ItemID=ItemNo.

The Serials table contains OrderID column but it's not actually an ID column. It allows duplicate values. And SerialNum, instead is a unique value in this table. So, I need to pass SerialNum to query, that will check OrderID for this SerialNum and select all rows with the same OrderID to another table.

The Serials table is similar to the following sample:

OrderID            SerialNum                      ItemID
945                   1a21s23                       231521
945                   1a21s43                       231411
945                   1a21ac3                       231311
102                   1a21we3                      231512
102                   1a21sd3                       231511

I need to pass a serial number, like 1a21s23 and insert all rows with OrderID 945 into another table.

CREATE PROC up_name
        @SerialNo               NVARCHAR(10)
AS
INSERT  ProdCycle
(
                OrderID,
                SerialNum,
                ItemNo
)
SELECT OrderID, SerialNum, ItemNo
FROM Serials
WHERE OrderID = 
(
                SELECT OrderID,
                FROM Serials
                WHERE SerialNum=@SerialNum
)

Open in new window

0
 
LVL 9

Accepted Solution

by:
jamesgu earned 500 total points
ID: 22862511
the code seems OK to me,

try to execute the select statement , see what do you get?

SELECT OrderID, SerialNum, ItemNo
FROM Serials
WHERE OrderID =
(
                SELECT OrderID,
                FROM Serials
                WHERE SerialNum=@SerialNum
)
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22862626
Think you just have an extra comma in code.
CREATE PROC up_name
	@SerialNo		NVARCHAR(10)
AS
INSERT  ProdCycle
(
		OrderID,
		SerialNum,
		ItemNo
)
SELECT ProdID, SerialNum, ItemNo
FROM Serials
WHERE OrderID = 
(
		SELECT OrderID /*, <-- remove this */
		FROM Serials
		WHERE SerialNum=@SerialNum
)

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 22862641
With your corrected code:
CREATE PROC up_name
        @SerialNo               NVARCHAR(10)
AS
INSERT  ProdCycle
(
                OrderID,
                SerialNum,
                ItemNo
)
SELECT OrderID, SerialNum, ItemNo
FROM Serials
WHERE OrderID = 
(
                SELECT OrderID
                FROM Serials
                WHERE SerialNum=@SerialNum
)

Open in new window

0

Featured Post

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

800 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