-1

I have a update statement that will change the state in a tableA from TableB. The data in this table is like:

Serial   PersonId    Date        Department    State      Begintime    Endtime
    1       1        2012-01-01      50          1        08:00        11:00
    2       1        2012-01-01      50          5        11:00        14:00

When I get State 2 from tableB then I need to set the states of the both records to: 2 but I want have just one record for each state.

The result should be:

Serial   PersonId    Date        Department    State     Begintime     Endtime
    1       1        2012-01-01      50          2       08:00         14:00

Vice versa also must be possible:
TableA contains:

Serial   PersonId    Date        Department    State     Begintime     Endtime
    1       1        2012-01-01      50          2       08:00         14:00

After the update I would like to have: (this two records are coming from tableB)

Serial   PersonId    Date        Department    State      Begintime    Endtime
    1       1        2012-01-01      50          1        08:00        10:00
    2       1        2012-01-01      50          5        12:00        15:00

I also don't know if this need to be updated with a update statement.
Any ideas to solve this?

I'm using sql server 2005.

4

1 回答 1

0

艾哈迈德,这对你有用吗?

INSERT INTO tableA a
SELECT * FROM tableB b
WHERE b.[State] NOT IN (
    SELECT [State] FROM tableA
)

我假设您想要在表之间复制新记录。

于 2012-11-16T03:40:12.843 回答