Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Insert Into

Posted on 2005-05-11
5
Medium Priority
?
249 Views
Last Modified: 2011-08-18
I'll try to explain this the best I can.  I have a table named "Batches" that I'm trying to insert into one row of data at a time.  In VB.NET I have a dropdown box that displays a number from another column "Code" from another table "Products".  Inside the "Products" table is a column named "ProductID" which corresponds with the Products.Code column.  Here is an example:

Products Table

ProductID     Code
1                 301C
2                 3386
3                 3278
4                 3375
5                 3201

I am trying to insert the ProductID number into "Batches.Product" column.  

In VB.NET my dropdownbox is called "product" and displays the Code from "Products.Code".  My question is, how do I insert into Batches.Product the "ProductID" corresponding with the "Code".

This is what I have so far, but I know it's incomplete:

INSERT INTO Batches (Product)
Values ('" & product.selectedItem.Text & "')"
0
Comment
Question by:johnqtr
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13978137
Try this:

"INSERT INTO Batches (Product)
SELECT ProductID
FROM Products
WHERE Code = '" & product.selectedItem.Text & "'"
0
 

Author Comment

by:johnqtr
ID: 13978352
Okay, what about if I needed to enter data in more than one column?  Let's say I need to enter 25863 in the "BatchID" column within table "Batches"?  Would I do this?

"INSERT INTO Batches (Product, BatchID)
VALUES ('', '25863')
SELECT ProductID
FROM Products
WHERE Code = '" & product.selectedItem.Text & "'"
0
 
LVL 28

Accepted Solution

by:
rafrancisco earned 1600 total points
ID: 13978363
Try this:

"INSERT INTO Batches (Product, BatchID)
SELECT ProductID, '25863'
FROM Products
WHERE Code = '" & product.selectedItem.Text & "'"
0
 

Author Comment

by:johnqtr
ID: 13978447
Thank You!  That worked great!
0
 
LVL 8

Expert Comment

by:Julianva
ID: 13978479
insert into batches
(productid
,product
,batchid
)

select
productid,
code,
batchid = 25863
from products
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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

564 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