Can someone help please I dont know what I am doing wrong:
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
I just want to check if my table MemberIdsToDelete exists, if so drop it,
create MemberIdsToDelete with the results set from the select
loop through MemberIdsToDelete table and perform operations
I am getting error that RowNum does not exist
For a start, to check if a table exists and then drop accordingly, you need to use something like
as for the error, your RowNum column does not exist when you are attempting to reference it. Include it in the SELECT statement
I hope this helps.
Edit. Based on you additional error from your comment. You are now attempting to access a temporary table that does not exist. You must first populate the temporary table
#MemberIdsToDeletebefore attempting to read from it. The invalid column error is down to the same problem. You are attempting to read a column calledRowNumfrom the temporary table which does not exist.Edit2. Remove the ‘#’ from the
#MemberIdsToDelete. You are inserting into a table not a temporary table. Or, Add a # to theselect intoabove (see the code above). This will make it a temporary table as required.