我使用触发器尝试使用以下语句从另一个表更新记录但是我收到错误
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。声明已经终止。
这是我使用的代码
class ErrorHandler implements ServerErrorHandler {
@Override public void error(Context context, Throwable throwable) throws Exception {
try {
Map<String, String> errors = new HashMap<>();
errors.put("error", throwable.getClass().getCanonicalName());
errors.put("message", throwable.getMessage());
Gson gson = new GsonBuilder().serializeNulls().create();
context.getResponse().status(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()).send(gson.toJson(errors));
throw throwable;
} catch (Throwable throwable1) {
throwable1.printStackTrace();
}
}
}
答案 0 :(得分:0)
如果在触发器中使用来自#!/usr/bin/python
# encoding: utf-8
import random
import torch
from torch.utils.data import Dataset
from torch.utils.data import sampler
import torchvision.transforms as transforms
import lmdb
import six
import sys
from PIL import Image
import numpy as np
class lmdbDataset(Dataset):
def __init__(self, root=None, transform=None, target_transform=None):
self.env = lmdb.open(
root,
max_readers=1,
readonly=True,
lock=False,
readahead=False,
meminit=False)
if not self.env:
print('cannot creat lmdb from %s' % (root))
sys.exit(0)
with self.env.begin(write=False) as txn:
nSamples = int(txn.get('num-samples'))
self.nSamples = nSamples
self.transform = transform
self.target_transform = target_transform
def __len__(self):
return self.nSamples
def __getitem__(self, index):
assert index <= len(self), 'index range error'
index += 1
with self.env.begin(write=False) as txn:
img_key = 'image-%09d' % index
imgbuf = txn.get(img_key)
buf = six.BytesIO()
buf.write(imgbuf)
buf.seek(0)
try:
img = Image.open(buf).convert('L')
except IOError:
print('Corrupted image for %d' % index)
return self[index + 1]
if self.transform is not None:
img = self.transform(img)
label_key = 'label-%09d' % index
label = str(txn.get(label_key))
if self.target_transform is not None:
label = self.target_transform(label)
return (img, label)
class resizeNormalize(object):
def __init__(self, size, interpolation=Image.BILINEAR):
self.size = size
self.interpolation = interpolation
self.toTensor = transforms.ToTensor()
def __call__(self, img):
img = img.resize(self.size, self.interpolation)
img = self.toTensor(img)
img.sub_(0.5).div_(0.5)
return img
class randomSequentialSampler(sampler.Sampler):
def __init__(self, data_source, batch_size):
self.num_samples = len(data_source)
self.batch_size = batch_size
def __iter__(self):
n_batch = len(self) // self.batch_size
tail = len(self) % self.batch_size
index = torch.LongTensor(len(self)).fill_(0)
for i in range(n_batch):
random_start = random.randint(0, len(self) - self.batch_size)
batch_index = random_start + torch.range(0, self.batch_size - 1)
index[i * self.batch_size:(i + 1) * self.batch_size] = batch_index
# deal with tail
if tail:
random_start = random.randint(0, len(self) - self.batch_size)
tail_index = random_start + torch.range(0, tail - 1)
index[(i + 1) * self.batch_size:] = tail_index
return iter(index)
def __len__(self):
return self.num_samples
class alignCollate(object):
def __init__(self, imgH=32, imgW=128, keep_ratio=False, min_ratio=1):
self.imgH = imgH
self.imgW = imgW
self.keep_ratio = keep_ratio
self.min_ratio = min_ratio
def __call__(self, batch):
images, labels = zip(*batch)
imgH = self.imgH
imgW = self.imgW
if self.keep_ratio:
ratios = []
for image in images:
w, h = image.size
ratios.append(w / float(h))
ratios.sort()
max_ratio = ratios[-1]
imgW = int(np.floor(max_ratio * imgH))
imgW = max(imgH * self.min_ratio, imgW) # assure imgH >= imgW
transform = resizeNormalize((imgW, imgH))
images = [transform(image) for image in images]
images = torch.cat([t.unsqueeze(0) for t in images], 0)
return images, labels
或INSERTED
表的子查询,则最好使用支持多行操作的语法。
例如,您使用DELETED
如果此子查询(IF ((SELECT ROUTING FROM INSERTED) LIKE 'Assy'...
)返回多行,则SELECT ROUTING FROM INSERTED LIKE 'Assy'
操作将失败。在这种情况下,您一次只能插入或更新一行。所以你不能使用这个查询,例如:
if
答案 1 :(得分:0)
这种方式适用于多行插入/更新,同时仅限于那些ROUTING值。
注意你的交叉应用确实需要一个order by子句,否则你会抓住一个随机行并可能得到意想不到的结果(也可能值得将每个中的*更改为你需要的列的列表,所以右边可以使用索引)。
ALTER TRIGGER [dbo].[WO-A]
ON [dbo].[WORKORDERS]
AFTER UPDATE, INSERT,
AS
BEGIN
SET NOCOUNT ON;
IF TRIGGER_NESTLEVEL() > 1
RETURN
ELSE
UPDATE T1
SET
[Assembly Notes]=T2.[Assembly Notes],
[Assy SO Confirm]=T2.[ASSEMBLY LOOKUP C],
[CALC COLOR]=T2.[COLOR],
[PILOT SIZE]=T2.[PILOT SIZE2],
[NO OF HOLES]=T2.[BOLT HOLES2],
[C/S OD]=T2.[C/S OD2],
[DISC OD]=T2.[DISC OD],
[C/S ANGLE2]=T2.[C/S ANGLE2],
[BH SIZE2]=T2.[BH SIZE2],
[SHORT WO PN]=t2.[CALC STOCK NO],
[CALC OFF-SET]=T2.[CALC OFF-SET],
[RUN OUT LAT]=T2.[RUN OUT LAT],
[RUN OUT RAD]=T2.[RUN OUT RAD],
[BOM SECONDRY DISC]=T2.[BOM - SECONDARY DISC],
[BH SPEC]=T2.[BH SPEC],
[BH CIRCLE2]=T2.[BH CIRCLE2],
[DISC THICKNESS2]=T2.[DISC THICKNESS],
[MRP BOM PARTS 1]=T2.[WHL BOM PART 1 PN],
[MRP BOM PARTS 2]=T2.[WHL BOM PART 2 PN],
[MRP BOM PARTS 3]=T2.[WHL BOM PART 3 PN],
[MRP BOM PARTS 4]=T2.[WHL BOM PART 4 PN],
--[ASSY PN-S]=T2.[CALC STOCK NO],
[SHORT WO PN2]=T2.[CALC STOCK NO],
[WO SALES DESCRIPTION] = T2.[Description for Sales],
--[Assy SO Confirm]=T2.[Assembly Lookup C],
[CUSTOMER PN]=T2.[CUSTOMER PN],
[MRP Wheel]=T2.[BOM WHEEL PN],
[TIRE PN]=T2.[BOM tire],
[TIRE SIZE]=T2.[TIRE SIZE],
[TIRE SUPPLIER]=T22.[Manufacturer],
[TIRE QUANTITY]=CASE WHEN T1.[TIRE PN]!='' THEN T1.[QUANTITY]
WHEN T1.[TIRE PN] IS NOT NULL THEN T1.[QUANTITY]
ELSE ''
END,
[RIM PN (MRP)]=t2.[BOM - RIM PN],
[WO Popularity]=t2.[POPULARITY],
--[RIM QUANTITY]=t2.[q
[WEIGHT]=T2.[unitWeight],--?? CHECK WETHER RIGHT COLUMN
--[ASSY DESC FOR LABEL]
SIZE=T21.[SIZE],
[MRP BLK DISC]=T21.[BLK DISC PN],
[WHEEL PN 1]=NULL,
[WHEEL PN 1-S]=NULL,
[ASSY PN-S]=NULL,
[MO ASSY PN]=null
FROM WORKORDERS T1
INNER JOIN inserted i ON T1.[WORK ORDER #] = i.[WORK ORDER #]
CROSS APPLY (SELECT TOP 1 *
FROM [ASSEMBLIES] T2
WHERE t1.[ASSY PN] = T2.[ASSEMBLY LOOKUP V 1]) T2
CROSS APPLY (SELECT TOP 1 *
FROM [TIRES] T22
WHERE T22.[Tire Lookup H] = T2.[BOM Tire] ) T22
CROSS APPLY (SELECT TOP 1 *
FROM [WHEELS] T21
WHERE T2.[BOM WHEEL] = T21.[WHEEL LOOKUP V] ) T21
WHERE i.ROUTING IN ('Assy', 'L&P', 'ASSY FD', 'ASSY BD', 'RGA')
END