0

I need to turn this query into an update statement. I will have to update the values from fields. Everything is already in place but the update statement.

Here is the select version of the query:

SELECT i.GoLiveDate, i.FirstBonusRun, i.TechFName, i.TechLName, i.TechEmail, i.TechPhone, i.WebISPFName, i.WebISPLName, 
              i.WebISPEmail, i.WebISPPhone, i.FullFillFName, i.FullFillLName, i.FullFillEmail, i.FullFillPhone, d.FName,
              d.LName, d.HomePhone, d.Email
              FROM NC_Information i
              INNER JOIN Distributor d
                ON d.DistID = i.ClientID
              WHERE clientID = @value

Is it possible to update two different tables from within the same query?

Here is the code I have so far:

public void Update (int ClientID)
{
    using ( var conn = new SqlConnection( GeneralFunctions.GetConnectionString() ) )
    using ( var cmd = conn.CreateCommand() )
    {
        conn.Open();
        cmd.CommandText =
        @"SELECT i.GoLiveDate, i.FirstBonusRun, i.TechFName, i.TechLName, i.TechEmail, i.TechPhone, i.WebISPFName, i.WebISPLName, 
          i.WebISPEmail, i.WebISPPhone, i.FullFillFName, i.FullFillLName, i.FullFillEmail, i.FullFillPhone, d.FName,
          d.LName, d.HomePhone, d.Email
          FROM NC_Information i
          INNER JOIN Distributor d
            ON d.DistID = i.ClientID
          WHERE clientID = @value";
        cmd.Parameters.AddWithValue( "@value", ClientID );
        cmd.ExecuteNonQuery();
    }
}
1
  • Nope just update one table put statement. An update statement begins with update tablename. Commented Apr 23, 2012 at 18:01

3 Answers 3

3

You can't update multiple tables in one statement, but you can use a transaction to make sure that the updates are contingent upon one another:

BEGIN TRANSACTION

UPDATE SomeTable
SET SomeColumn  = 'Foo' 
WHERE SomeID = 123   

UPDATE AnotherTable
SET AnotherColumn = 'Bar'
WHERE AnotherID = 456    

COMMIT 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to try and implement this one. I like them being contingent upon one another.
0

I think, you cannot directly do the update on two tables. But you can Optimize the query.

How?

OUTPUT keyword in Insert/Update/Delete Statement

The first Update Statement's Select Data(filtered data) can be reused using the below mentioned example.

CREATE TABLE #table1
(
    id INT,
    employee VARCHAR(32)
)
go

INSERT INTO #table1 VALUES 
      (1, 'name1')
     ,(2, 'name2')
     ,(3, 'name3')
     ,(4, 'name4');
GO

DECLARE @GuestTable TABLE
(
    id INT,
    employee VARCHAR(32)
);

update #table1
Set id = 33
OUTPUT inserted.* INTO @GuestTable
Where id = 3

The Data in the '@GuestTable' Table is filtered data and can be reused.

select * from @GuestTable

drop table #table1

Comments

0

Alternatively, you can create a dataset with two datatables, and let the tableadaptermanager manage the updates.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.