DECLARE @TOTAL AS INTIF OBJECT_ID('TEMPDB..#TEMP1') IS NOT NULLDROP TABLE #TEMP1;SELECT A.*, ROW_NUMBER() OVER (ORDER BY CUSTOMER_ID) AS ROWIDINTO #TEMP1 FROM Table1 ASELECT @TOTAL = COUNT(*) FROM #TEMP1DECLARE @COUNTER AS INT, @VAL AS INTSET @COUNTER = 1SET @VAL = 1DECLARE @CURRENTVAL AS VARCHAR(255)DECLARE @CURRENTCUSTOMER AS VARCHAR(255)DECLARE @CURRENTCUSTOMERENCOUNTER AS VARCHAR(255)DECLARE @COMPAREVAL AS VARCHAR(255)WHILE (@COUNTER <= @TOTAL) BEGIN -- GET ROW FROM #TEMP1 SELECT @CURRENTCUSTOMER = CUSTOMER_ID, @CURRENTVAL = LEVEL_ID, @CURRENTCUSTOMERDEPT = DEPT_ID FROM #TEMP1 WHERE ROWID = @COUNTER IF (@CURRENTVAL LIKE '%UltraSound%') UPDATE [Table1] SET EVENT_GROUP = 'Group'+CAST(@VAL AS VARCHAR(10)) WHERE CUSTOMER_ID = @CURRENTCUSTOMER AND DEPT_ID = @CURRENTCUSTOMERDEPT AND LEVEL_ID = @CURRENTVAL ELSE SET @VAL = @VAL + 1 SET @COUNTER = @COUNTER + 1END 这是我运行此代码后得到的(这是错误的) )this is what I got after running this code (it is wrong)customer_iddept_idlevel_idevent_group222300300UltraSound1Group1222300300UltraSound1Group1222300300LabNULL222300300LabNULL222300300UltraSound2Group3222300300UltraSound2Group3222300300UltraSound2Group3444400400Intensive CareNULL444400400UltraSound4Group6444400400UltraSound5Group4444400400UltraSound6Group4444400400CardiologyNULL444400400CardiologyNULL444400400UltraSound4Group6444400400UltraSound4Group6444400400XrayNULL推荐答案 1)从SOURCE GROUP BY中选择level_id level_id 2)加入SOURCE和Group+ ROWNUMBER()来自(1) 然后是NULL的东西。1) Select level_id from SOURCE GROUP BY level_id2) Join SOURCE and "Group" + ROWNUMBER() from (1)Then the NULL thing.DECLARE @TotalRows INT, @CurrentRow INTDECLARE @TransID INTIF OBJECT_ID('tempdb..#QcAccounts') IS NOT NULL DROP TABLE #QcAccountscreate table #QcAccounts(RowID INT IDENTITY ( 1 , 1 ),TransID INT)INSERT #QcAccountsSELECT TransID FROM AgentTransactionSET @TotalRows = @@ROWCOUNTSET @CurrentRow = 1WHILE (@CurrentRow <= @TotalRows)BEGINSELECT @TransID = TransIDFROM #QcAccountsWHERE RowID = @CurrentRowprint @TransID----------------------------------QcTransaction--------------------------------------------------------IF NOT EXISTS ( SELECT TransID FROM QcTransaction WHERE TransID = @TransID )BEGININSERT INTO QcTransaction(TransID,AllotedDate) VALUES(@TransID,getdate())--UPDATE AgentTransaction SET AuditStatusID = dbo.udfGetAuditStatusID('ALT')--WHERE TransID = @TransIDEND-------------------------------------------------------------------------------------------------------SET @CurrentRow = @CurrentRow + 1END 这篇关于虽然SQL中的循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 01:38