william
william

Reputation: 7664

Update and select in one query

I found similar questions with correct answers. But they're a bit complicated for me. I just want a simple basic statement.

I have:

string sql = "UPDATE tblPopUp 
                 SET PopUp = 'False' 
               WHERE DisplayNo = 1"

...and:

string sql1 = "SELECT Period  
                 FROM tblPopUp 
                WHERE DisplayNo = 1"

How can I combine them?

Upvotes: 16

Views: 60287

Answers (5)

Marek Bernád
Marek Bernád

Reputation: 629

Old Q, but still in usage, for psql solution, try this:

UPDATE table SET column = value
WHERE condition
RETURNING column;

Upvotes: 1

Lafras Henning
Lafras Henning

Reputation: 133

The correct way to do this (now for MySQL 5+), would be with a stored procedure.

Upvotes: 1

Subhash
Subhash

Reputation: 261

UPDATE tblPopUp  
SET PopUp = 'False', Period = Period  
OUTPUT DELETED.Period
WHERE DisplayNo = 1

For more information about OUTPUT clause please check this post.

Upvotes: 19

mahesh
mahesh

Reputation: 1390

Try This

UPDATE tblPopUp 
             SET PopUp = 'False' 
           WHERE DisplayNo = '1'
(
SELECT Period  
             FROM tblPopUp 
            WHERE DisplayNo = '1'
)

Upvotes: 0

OMG Ponies
OMG Ponies

Reputation: 332531

You can't.

There's no convention in a SQL UPDATE statement for returning data. And vice versa -- a SELECT statement doesn't write information to a table.

If you've found questions/answers that you feel are similar to what you want, please provide links.

Upvotes: 2

Related Questions