Solved

Insert into values and select statement

Posted on 2008-10-28
10
1,607 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
  • 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 92

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 92

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 92

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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