我需要得到比我更了解SQL的人的帮助...... 我正在尝试进行单个SP调用,使用IF THEN ELSE但仍然得到三个"多部分标识符无法绑定"消息。
页面可以由商家或集团赞助。 在advertEntityType字段中,我有一个" B"或者" G"对于企业或团体。 在advertEntityID字段中,我有businessID或groupID。
SELECT
Advertisements.advertEntityType
,Advertisements.advertEntityID
FROM Advertisements
WHERE Advertisements.advertPageName = @pageName
IF (Advertisements.advertEntityType = 'G')
SELECT
Group.groupID
,Group.groupName
,Group.groupPhone
,Group.groupWebsite
,Group.groupLogoName
FROM Group
WHERE Group.groupID = Advertisements.advertEntityID
ELSE
SELECT
Business.businessID
,Business.businessName
,Business.businessWorkPhone
,Business.businessWebsite
,Business.businessLogoName
FROM Business
WHERE Business.businessID = Advertisements.advertEntityID
当我尝试执行时,我收到这些消息,我似乎无法理解......
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 36
The multi-part identifier "Advertisements.advertEntityType" could not be bound.
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 46
The multi-part identifier "Advertisements.advertEntityID" could not be bound.
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 58
The multi-part identifier "Advertisements.advertEntityID" could not be bound.
答案 0 :(得分:0)
Advertisements.advertEntityType
是表名和列名。如果要在if语句中使用它们,则需要将这些值分配给局部变量。如果我理解正确,你想根据第一个select语句中的条件进行评估。在这种情况下,您应该使用此类查询:
DECLARE @entityType int --assuming the type of Advertisements.advertEntityType is int
DECLARE @advertEntityID int --assuming the type of Advertisements.advertEntityID is int
SELECT
@entityType = Advertisements.advertEntityType,
@advertEntityID = Advertisements.advertEntityID
FROM Advertisements
WHERE Advertisements.advertPageName = @pageName
IF (@entityType = 'G')
SELECT
Group.groupID
,Group.groupName
,Group.groupPhone
,Group.groupWebsite
,Group.groupLogoName
FROM Group
WHERE Group.groupID = @advertEntityID
ELSE
SELECT
Business.businessID
,Business.businessName
,Business.businessWorkPhone
,Business.businessWebsite
,Business.businessLogoName
FROM Business
WHERE Business.businessID = @advertEntityID