Solved

Insert into values and select statement

Posted on 2008-10-28
10
1,611 Views
Last Modified: 2012-05-05
I'm using MS Access as the DB, Visual Studio VB.net as the interface and I'm trying to run the following SQL Statement

            sSql = "Insert Into TEMPIN(IDSTAT,FECHACON)"
            sSql += " Values(2,'" & dtfecha & "');"
            sSql += " Select * FROM MASTER"
            sSql += " WHERE CB = '" & strLine & "' and IDSTAT = 1 "
            If Con.State = ConnectionState.Closed Then Con.Open()
            cmd = New OleDb.OleDbCommand(sSql, Con)
            cmd.ExecuteNonQuery()

What I want to achive is Insert all field from "MASTER" table into "TEMPIN" table where both tables are identical, plus a 2 value fields from different variables
Is this posible to mix the VALUE and SELECT in a INSERT INTO SQL Statement
0
Comment
Question by:ranhell
[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
  • 3
10 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 22824061
If you are selecting all the fields to be inserted you can just do this:

Insert Into TEMPIN
Select statement here


If you are inserting all the fields into the table you just need to select them all (in the order they appear in the table) and that will insert them for you without having to identify each field individually.

Then the select statement can be any statement that produces the required results you are looking for.
0
 
LVL 10

Author Comment

by:ranhell
ID: 22824116
I know how to do that, but that doesn't solve my problem,
Let me clarify.
I have Tables MASTER and TEMPIN
And I want to Insert all fields from Table MASTER to TEMPIN, plus I need to Insert to values from 2 different variables to table TEMPIN using the SQL clause Values.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 22824279
ranhell said:
>>I have Tables MASTER and TEMPIN
>>And I want to Insert all fields from Table MASTER to TEMPIN, plus I need to Insert to values from 2 different variables to table TEMPIN using the SQL clause Values.

The you need two different inserts, one using syntax:

INSERT INTO Table ()
SELECT ...

INSERT INTO Table () VALUES ()
0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
LVL 10

Author Comment

by:ranhell
ID: 22824365
Again I know How to do that see queries below.

            sSql = "Insert Into TEMPIN"
            sSql += " Select * FROM MASTER"
            sSql += " WHERE CB = '" & strLine & "' and IDSTAT = 1 "
            If Con.State = ConnectionState.Closed Then Con.Open()
            cmd = New OleDb.OleDbCommand(sSql, Con)
            cmd.ExecuteNonQuery()


            sSql = "UPDATE TEMPIN Set IDSTAT = 2, FECHACON = '" & dtfecha & "'"
            If Con.State = ConnectionState.Closed Then Con.Open()
            cmd = New OleDb.OleDbCommand(sSql, Con)
            cmd.ExecuteNonQuery()

What I was hoping is to do only 1 SQL statement what would combine both.
is these posible...??
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 22824432
ranhell said:
>>What I was hoping is to do only 1 SQL statement what would combine both.
>>is these posible...??

No, you need separate statements.
0
 
LVL 10

Author Comment

by:ranhell
ID: 22824581
OK, then I'm trying to do the following SQL statement
sSql = "UPDATE MASTER SET IDSTAT = TEMPIN.IDSTAT, FECHACON = TEMPIN.FECHACON"
        sSql += "FROM MASTER inner join TEMPIN WHERE CB = TEMPIN.CB"
        If Con.State = ConnectionState.Closed Then Con.Open()
        cmd = New OleDb.OleDbCommand(sSql, Con)
but it doesn't do what is supposedly to do, it does not update MASTER table and it doesn't give me an error either.

can you help me?
0
 
LVL 10

Author Comment

by:ranhell
ID: 22825142
never mind I figure it out

Sql = "UPDATE"
            sSql += " MASTER INNER JOIN TEMPIN ON MASTER.CB = TEMPIN.CB"
            sSql += " SET MASTER.IDSTAT = TEMPIN.IDSTAT, MASTER.FECHACON = TEMPIN.FECHACON"
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22827588
I object.  The user needed two different SQL statements to do what s/he wanted to do, as I pointed out.

Patrick
0
 
LVL 10

Author Closing Comment

by:ranhell
ID: 31510843
Good
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

626 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