在使用SQL Server Management Studio的SQL编码中,我可以从插入或删除时具有不同行数的表中删除。我更新表A之后,如果表B中有表项,我想删除表A中的匹配项。我的想法是最后只有表A中已更新的行。我有10列,但将使用4到把事情简单化。
CREATE TRIGGER utr_Updates
ON tableA
AFTER UPDATE
AS BEGIN
SET NOCOUNT ON;
EXEC usp_updates
END
CREATE PROCEDURE usp_Updates
AS
BEGIN
IF EXISTS (SELECT * FROM tableA
WHERE col1 IN (SELECT DISTINCT col1 FROM tableB))
DELETE FROM tableA
WHERE col1 IN (SELECT DISTINCT col1 FROM tableB)
IF EXISTS (SELECT * FROM tableA
WHERE col2 IN (SELECT DISTINCT col2 FROM tableB))
DELETE FROM tableA
WHERE col2 IN (SELECT DISTINCT col2 FROM tableB)
IF EXISTS (SELECT * FROM tableA
WHERE col3 IN (SELECT DISTINCT col3 FROM tableB))
DELETE FROM tableA
WHERE col3 IN (SELECT DISTINCT col3 FROM tableB)
IF EXISTS (SELECT * FROM tableA
WHERE col4 IN (SELECT DISTINCT col4 FROM tableB))
DELETE FROM tableA
WHERE col4 IN (SELECT DISTINCT col4 FROM tableB)
END
START POINT
START
TABLE A TABLE B
col1 W, X, Y, Z col1 W, X, Y, Z
col2 1, 2, 3, 4 col2 1, 2, 3, 4
col3 A, 5, 6, D col3 A, 5, 6, D
col4 7, B, C, D col3 7, B, C, D
表A更新了col1和col3
AFTER UPDATE
TABLE A TABLE B
col1 W, S, Y, Z col1 W, X, Y, Z
col2 1, 2, 3, 4 col2 1, 2, 3, 4
col3 A, 5, 1, D col3 A, 5, 6, D
col4 7, B, C, D col3 7, B, C, D
期望的结果
TABLE A
col1 W, S, Y, Z
col3 A, 5, 1, D
答案 0 :(得分:0)
您可以使用...
streamState = waitOnStreamForState(stream, 4);
int i = stream.readInt();
...
private static int
waitOnStreamForState(DataInputStream stream, int nBytes) throws IOException {
return waitOnStream(stream, nBytes, STREAM_ACTIVITY_THRESHOLD, STREAM_POLL_INTERVAL)
? STREAM_STATE_ACTIVE
: STREAM_STATE_INACTIVE;
private static boolean
waitOnStream(DataInputStream stream, int nBytes, long timeout, long pollInterval) throws IOException {
int timeWaitingForAvailable = 0;
while( stream.available() < nBytes ){
if( timeWaitingForAvailable >= timeout && timeout > 0 ){
return false;
}
try{
Thread.sleep(pollInterval);
}catch( InterruptedException e ){
Thread.currentThread().interrupt();
return (stream.available() >= nBytes);
}
timeWaitingForAvailable += pollInterval;
}
return true;
}
:
NOT EXISTS
或者,在SQL Server中,您可以使用select a.*
from a
where not exists (select 1
from b
where b.col1 = a.col1 and b.col2 = a.col2 and b.col3 = a.col3 and b.col4 = a.col4
);
:
EXCEPT