这可能很简单,但我一直无法搞清楚。 我有一段代码(类似于下面的代码),我通过select为一堆变量分配一堆值,它完成了工作,但是当我运行它时,我得到的结果集导致我的SSMS崩溃了避免这种情况的方法......
DECLARE @Name VARCHAR(100)
DECLARE @LastName VARCHAR(100)
DECLARE @Address VARCHAR(100)
SELECT TOP 1
@Name = Name
@LastName = LastName
@Address = Address
From Person.Address
Where Name = 'Name'
Order By ID
我在一个循环中使用上面的代码,我处理大约3-400K行并将这些变量传递给存储过程,每个top 1 select语句抛出一个结果集导致我的SSMS崩溃我真的不需要select要显示的前1个值,任何想法如何摆脱这个?....
非常感谢任何帮助。
---如下面所要求的那样是代码,我已经删除了一些东西,但这就是它,我从Query Analayzer运行它,因为这只是一次进程所以我们不需要创建SP。
DECLARE @retstat INT
DECLARE @Name VARCHAR(255)
DECLARE @Lang TINYINT
DECLARE @Address CHAR(10)
DECLARE @ID INT
DECLARE @BranchSeqNo INT
DECLARE @AddressCode VARCHAR(10)
DECLARE @Address1 VARCHAR(50)
DECLARE @City VARCHAR(30)
DECLARE @State VARCHAR(3)
DECLARE @PostalCode VARCHAR(15)
DECLARE @Country VARCHAR(30)
SET @ID = NULL
UPDATE RAWClaimTable Set Processed = 0 where Processed is null
UPDATE RAWClaimTable SET ErrorCode = NULL ,ErrorMessage = NULL ,Processed = 0
WHERE ErrorMessage IS NOT NULL AND CLAIMID is null
WHILE EXISTS ( SELECT *
FROM RAWClaimTable
WHERE Processed = 0 )
BEGIN
-----Initialize Default Variables
SET @Lang = 0
SET @Address = 'Import'
SET @SaveMode = 0
SET @ID = Null
SELECT TOP 1
@LossDate = LossDate ,
@ClaimDate = ClaimDate ,
@OpenDate = OpenDate ,
@Receivedate = ReceiVeDate ,
@Name = Name ,
@Address = Address ,
@Address1 = Address1 ,
@City = City ,
@State = State ,
@PostalCode = PostalCode ,
@Country = Country
FROM RAWClaimTable
WHERE Processed = 0
ORDER BY ClaimID
BEGIN TRY
EXEC @RetStat = Usp_ProcessRawData @Lang, @Address, @SaveMode, @ID OUT,
@BranchSeqNo, @OriginalBranchSeqNo, @IncidentID,
@AssignmentTypeCode, @PartnershipID, @AccountID,
END TRY
BEGIN CATCH
SELECT @RetStat = @@Error
if @@Trancount > 0 rollback tran
IF @RetStat != 0
BEGIN
update RAWClaimTable set Errormessage = ERROR_MESSAGE() where ClaiMKey = @Name
END
END CATCH
IF @ID IS NOT NULL
BEGIN
UPDATE RAWClaimTable
SET ClaimID = @ID ,
Processed = 1
WHERE ClaiMKey = @Name
END
ELSE
BEGIN
UPDATE RAWClaimTable
SET Processed = 1
WHERE ClaiMKey = @Name
END
END
答案 0 :(得分:0)
使用CURSOR循环你的行!
SET @Lang = 0
SET @Address = 'Import'
SET @SaveMode = 0
SET @ID = Null
DECLARE my_cursor CURSOR FOR
SELECT LossDate, ClaimDate, OpenDate, ReceiVeDate, Name, Address,
Address1, City, State, PostalCode, Country
FROM RAWClaimTable
WHERE Processed = 0
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO @LossDate, @ClaimDate, @OpenDate, @Receivedate, @Name, @Address,
@Address1, @City, @State, @PostalCode, @Country
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXEC @RetStat = Usp_ProcessRawData @Lang, @Address, @SaveMode, @ID OUT,
@BranchSeqNo, @OriginalBranchSeqNo, @IncidentID,
@AssignmentTypeCode, @PartnershipID, @AccountID,
END TRY
BEGIN CATCH
SELECT @RetStat = @@Error
if @@Trancount > 0 rollback tran
IF @RetStat != 0
BEGIN
update RAWClaimTable set Errormessage = ERROR_MESSAGE()
where ClaiMKey = @Name
END
END CATCH
IF @ID IS NOT NULL
BEGIN
UPDATE RAWClaimTable
SET ClaimID = @ID ,
Processed = 1
WHERE ClaiMKey = @Name
END
ELSE
BEGIN
UPDATE RAWClaimTable
SET Processed = 1
WHERE ClaiMKey = @Name
END
FETCH NEXT FROM my_cursor
INTO @LossDate, @ClaimDate, @OpenDate, @Receivedate, @Name, @Address,
@Address1, @City, @State, @PostalCode, @Country
END