If you are talking about cursors in T-SQL, such as in a stored procedure, it would be much more effiecient to insert the results of the query used to create the cursor into a temporary table. There is no need to use a cursor for this.
Rather than declaring the cursor
DECLARE tbl_cursor CURSOR
FOR SELECT col1, col2, col3, col4 FROM tbl
and then writing code to insert records to a temporary table, just insert directly into the temporary table.
SELECT col1, col2, col3, col4
INTO #temp FROM tbl
Of course, you can fetch the cursor data and insert.
DECLARE tbl_cursor CURSOR
FOR SELECT col1, col2, col3, col4 FROM tbl
OPEN tbl_cursor
FETCH NEXT FROM tbl_cursor INTO @col1, @col2, @col3, @col4
WHILE @@FETCH_STATUS = 0
BEGIN
Insert #temp(col1,col2,col3,col4)
Values(@col1,@col2,@col3,@col4)
FETCH NEXT FROM tbl_cursor INTO @col1, @col2, @col3, @col4
END
Insert #t
Select tbl1.IDNo, tbl1.[Name], Sum(tbl2.Total) As ItemsBought
From tbl1 Inner Join tbl2
On tbl1.IDNo=tbl2.IDNo
Group By tbl1.IDNo, tnl1.[Name Terry
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.