有人可以帮助我,我不知道我做错了什么:
IF EXISTS ( SELECT name
FROM sys.tables
WHERE name = N'MemberIdsToDelete' )
DROP TABLE [MemberIdsToDelete];
GO
SELECT mm.memberid ,
mm.aspnetuserid ,
mm.email ,
mm.RowNum AS RowNum
INTO #MemberIdsToDelete
FROM membership.members AS mm
LEFT JOIN aspnet_membership AS asp ON mm.aspnetuserid = asp.userid
LEFT JOIN trade.tradesmen AS tr ON tr.memberid = mm.memberid
WHERE asp.isapproved = 0
AND tr.ImportDPN IS NOT NULL
AND tr.importDPN <> ''
ORDER BY mm.memberid
DECLARE @MaxRownum INT
SET @MaxRownum = ( SELECT MAX(RowNum)
FROM #MemberIdsToDelete
)
DECLARE @Iter INT
SET @Iter = ( SELECT MIN(RowNum)
FROM #MemberIdsToDelete
)
DECLARE @MemberId INT
DECLARE @TrademId INT
DECLARE @UId UNIQUEIDENTIFIER
DECLARE @Successful INT
DECLARE @OutputMessage VARCHAR(200)
DECLARE @Email VARCHAR(100)
DECLARE @Username VARCHAR(100)
SELECT @MemberId = memberId ,
@UId = AspNetUserId
FROM MemberIdsToDelete
SELECT @TrademId = TradesManId
FROM trade.TradesMen
WHERE memberId = @MemberId;
WHILE @Iter <= @MaxRownum
BEGIN
SELECT *
FROM #MemberIdsToDelete
WHERE RowNum = @Iter
--more code here
SET @Iter = @Iter + 1
END
我只想检查我的表MemberIdsToDelete是否存在,如果是这样的话, 使用select中的结果集创建MemberIdsToDelete 循环遍历MemberIdsToDelete表并执行操作
我收到RowNum不存在的错误
答案 0 :(得分:0)
该表中没有RowNum列。
尝试:
选择mm.memberid,mm.aspnetuserid,mm.email,row_number()over(order by(select 1))as RowNum ....
这应该可以解决你的问题,但我实际上并不推荐这种循环删除的想法。
答案 1 :(得分:0)
首先,要检查表是否存在然后相应地删除,您需要使用类似
的内容IF EXISTS (SELECT name
FROM sys.tables
WHERE name = N'MemberIdsToDelete')
DROP TABLE [MemberIdsToDelete];
GO
对于错误,当您尝试引用它时,您的RowNum列不存在。将其包含在SELECT语句
中select mm.memberid, mm.aspnetuserid, mm.email, mm.RowNum AS RowNum
into #MemberIdsToDelete
from membership.members as mm
left join aspnet_membership as asp
on mm.aspnetuserid=asp.userid
left join trade.tradesmen as tr
on tr.memberid=mm.memberid
where asp.isapproved = 0 and tr.ImportDPN IS NOT NULL
and tr.importDPN <> ''
order by mm.memberid;
GO
我希望这会有所帮助。
编辑。基于您的评论中的其他错误。您现在正在尝试访问不存在的临时表。在尝试从中读取临时表之前,必须首先填充临时表#MemberIdsToDelete
。无效的列错误归结为同样的问题。您正在尝试从临时表中读取一个名为RowNum
的列,该列不存在。
EDIT2。从#MemberIdsToDelete
中删除“#”。您正在插入表而不是临时表。或者,在上面的select into
中添加#(请参阅上面的代码)。这将使其成为一个临时表。