Solved

Insert into values and select statement

Posted on 2008-10-28
10
1,605 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 10

Author Comment

by:ranhell
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now