我正在尝试从一些单租户DB(每个客户端)到多租户DB(所有客户端或“全局”)进行一些ETL。在全局数据库中,我试图维护一个查找表,该表标识与客户端关联的用户。所以我有这个:
filterCriterias.RemoveAll(c => c.ColumnName == null ||
c.Comparator == null ||
c.Criteria == null);
在执行其他一些ETL操作之后,我想通过删除Client.Users表中不再存在的所有User来清理ClientUsers表。我认为这会起作用:
// $file is the path plus name of the file location localy.
private function CheckFileIntegrity($file) {
$dl = false;
$fileR = file($file);
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$dl = true;
}
}
return $dl;
}
但这会删除所有记录。我测试了各个选择语句,它们返回了我第一次测试所期望的结果,其中没有要删除的语句。两个选择语句都返回完全相同的数据集。对我而言,这意味着不应删除任何记录。对?显然,我对EXISTS的工作方式缺乏基本的了解或细微差别,因为这似乎应该很简单!
我意识到可以使用其他方法来执行此操作,但是我很困惑为什么EXCEPT不能像我认为的那样工作。
答案 0 :(得分:2)
它不起作用的原因是您实际上是在顺序运行两个语句。将您的代码更像是:
@RequestMapping(value = "/token", method = RequestMethod.POST)
public ResponseEntity<Jwt.Response> postToken(TokenBody body, HttpServletResponse response) throws UnirestException, IOException,
HttpUnauthorizedException {
Jwt.Response jwt = null;
try {
jwt = authService.loginHumanUser(body.getGroups(), body.getCode(), body.getRedirect_uri());
} catch (HttpUnauthorizedException e) {
throw new HttpUnauthorizedException("Failed to successfully authenticate with the application", e);
}
int expirationSeconds = Integer.valueOf(environment.getRequiredProperty("EXPIRATION_SECONDS"));
Cookie accessTokenCookie = new Cookie("access_token", jwt.getAccessToken());
accessTokenCookie.setDomain("dev-myapp.com");
accessTokenCookie.setHttpOnly(true);
accessTokenCookie.setMaxAge(expirationSeconds);
accessTokenCookie.setSecure(true);
accessTokenCookie.setPath("/");
response.addCookie(accessTokenCookie);
Cookie loggedInCookie = new Cookie("logged_in", "true");
loggedInCookie.setDomain("dev-myapp.com");
loggedInCookie.setSecure(true);
loggedInCookie.setMaxAge(expirationSeconds);
loggedInCookie.setPath("/");
response.addCookie(loggedInCookie);
return ResponseEntity.ok(jwt);
}
如果要修改Set-Cookie
语句,则需要在其后加上DECLARE @ClientID varchar = 'ClientA'
DELETE FROM Global.dto.ClientUsers;
SELECT ClientID, UserID FROM Global.dto.ClientUsers WHERE ClientID=@ClientID
EXCEPT
SELECT ClientID=@ClientID, UserID FROM ClientA_DB.dbo.Users;
,Delete
等。
有关获得所需结果的一些替代方法,请参见以下出色的答案: Using T-SQL EXCEPT with DELETE / Optimizing a query