2012-08-29 66 views
0

我有一個包含memberId的表,但它們並不同時運行。儘管表沒有併發編號

我該如何循環瀏覽它們?

我有這樣的:

select mm.memberid, mm.aspnetuserid, mm.email 
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 

ALTER TABLE #MemberIdsToDelete ADD id int NOT NULL AUTO_INCREMENT 

DECLARE @MaxRownum int 
SET @MaxRownum = (SELECT MAX(id) FROM MemberIdsToDelete) 

WHILE @Iter <= @MaxRownum 
-- do things 
SET @Iter = @Iter + 1 
END 

以上是不正確的,我在ALTER TABLE線得到錯誤很明顯: 附近有語法錯誤AUTO_INCREMENT「

+2

爲什麼你需要通過他們循環逐行? 「做什麼」需要做什麼?如果可能,您應該在SQL中使用基於集合的操作。你也需要'IDENTITY'而不是'AUTO_INCREMENT'。你可以使用'select IDENTITY(INT,1,1)AS ID,mm.memberid,mm.aspnetuserid,mm.email INTO ....'在一個操作中完成。 –

+0

併發?難道你的意思是「持續」? – Tomalak

+0

我必須遍歷每一行才能獲取memberid,這是不連續的,所以我可以從memberId匹配的所有其他表中刪除。 –

回答

0

您可以通過表使用遊標循環,這樣的事情:

DECLARE db_cursor CURSOR FOR SELECT Col1, Col2, -- your select query 

OPEN db_cursor 
FETCH NEXT FROM db_cursor INTO @Var1, @Var2, -- list of your variables based on select query 

WHILE @@FETCH_STATUS = 0 
BEGIN 

    -- do stuff 

    FETCH NEXT FROM db_cursor INTO @Var1, @Var2, -- list of your variables based on select query 
END 

CLOSE db_cursor 
DEALLOCATE db_cursor 
+1

你可以,但你不應該,除非你絕對不能避免它。 – Tomalak

+0

看起來像這就是我需要做的事情,我的操作將是一次性的,清除所有對其他表的引用的舊記錄。有大約14個表受到影響,我有180378個記錄..eish –

+0

@charlie_cat然後它看起來像這不是你想要做的。 2525292什麼時候做什麼,什麼時候可以做14。數據庫將找出如何最好地處理這些2525292事情 –

1

ALTER TABLE改變實際的表,而不是數據。在添加數據後添加一列沒有意義,所以它可能不是你想要的。

你可以通過任何結果集與遊標,但也有有關遊標兩個重要的規則循環:

  1. 切勿使用遊標時,你可以做一組操作。
  2. 你可能不需要一個光標用於你正在考慮的事情,重新考慮一段時間以確保你需要它。 CURSOR表示您正在考慮像SQL程序代碼問題一樣的SQL問題 - 它可能是唯一的方法,但要避免它(即使做了其他事情,如果您沒有看到集合方法,請在午飯後回來) 。

在你說你需要從其他表刪除的意見,那將是更好的多內斯爲:

基於集合
delete from table1 where memberId in 
(
select mm.memberid 
    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 <> '' 
) 
delete from table2 where memberId in 
(
select mm.memberid 
    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 <> '' 
) 
delete from table3 where memberId in 
(
select mm.memberid 
    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 <> '' 
) 

三種操作,其本身固有地更快,是東西查詢分析器能爲你做更好的工作,最好的是編寫起來更簡單。

0

也許ROW_NUMBER()是更多你想要的+我剛剛relected了很多的意見到OP提出:

SELECT 
     IDENTITY(INT,1,1) AS ID, 
     ROW_NUMBER() OVER (ORDER BY mm.memberid) AS RowN, 
     mm.memberid, 
     mm.aspnetuserid, 
     mm.email 
    INTO #MemberIdsToDelete  
    FROM 
     membership.members mm  
     INNER JOIN aspnet_membership asp  
      ON mm.aspnetuserid=asp.userid  
     INNER JOIN trade.tradesmen tr  
      ON tr.memberid=mm.memberid  
    WHERE 
     asp.isapproved <> 0 
     AND tr.importDPN <> ''  


    DECLARE @MaxRownum INT = (SELECT MAX(RowN) FROM MemberIdsToDelete) 
    DECLARE @Iter INT = 0 

    WHILE @Iter <= @MaxRownum 
     -- do things 
     SET @Iter = @Iter + 1 
    END 
相關問題