0

有人可以帮忙吗我不知道我做错了什么:

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 不存在的错误

4

2 回答 2

0

该表中没有 RowNum 列。

尝试:

select mm.memberid, mm.aspnetuserid, mm.email, row_number() over (order by (select 1)) as RowNum ....

这应该可以解决您的问题,但我实际上并不推荐这种循环删除的想法。

于 2012-08-28T09:58:27.113 回答
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从不存在的临时表中调用的列。

编辑2。从#MemberIdsToDelete. 您插入的是表而不是临时表。或者,在select into上面添加 # (参见上面的代码)。这将根据需要使其成为临时表。

于 2012-08-28T10:04:09.083 回答