在我的代码的早期,我已经声明了一个变量(以及其他变量):
DECLARE @ZIPFk int;
然后,不久之后,我尝试使用包含两个连接的选择表达式设置上述变量的值:
SET @ZIPFk=(SELECT Zip.id
FROM Zip
INNER JOIN Zip5 ON Zip5.id=Zip.Zip5Fk
JOIN PlaceName ON Zip5.PlaceNameFk=PlaceName.id
WHERE PlaceName.PlaceName=@USPSComm AND Zip5.ZipCode5=@ZIPCode)
它没有工作,我无法弄清楚原因。我的结果表在字段中只有一个NULL值,我希望/希望填充@ZIPFk值。当自己运行时,除了声明的变量之外,SELECTION表达式确实会产生一个具有单行/单列/单值的表。
以下是使用声明的变量查看实际UPDATE发生的查询结尾:
UPDATE CompleteSubaddressZip
SET ZipFk=@ZIPFk
FROM CompleteSubaddressZip as a
JOIN CompleteSubaddress AS b ON b.id=a.CompleteSubaddressFk
WHERE (b.AddressUUID=@SelectedUUID)
而且......这是对整个事情的看法:
USE MyDatabase;
DECLARE @USPSComm varchar = 'Springville' --<--Enter USPS ZIP community name here
DECLARE @ZIPCode varchar = '99221' --<--Enter USPS ZIP Code here
DECLARE @RowsToProcess int;
DECLARE @ZIPFk int;
DECLARE @CurrentRow int;
DECLARE @SelectedUUID uniqueidentifier;
DECLARE @UUIDTable table (RowID int not null primary key identity (1,1), UUID uniqueidentifier);
SET @ZIPFk=(SELECT a.id
FROM dbo.Zip AS a
INNER JOIN Zip5 as b ON b.id=a.Zip5Fk
JOIN PlaceName as c ON b.PlaceNameFk=c.id
WHERE c.PlaceName=@USPSComm AND b.ZipCode5=@ZIPCode)
INSERT INTO @UUIDTable (UUID) VALUES
( 'f4x6faf4-bxx2-4166-bcd6-88c9af8ab546'), ('3e20xx5e-5edc-4735-8962-5ab3c50d053e'),
('696xxB2D-50DE-4361-807D-48xxD2AC5CxB')
, ('c5879x41-e144-4ecc-8056-5493exxx1a5'), ('af7993cf-2xec-4c5f-b02e-92cb6dxxxf2b')
SET @RowsToProcess =@@ROWCOUNT
SET @CurrentRow=0
WHILE @CurrentRow<@RowsToProcess
BEGIN
SET @CurrentRow =@CurrentRow+1
SELECT
@SelectedUUID=UUID
FROM @UUIDTable
WHERE RowID=@CurrentRow
UPDATE CompleteSubaddressZip
SET ZipFk=@ZIPFk
FROM CompleteSubaddressZip as a
JOIN CompleteSubaddress AS b ON b.id=a.CompleteSubaddressFk
WHERE (b.AddressUUID=@SelectedUUID)
END
答案 0 :(得分:0)
DECLARE @ZIPCode varchar = '99221' --<--Enter USPS ZIP Code here
DECLARE @RowsToProcess int;
DECLARE @ZIPFk int;
DECLARE @CurrentRow int;
DECLARE @SelectedUUID uniqueidentifier;
DECLARE @UUIDTable table (RowID int not null primary key identity (1,1), UUID uniqueidentifier);
SELECT TOP 1 @ZIPFk = Zip.id
FROM Zip
INNER JOIN Zip5 ON Zip5.id=Zip.Zip5Fk
JOIN PlaceName ON Zip5.PlaceNameFk=PlaceName.id
WHERE PlaceName.PlaceName=@USPSComm AND Zip5.ZipCode5=@ZIPCode
INSERT INTO @UUIDTable (UUID) VALUES
( 'f4x6faf4-bxx2-4166-bcd6-88c9af8ab546'), ('3e20xx5e-5edc-4735-8962-5ab3c50d053e'),
('696xxB2D-50DE-4361-807D-48xxD2AC5CxB')
, ('c5879x41-e144-4ecc-8056-5493exxx1a5'), ('af7993cf-2xec-4c5f-b02e-92cb6dxxxf2b')
UPDATE CompleteSubaddressZip
SET ZipFk=@ZIPFk
FROM
CompleteSubaddressZip as a
INNER JOIN CompleteSubaddress AS b
ON b.id=a.CompleteSubaddressFk
INNER JOIN @UUIDTable u
ON b.AddressUUID=u.UUID
答案 1 :(得分:0)
我向任何可能阅读此主题的人道歉:我摸索着并且事实上我并不确定我已经改变了什么以纠正我的问题,这使得这个帖子几乎无用。
然而,对于后代,这是我的最终(?)查询,它实际上按预期工作。 (我非常感谢用户的评论&#34; Matt&#34;谁帮我清理了东西,至少让我走上正轨,告诉我什么时候我很傻)。我拿出了循环&#39;并删除了不必要的变量,并通常简化了一切:
USE MyDatabase;
DECLARE @USPSComm varchar (20)='Springfield'; -- <--Enter USPS ZIP community name here
DECLARE @ZIPCode varchar (5)='97477'; -- <--Enter USPS ZIP Code here
DECLARE @ZIPFk int;
SET @ZIPFk=(SELECT x.id
FROM Zip AS x
INNER JOIN Zip5 as y ON y.id=x.Zip5Fk
JOIN PlaceName as z ON y.PlaceNameFk=z.id
WHERE z.PlaceName=@USPSComm AND y.ZipCode5=@ZIPCode);
UPDATE a
SET ZipFk=@ZIPFk
FROM CompleteSubaddressZip as a
JOIN CompleteSubaddress AS b ON b.id=a.CompleteSubaddressFk
JOIN Zip as c ON a.ZipFk=c.id
JOIN Zip5 as d ON c.Zip5Fk=d.id
JOIN PlaceName as e ON d.PlaceNameFk=e.id
WHERE b.AddressUUID IN ('xxxxxxxx-xxxx-UUID-xxxx-xxxxxxxxxxxx'
, 'xxxxxxxx-xxxx-UUID-xxxx-xxxxxxxxxxxx'
, 'xxxxxxxx-xxxx-UUID-xxxx-xxxxxxxxxxxx'
, 'xxxxxxxx-xxxx-UUID-xxxx-xxxxxxxxxxxx'
, 'xxxxxxxx-xxxx-UUID-xxxx-xxxxxxxxxxxx');