While Looping Query gives Exception out of memoryWeb Service Out of Memory Exception while filling ADO.Net DataSetDo while loop in SQL Server 2008Loop in Sql query (While , For)WHILE loop in a WHERE queryOut of Memory Exception running a large Insert scriptException out of memory from AndroidWhile loop is showing time out error in sqlWhile loop - out of memory errorWhile LOOP IN Dynamic QUERY
How can I effectively communicate to recruiters that a phone call is not possible?
How can I fix the dull colors I am getting in Ubuntu 19.04 Terminal?
How can I get a player to accept that they should stop trying to pull stunts without thinking them through first?
How to befriend private nested class?
Is anyone advocating the promotion of homosexuality in UK schools?
Swapping "Good" and "Bad"
Does throwing a penny at a train stop the train?
What happens to unproductive professors?
When did "&" stop being taught alongside the alphabet?
Why did Harry Potter get a bedroom?
Are there any sports for which the world's best player is female?
What does the phrase "head down the rat's hole" mean here?
Should disabled buttons give feedback when clicked?
Which star / galaxy is moving away from us the fastest?
Convert BAM to properly paired FASTQ files
Does a wizard need their hands free in order to cause their familiar from the Find Familiar spell to reappear?
Diagonal arrows (using TikZ) should be aligned in parallel
Singleton with an object which throws in the ctor - accessing again?
Is there any word for "disobedience to God"?
Can Jimmy hang on his rope?
Do I have a right to cancel a purchase of foreign currency in the UK?
Salt, pepper, herbs and spices
Why return a static pointer instead of an out parameter?
Confirming the Identity of a (Friendly) Reviewer After the Reviews
While Looping Query gives Exception out of memory
Web Service Out of Memory Exception while filling ADO.Net DataSetDo while loop in SQL Server 2008Loop in Sql query (While , For)WHILE loop in a WHERE queryOut of Memory Exception running a large Insert scriptException out of memory from AndroidWhile loop is showing time out error in sqlWhile loop - out of memory errorWhile LOOP IN Dynamic QUERY
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I need to loop through all the data from the table result. For example select result got a column call Quantity. I need the Column value to do the looping. But I get out of memory exception during looping.
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
SET @COUNT = 1
WHILE exists (Select * from #tempOpening)
BEGIN
SELECT top 1 @Qty = QTY, @itemcode = ItemCode, @GRNNo = GRNNo, @Cost=UnitPrice
, @PurchaseDate=PurchaseDate, @UOM=UOM, @Weight = Weight
from #tempOpening order by ItemCode
print @Qty
print @itemcode
WHILE @Qty>0
BEGIN
SELECT @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST(@COUNT as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate,
'' TestData,
''TestRemark,
'HQ' Location,
@UOM BuyType,
1 qty,
@Weight Weight,
@Cost Cost,
0 PackingFee,
'' Remarks,
@GRNNo as GRNNO,
null returnDate,
'SATO' createdBy,
GETDATE() createDate,
null CreatedDate,
null ModifiedDate,
null SaNo,
@Qty OriginalQty,
@Weight
SET @COUNT = @COUNT+1
SET @Qty = @Qty -1
END
print 'Out loop'
DELETE FROM #tempOpening where ItemCode= @itemcode and GRNNo = @GRNNo
END
|
show 6 more comments
I need to loop through all the data from the table result. For example select result got a column call Quantity. I need the Column value to do the looping. But I get out of memory exception during looping.
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
SET @COUNT = 1
WHILE exists (Select * from #tempOpening)
BEGIN
SELECT top 1 @Qty = QTY, @itemcode = ItemCode, @GRNNo = GRNNo, @Cost=UnitPrice
, @PurchaseDate=PurchaseDate, @UOM=UOM, @Weight = Weight
from #tempOpening order by ItemCode
print @Qty
print @itemcode
WHILE @Qty>0
BEGIN
SELECT @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST(@COUNT as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate,
'' TestData,
''TestRemark,
'HQ' Location,
@UOM BuyType,
1 qty,
@Weight Weight,
@Cost Cost,
0 PackingFee,
'' Remarks,
@GRNNo as GRNNO,
null returnDate,
'SATO' createdBy,
GETDATE() createDate,
null CreatedDate,
null ModifiedDate,
null SaNo,
@Qty OriginalQty,
@Weight
SET @COUNT = @COUNT+1
SET @Qty = @Qty -1
END
print 'Out loop'
DELETE FROM #tempOpening where ItemCode= @itemcode and GRNNo = @GRNNo
END
Is it getting stuck in the inner loop or the outer loop?
– Dale Burrell
Mar 26 at 1:08
2
any particular reason why you are usingLOOPand not set-based solution ?
– Squirrel
Mar 26 at 1:10
1
Your loop will go forever if either of ItemCode or GRNNo are null
– Dale Burrell
Mar 26 at 1:12
@DaleBurrell my loop will end, if i export the result to file it work.
– Woon Lai
Mar 26 at 1:28
1
@WoonLai That's not a good reason to use a loop. You can still do it as a set.
– Joel Coehoorn
Mar 26 at 1:40
|
show 6 more comments
I need to loop through all the data from the table result. For example select result got a column call Quantity. I need the Column value to do the looping. But I get out of memory exception during looping.
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
SET @COUNT = 1
WHILE exists (Select * from #tempOpening)
BEGIN
SELECT top 1 @Qty = QTY, @itemcode = ItemCode, @GRNNo = GRNNo, @Cost=UnitPrice
, @PurchaseDate=PurchaseDate, @UOM=UOM, @Weight = Weight
from #tempOpening order by ItemCode
print @Qty
print @itemcode
WHILE @Qty>0
BEGIN
SELECT @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST(@COUNT as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate,
'' TestData,
''TestRemark,
'HQ' Location,
@UOM BuyType,
1 qty,
@Weight Weight,
@Cost Cost,
0 PackingFee,
'' Remarks,
@GRNNo as GRNNO,
null returnDate,
'SATO' createdBy,
GETDATE() createDate,
null CreatedDate,
null ModifiedDate,
null SaNo,
@Qty OriginalQty,
@Weight
SET @COUNT = @COUNT+1
SET @Qty = @Qty -1
END
print 'Out loop'
DELETE FROM #tempOpening where ItemCode= @itemcode and GRNNo = @GRNNo
END
I need to loop through all the data from the table result. For example select result got a column call Quantity. I need the Column value to do the looping. But I get out of memory exception during looping.
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
SET @COUNT = 1
WHILE exists (Select * from #tempOpening)
BEGIN
SELECT top 1 @Qty = QTY, @itemcode = ItemCode, @GRNNo = GRNNo, @Cost=UnitPrice
, @PurchaseDate=PurchaseDate, @UOM=UOM, @Weight = Weight
from #tempOpening order by ItemCode
print @Qty
print @itemcode
WHILE @Qty>0
BEGIN
SELECT @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST(@COUNT as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate,
'' TestData,
''TestRemark,
'HQ' Location,
@UOM BuyType,
1 qty,
@Weight Weight,
@Cost Cost,
0 PackingFee,
'' Remarks,
@GRNNo as GRNNO,
null returnDate,
'SATO' createdBy,
GETDATE() createDate,
null CreatedDate,
null ModifiedDate,
null SaNo,
@Qty OriginalQty,
@Weight
SET @COUNT = @COUNT+1
SET @Qty = @Qty -1
END
print 'Out loop'
DELETE FROM #tempOpening where ItemCode= @itemcode and GRNNo = @GRNNo
END
edited Mar 26 at 1:02
Dale Burrell
4,1795 gold badges27 silver badges57 bronze badges
4,1795 gold badges27 silver badges57 bronze badges
asked Mar 26 at 1:00
Woon LaiWoon Lai
31 bronze badge
31 bronze badge
Is it getting stuck in the inner loop or the outer loop?
– Dale Burrell
Mar 26 at 1:08
2
any particular reason why you are usingLOOPand not set-based solution ?
– Squirrel
Mar 26 at 1:10
1
Your loop will go forever if either of ItemCode or GRNNo are null
– Dale Burrell
Mar 26 at 1:12
@DaleBurrell my loop will end, if i export the result to file it work.
– Woon Lai
Mar 26 at 1:28
1
@WoonLai That's not a good reason to use a loop. You can still do it as a set.
– Joel Coehoorn
Mar 26 at 1:40
|
show 6 more comments
Is it getting stuck in the inner loop or the outer loop?
– Dale Burrell
Mar 26 at 1:08
2
any particular reason why you are usingLOOPand not set-based solution ?
– Squirrel
Mar 26 at 1:10
1
Your loop will go forever if either of ItemCode or GRNNo are null
– Dale Burrell
Mar 26 at 1:12
@DaleBurrell my loop will end, if i export the result to file it work.
– Woon Lai
Mar 26 at 1:28
1
@WoonLai That's not a good reason to use a loop. You can still do it as a set.
– Joel Coehoorn
Mar 26 at 1:40
Is it getting stuck in the inner loop or the outer loop?
– Dale Burrell
Mar 26 at 1:08
Is it getting stuck in the inner loop or the outer loop?
– Dale Burrell
Mar 26 at 1:08
2
2
any particular reason why you are using
LOOP and not set-based solution ?– Squirrel
Mar 26 at 1:10
any particular reason why you are using
LOOP and not set-based solution ?– Squirrel
Mar 26 at 1:10
1
1
Your loop will go forever if either of ItemCode or GRNNo are null
– Dale Burrell
Mar 26 at 1:12
Your loop will go forever if either of ItemCode or GRNNo are null
– Dale Burrell
Mar 26 at 1:12
@DaleBurrell my loop will end, if i export the result to file it work.
– Woon Lai
Mar 26 at 1:28
@DaleBurrell my loop will end, if i export the result to file it work.
– Woon Lai
Mar 26 at 1:28
1
1
@WoonLai That's not a good reason to use a loop. You can still do it as a set.
– Joel Coehoorn
Mar 26 at 1:40
@WoonLai That's not a good reason to use a loop. You can still do it as a set.
– Joel Coehoorn
Mar 26 at 1:40
|
show 6 more comments
1 Answer
1
active
oldest
votes
Try this: if this work for you I can explain all the things:
SET NOCOUNT ON
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
--Decalre basic table
DECLARE @tempOpening TABLE
(
Id int IDENTITY(1,1),
QTY int,
ItemCode nvarchar(30),
GRNNo nvarchar(30),
UnitPrice decimal(18,2),
PurchaseDate datetime,
UOM nvarchar(30),
Weight decimal(18,2)
)
DECLARE @site_value INT;
SET @site_value = 0;
--insert test data into basic table
WHILE @site_value <= 4752
BEGIN
INSERT INTO @tempOpening(QTY, ItemCode, GRNNo, UnitPrice, PurchaseDate, UOM, Weight)
VALUES(10,'Code100' + CAST(@site_value AS VARCHAR(10)),'GRNNo',10.2,GETDATE(), 'Unit',5.5);
SET @site_value = @site_value + 1;
END;
--created a new table for avoiding inner loop
DECLARE @tblQty TABLE
(
Id INT IDENTITY(1,1),
RowNo INT
)
DECLARE @maxQty INT
SELECT @maxQty =MAX(QTY) FROM @tempOpening
DECLARE @i INT = 1;
--insert data into Qty table with max Qty, this table data will help us to do dynamic query with joining
WHILE @i <= @maxQty
BEGIN
INSERT INTO @tblQty(RowNo)
VALUES(@i)
SET @i = @i + 1;
END;
SET @COUNT = 1
--created a temp table to insert all inner loop data, this is the final output data
DECLARE @tblAllData TABLE
(
Id INT IDENTITY(1,1),
ItemCode NVARCHAR(30),
SerialNo NVARCHAR(30),
PurchaseDate DATETIME,
TestData NVARCHAR(100),
TestRemark NVARCHAR(100),
Location NVARCHAR(100),
BuyType NVARCHAR(30),
Qty INT,
Weight DECIMAL(18,2),
Cost DECIMAL(18,2),
PackingFee INT,
Remarks NVARCHAR(200),
GRNNO NVARCHAR(30),
ReturnDate DATETIME NULL,
CreatedBy NVARCHAR(100),
CreateDate DATETIME NULL,
CreatedDate DATETIME NULL,
ModifiedDate DATETIME NULL,
SaNo INT NULL,
OriginalQty INT
)
DECLARE C CURSOR FOR
SELECT QTY, ItemCode, GRNNo, UnitPrice , PurchaseDate, UOM, Weight from @tempOpening
OPEN C
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
WHILE @@FETCH_STATUS = 0
BEGIN
--insert into the all table data
INSERT INTO @tblAllData(ItemCode, SerialNo, PurchaseDate, TestData, TestRemark, Location, BuyType, Qty, Weight, Cost, PackingFee, Remarks, GRNNO, ReturnDate,
CreatedBy, CreateDate, CreatedDate, ModifiedDate, SaNo, OriginalQty)
SELECT TOP (@Qty) @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST((@COUNT + tbl.RowNo) as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate, '' TestData, ''TestRemark, 'HQ' Location, @UOM BuyType, 1 qty, @Weight, @Cost Cost, 0 PackingFee, '' Remarks,
@GRNNo as GRNNO, null returnDate, 'SATO' createdBy, GETDATE() createDate, null CreatedDate, null ModifiedDate, null SaNo, @Qty - tbl.RowNo AS OriginalQty
FROM @tblQty tbl
ORDER BY tbl.RowNo
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
END
CLOSE C
DEALLOCATE C
SELECT * FROM @tblAllData
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348438%2fwhile-looping-query-gives-exception-out-of-memory%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this: if this work for you I can explain all the things:
SET NOCOUNT ON
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
--Decalre basic table
DECLARE @tempOpening TABLE
(
Id int IDENTITY(1,1),
QTY int,
ItemCode nvarchar(30),
GRNNo nvarchar(30),
UnitPrice decimal(18,2),
PurchaseDate datetime,
UOM nvarchar(30),
Weight decimal(18,2)
)
DECLARE @site_value INT;
SET @site_value = 0;
--insert test data into basic table
WHILE @site_value <= 4752
BEGIN
INSERT INTO @tempOpening(QTY, ItemCode, GRNNo, UnitPrice, PurchaseDate, UOM, Weight)
VALUES(10,'Code100' + CAST(@site_value AS VARCHAR(10)),'GRNNo',10.2,GETDATE(), 'Unit',5.5);
SET @site_value = @site_value + 1;
END;
--created a new table for avoiding inner loop
DECLARE @tblQty TABLE
(
Id INT IDENTITY(1,1),
RowNo INT
)
DECLARE @maxQty INT
SELECT @maxQty =MAX(QTY) FROM @tempOpening
DECLARE @i INT = 1;
--insert data into Qty table with max Qty, this table data will help us to do dynamic query with joining
WHILE @i <= @maxQty
BEGIN
INSERT INTO @tblQty(RowNo)
VALUES(@i)
SET @i = @i + 1;
END;
SET @COUNT = 1
--created a temp table to insert all inner loop data, this is the final output data
DECLARE @tblAllData TABLE
(
Id INT IDENTITY(1,1),
ItemCode NVARCHAR(30),
SerialNo NVARCHAR(30),
PurchaseDate DATETIME,
TestData NVARCHAR(100),
TestRemark NVARCHAR(100),
Location NVARCHAR(100),
BuyType NVARCHAR(30),
Qty INT,
Weight DECIMAL(18,2),
Cost DECIMAL(18,2),
PackingFee INT,
Remarks NVARCHAR(200),
GRNNO NVARCHAR(30),
ReturnDate DATETIME NULL,
CreatedBy NVARCHAR(100),
CreateDate DATETIME NULL,
CreatedDate DATETIME NULL,
ModifiedDate DATETIME NULL,
SaNo INT NULL,
OriginalQty INT
)
DECLARE C CURSOR FOR
SELECT QTY, ItemCode, GRNNo, UnitPrice , PurchaseDate, UOM, Weight from @tempOpening
OPEN C
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
WHILE @@FETCH_STATUS = 0
BEGIN
--insert into the all table data
INSERT INTO @tblAllData(ItemCode, SerialNo, PurchaseDate, TestData, TestRemark, Location, BuyType, Qty, Weight, Cost, PackingFee, Remarks, GRNNO, ReturnDate,
CreatedBy, CreateDate, CreatedDate, ModifiedDate, SaNo, OriginalQty)
SELECT TOP (@Qty) @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST((@COUNT + tbl.RowNo) as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate, '' TestData, ''TestRemark, 'HQ' Location, @UOM BuyType, 1 qty, @Weight, @Cost Cost, 0 PackingFee, '' Remarks,
@GRNNo as GRNNO, null returnDate, 'SATO' createdBy, GETDATE() createDate, null CreatedDate, null ModifiedDate, null SaNo, @Qty - tbl.RowNo AS OriginalQty
FROM @tblQty tbl
ORDER BY tbl.RowNo
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
END
CLOSE C
DEALLOCATE C
SELECT * FROM @tblAllData
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
add a comment |
Try this: if this work for you I can explain all the things:
SET NOCOUNT ON
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
--Decalre basic table
DECLARE @tempOpening TABLE
(
Id int IDENTITY(1,1),
QTY int,
ItemCode nvarchar(30),
GRNNo nvarchar(30),
UnitPrice decimal(18,2),
PurchaseDate datetime,
UOM nvarchar(30),
Weight decimal(18,2)
)
DECLARE @site_value INT;
SET @site_value = 0;
--insert test data into basic table
WHILE @site_value <= 4752
BEGIN
INSERT INTO @tempOpening(QTY, ItemCode, GRNNo, UnitPrice, PurchaseDate, UOM, Weight)
VALUES(10,'Code100' + CAST(@site_value AS VARCHAR(10)),'GRNNo',10.2,GETDATE(), 'Unit',5.5);
SET @site_value = @site_value + 1;
END;
--created a new table for avoiding inner loop
DECLARE @tblQty TABLE
(
Id INT IDENTITY(1,1),
RowNo INT
)
DECLARE @maxQty INT
SELECT @maxQty =MAX(QTY) FROM @tempOpening
DECLARE @i INT = 1;
--insert data into Qty table with max Qty, this table data will help us to do dynamic query with joining
WHILE @i <= @maxQty
BEGIN
INSERT INTO @tblQty(RowNo)
VALUES(@i)
SET @i = @i + 1;
END;
SET @COUNT = 1
--created a temp table to insert all inner loop data, this is the final output data
DECLARE @tblAllData TABLE
(
Id INT IDENTITY(1,1),
ItemCode NVARCHAR(30),
SerialNo NVARCHAR(30),
PurchaseDate DATETIME,
TestData NVARCHAR(100),
TestRemark NVARCHAR(100),
Location NVARCHAR(100),
BuyType NVARCHAR(30),
Qty INT,
Weight DECIMAL(18,2),
Cost DECIMAL(18,2),
PackingFee INT,
Remarks NVARCHAR(200),
GRNNO NVARCHAR(30),
ReturnDate DATETIME NULL,
CreatedBy NVARCHAR(100),
CreateDate DATETIME NULL,
CreatedDate DATETIME NULL,
ModifiedDate DATETIME NULL,
SaNo INT NULL,
OriginalQty INT
)
DECLARE C CURSOR FOR
SELECT QTY, ItemCode, GRNNo, UnitPrice , PurchaseDate, UOM, Weight from @tempOpening
OPEN C
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
WHILE @@FETCH_STATUS = 0
BEGIN
--insert into the all table data
INSERT INTO @tblAllData(ItemCode, SerialNo, PurchaseDate, TestData, TestRemark, Location, BuyType, Qty, Weight, Cost, PackingFee, Remarks, GRNNO, ReturnDate,
CreatedBy, CreateDate, CreatedDate, ModifiedDate, SaNo, OriginalQty)
SELECT TOP (@Qty) @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST((@COUNT + tbl.RowNo) as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate, '' TestData, ''TestRemark, 'HQ' Location, @UOM BuyType, 1 qty, @Weight, @Cost Cost, 0 PackingFee, '' Remarks,
@GRNNo as GRNNO, null returnDate, 'SATO' createdBy, GETDATE() createDate, null CreatedDate, null ModifiedDate, null SaNo, @Qty - tbl.RowNo AS OriginalQty
FROM @tblQty tbl
ORDER BY tbl.RowNo
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
END
CLOSE C
DEALLOCATE C
SELECT * FROM @tblAllData
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
add a comment |
Try this: if this work for you I can explain all the things:
SET NOCOUNT ON
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
--Decalre basic table
DECLARE @tempOpening TABLE
(
Id int IDENTITY(1,1),
QTY int,
ItemCode nvarchar(30),
GRNNo nvarchar(30),
UnitPrice decimal(18,2),
PurchaseDate datetime,
UOM nvarchar(30),
Weight decimal(18,2)
)
DECLARE @site_value INT;
SET @site_value = 0;
--insert test data into basic table
WHILE @site_value <= 4752
BEGIN
INSERT INTO @tempOpening(QTY, ItemCode, GRNNo, UnitPrice, PurchaseDate, UOM, Weight)
VALUES(10,'Code100' + CAST(@site_value AS VARCHAR(10)),'GRNNo',10.2,GETDATE(), 'Unit',5.5);
SET @site_value = @site_value + 1;
END;
--created a new table for avoiding inner loop
DECLARE @tblQty TABLE
(
Id INT IDENTITY(1,1),
RowNo INT
)
DECLARE @maxQty INT
SELECT @maxQty =MAX(QTY) FROM @tempOpening
DECLARE @i INT = 1;
--insert data into Qty table with max Qty, this table data will help us to do dynamic query with joining
WHILE @i <= @maxQty
BEGIN
INSERT INTO @tblQty(RowNo)
VALUES(@i)
SET @i = @i + 1;
END;
SET @COUNT = 1
--created a temp table to insert all inner loop data, this is the final output data
DECLARE @tblAllData TABLE
(
Id INT IDENTITY(1,1),
ItemCode NVARCHAR(30),
SerialNo NVARCHAR(30),
PurchaseDate DATETIME,
TestData NVARCHAR(100),
TestRemark NVARCHAR(100),
Location NVARCHAR(100),
BuyType NVARCHAR(30),
Qty INT,
Weight DECIMAL(18,2),
Cost DECIMAL(18,2),
PackingFee INT,
Remarks NVARCHAR(200),
GRNNO NVARCHAR(30),
ReturnDate DATETIME NULL,
CreatedBy NVARCHAR(100),
CreateDate DATETIME NULL,
CreatedDate DATETIME NULL,
ModifiedDate DATETIME NULL,
SaNo INT NULL,
OriginalQty INT
)
DECLARE C CURSOR FOR
SELECT QTY, ItemCode, GRNNo, UnitPrice , PurchaseDate, UOM, Weight from @tempOpening
OPEN C
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
WHILE @@FETCH_STATUS = 0
BEGIN
--insert into the all table data
INSERT INTO @tblAllData(ItemCode, SerialNo, PurchaseDate, TestData, TestRemark, Location, BuyType, Qty, Weight, Cost, PackingFee, Remarks, GRNNO, ReturnDate,
CreatedBy, CreateDate, CreatedDate, ModifiedDate, SaNo, OriginalQty)
SELECT TOP (@Qty) @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST((@COUNT + tbl.RowNo) as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate, '' TestData, ''TestRemark, 'HQ' Location, @UOM BuyType, 1 qty, @Weight, @Cost Cost, 0 PackingFee, '' Remarks,
@GRNNo as GRNNO, null returnDate, 'SATO' createdBy, GETDATE() createDate, null CreatedDate, null ModifiedDate, null SaNo, @Qty - tbl.RowNo AS OriginalQty
FROM @tblQty tbl
ORDER BY tbl.RowNo
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
END
CLOSE C
DEALLOCATE C
SELECT * FROM @tblAllData
Try this: if this work for you I can explain all the things:
SET NOCOUNT ON
DECLARE @Qty int
DECLARE @COUNT int
DECLARE @itemcode nvarchar(30)
DECLARE @GRNNo nvarchar(30)
DECLARE @UOM nvarchar(30)
DECLARE @Weight decimal(18,2)
DECLARE @PurchaseDate datetime
DECLARE @Cost decimal(18,2)
--Decalre basic table
DECLARE @tempOpening TABLE
(
Id int IDENTITY(1,1),
QTY int,
ItemCode nvarchar(30),
GRNNo nvarchar(30),
UnitPrice decimal(18,2),
PurchaseDate datetime,
UOM nvarchar(30),
Weight decimal(18,2)
)
DECLARE @site_value INT;
SET @site_value = 0;
--insert test data into basic table
WHILE @site_value <= 4752
BEGIN
INSERT INTO @tempOpening(QTY, ItemCode, GRNNo, UnitPrice, PurchaseDate, UOM, Weight)
VALUES(10,'Code100' + CAST(@site_value AS VARCHAR(10)),'GRNNo',10.2,GETDATE(), 'Unit',5.5);
SET @site_value = @site_value + 1;
END;
--created a new table for avoiding inner loop
DECLARE @tblQty TABLE
(
Id INT IDENTITY(1,1),
RowNo INT
)
DECLARE @maxQty INT
SELECT @maxQty =MAX(QTY) FROM @tempOpening
DECLARE @i INT = 1;
--insert data into Qty table with max Qty, this table data will help us to do dynamic query with joining
WHILE @i <= @maxQty
BEGIN
INSERT INTO @tblQty(RowNo)
VALUES(@i)
SET @i = @i + 1;
END;
SET @COUNT = 1
--created a temp table to insert all inner loop data, this is the final output data
DECLARE @tblAllData TABLE
(
Id INT IDENTITY(1,1),
ItemCode NVARCHAR(30),
SerialNo NVARCHAR(30),
PurchaseDate DATETIME,
TestData NVARCHAR(100),
TestRemark NVARCHAR(100),
Location NVARCHAR(100),
BuyType NVARCHAR(30),
Qty INT,
Weight DECIMAL(18,2),
Cost DECIMAL(18,2),
PackingFee INT,
Remarks NVARCHAR(200),
GRNNO NVARCHAR(30),
ReturnDate DATETIME NULL,
CreatedBy NVARCHAR(100),
CreateDate DATETIME NULL,
CreatedDate DATETIME NULL,
ModifiedDate DATETIME NULL,
SaNo INT NULL,
OriginalQty INT
)
DECLARE C CURSOR FOR
SELECT QTY, ItemCode, GRNNo, UnitPrice , PurchaseDate, UOM, Weight from @tempOpening
OPEN C
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
WHILE @@FETCH_STATUS = 0
BEGIN
--insert into the all table data
INSERT INTO @tblAllData(ItemCode, SerialNo, PurchaseDate, TestData, TestRemark, Location, BuyType, Qty, Weight, Cost, PackingFee, Remarks, GRNNO, ReturnDate,
CreatedBy, CreateDate, CreatedDate, ModifiedDate, SaNo, OriginalQty)
SELECT TOP (@Qty) @itemcode AS ItemCode,
'D'+RIGHT('00000000'+CAST((@COUNT + tbl.RowNo) as nvarchar(8)),8) as SerialNo,
@PurchaseDate PurchaseDate, '' TestData, ''TestRemark, 'HQ' Location, @UOM BuyType, 1 qty, @Weight, @Cost Cost, 0 PackingFee, '' Remarks,
@GRNNo as GRNNO, null returnDate, 'SATO' createdBy, GETDATE() createDate, null CreatedDate, null ModifiedDate, null SaNo, @Qty - tbl.RowNo AS OriginalQty
FROM @tblQty tbl
ORDER BY tbl.RowNo
FETCH NEXT FROM C INTO @Qty , @itemcode , @GRNNo , @Cost , @PurchaseDate, @UOM , @Weight
END
CLOSE C
DEALLOCATE C
SELECT * FROM @tblAllData
answered Mar 26 at 3:21
Hasan MahmoodHasan Mahmood
9657 silver badges10 bronze badges
9657 silver badges10 bronze badges
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
add a comment |
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
Thank you so much. i will follow your logic to modify abit.
– Woon Lai
Mar 26 at 5:43
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348438%2fwhile-looping-query-gives-exception-out-of-memory%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it getting stuck in the inner loop or the outer loop?
– Dale Burrell
Mar 26 at 1:08
2
any particular reason why you are using
LOOPand not set-based solution ?– Squirrel
Mar 26 at 1:10
1
Your loop will go forever if either of ItemCode or GRNNo are null
– Dale Burrell
Mar 26 at 1:12
@DaleBurrell my loop will end, if i export the result to file it work.
– Woon Lai
Mar 26 at 1:28
1
@WoonLai That's not a good reason to use a loop. You can still do it as a set.
– Joel Coehoorn
Mar 26 at 1:40