我遇到了ELSE语法错误,非常感谢第二眼向我展示什么地方不对!我正在使用SSMS v17.3。我收到的消息是:
第156条消息,状态15,状态1,第98行
关键字“ ELSE”附近的语法不正确。
第98行是出发前的最后一个ELSE。整个语句结尾上方大约6行。谢谢高级!
DECLARE @GroupID uniqueidentifier
DECLARE @fCompID int
DECLARE @fPropID int
DECLARE @fCompGUID uniqueidentifier
Set @GroupID = 'E63DC5E7-C8C8-4EA3-B1BF-75712DD83EF4'
SET @fCompID = '0'
SET @fPropID = '0'
WHILE @fCompID <= '999'
BEGIN
IF @fCompID = (SELECT fID FROM tSCCompany WHERE fID = @fCompID)
SET @fCompGUID = (SELECT fCompanyID FROM tSCCompany WHERE fID = @fCompID)
WHILE @fPropID <='999'
BEGIN
IF @fPropID = (SELECT fID FROM tSCProperty WHERE fID = @fPropID AND fCompanyID = @fCompGUID)
BEGIN
INSERT INTO zPropTest(fGroupID, fPropertyID)
Select @GroupID, (select fPropertyID from tSCProperty where fID = @fPropID AND fCompanyID = @fCompGUID)
SET @fPropID = @fPropID + 1
END
ELSE
BEGIN
SET @fPropID = @fPropID + 1
END
END
ELSE
BEGIN
SET @fCompID = @fCompID + 1
SET @fPropID = '0'
END
END
GO
答案 0 :(得分:0)
我认为您错过了前1名
DECLARE @GroupID uniqueidentifier
DECLARE @fCompID int
DECLARE @fPropID int
DECLARE @fCompGUID uniqueidentifier
Set @GroupID = 'E63DC5E7-C8C8-4EA3-B1BF-75712DD83EF4'
SET @fCompID = '0'
SET @fPropID = '0'
WHILE @fCompID <= '999'
BEGIN
IF @fCompID = (SELECT fID FROM tSCCompany WHERE fID = @fCompID) -- Need top 1?
SET @fCompGUID = (SELECT fCompanyID FROM tSCCompany WHERE fID = @fCompID) -- Need top 1?
WHILE @fPropID <='999'
BEGIN
IF @fPropID = (SELECT fID FROM tSCProperty WHERE fID = @fPropID AND fCompanyID = @fCompGUID) -- Need top 1?
BEGIN
INSERT INTO zPropTest(fGroupID, fPropertyID)
Select @GroupID,
(select top 1 fPropertyID from tSCProperty
where fID = @fPropID AND fCompanyID = @fCompGUID)
SET @fPropID = @fPropID + 1
END
ELSE
BEGIN
SET @fPropID = @fPropID + 1
END
END
ELSE
BEGIN
SET @fCompID = @fCompID + 1
SET @fPropID = '0'
END
END
GO
答案 1 :(得分:0)
似乎您缺少开始块之一。请尝试如下:
DECLARE @GroupID uniqueidentifier
DECLARE @fCompID int
DECLARE @fPropID int
DECLARE @fCompGUID uniqueidentifier
Set @GroupID = 'E63DC5E7-C8C8-4EA3-B1BF-75712DD83EF4'
SET @fCompID = '0'
SET @fPropID = '0'
BEGIN-- MISSING!!
WHILE @fCompID <= '999'
BEGIN
IF @fCompID = (SELECT fID FROM tSCCompany WHERE fID = @fCompID)
SET @fCompGUID = (SELECT fCompanyID FROM tSCCompany WHERE fID = @fCompID)
WHILE @fPropID <='999'
BEGIN
IF @fPropID = (SELECT fID FROM tSCProperty WHERE fID = @fPropID AND fCompanyID = @fCompGUID)
BEGIN
INSERT INTO zPropTest(fGroupID, fPropertyID)
Select @GroupID, (select fPropertyID from tSCProperty where fID = @fPropID AND fCompanyID = @fCompGUID)
SET @fPropID = @fPropID + 1
END
ELSE
BEGIN
SET @fPropID = @fPropID + 1
END
END
END-- MISSING
ELSE
BEGIN
SET @fCompID = @fCompID + 1
SET @fPropID = '0'
END
END
GO
答案 2 :(得分:0)
欢迎堆栈溢出!
您的If
语句需要块指示符。看到我添加的两行
DECLARE @GroupID uniqueidentifier
DECLARE @fCompID int
DECLARE @fPropID int
DECLARE @fCompGUID uniqueidentifier
Set @GroupID = 'E63DC5E7-C8C8-4EA3-B1BF-75712DD83EF4'
SET @fCompID = '0'
SET @fPropID = '0'
WHILE @fCompID <= '999'
BEGIN
IF @fCompID = (SELECT fID FROM tSCCompany WHERE fID = @fCompID)
Begin -- I added this
SET @fCompGUID = (SELECT fCompanyID FROM tSCCompany WHERE fID = @fCompID)
WHILE @fPropID <='999'
BEGIN
IF @fPropID = (SELECT fID FROM tSCProperty WHERE fID = @fPropID AND fCompanyID = @fCompGUID)
BEGIN
INSERT INTO zPropTest(fGroupID, fPropertyID)
Select @GroupID, (select fPropertyID from tSCProperty where fID = @fPropID AND fCompanyID = @fCompGUID)
SET @fPropID = @fPropID + 1
END
ELSE
BEGIN
SET @fPropID = @fPropID + 1
END
End
End -- I added this, too
ELSE
BEGIN
SET @fCompID = @fCompID + 1
SET @fPropID = '0'
END
END
GO