UPDATE: For working example using AdventureWorks visit : SQL SERVER – Simple Example of Cursor – Sample Cursor Part 2
This is the simplest example of the SQL Server Cursor. I have used this all the time for any use of Cursor in my T-SQL.
DECLARE @AccountID INT
DECLARE @getAccountID CURSOR
SET @getAccountID =Â CURSORÂ FOR
SELECT Account_ID
FROM Accounts
OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
END
CLOSE @getAccountID
DEALLOCATE @getAccountID
Reference: Pinal Dave (http://www.SQLAuthority.com), BOL





196 Comments. Leave new
diff bet result set,data set and data table in sql
Hi,
I have written a cursor in MS SQL server and was able to execute it also. But i have an issue where it’s not displaying the results in the order what i have written it’s displaying randomly. First I am selecting the records from Info table and then from sinfo table.
Example:
DECLARE emails CURSOR FOR select distinct LTRIM(RTRIM(useremail)) from Info
UNION
select DISTINCT LTRIM(RTRIM(useremail)) from SInfo
SET NOCOUNT ON
DECLARE @email varchar(100)
OPEN emails
FETCH NEXT from emails
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT from emails into @email
select username, password, email from info where useremail = @email
union
select username, password, email from Sinfo where useremail = @email
END
CLOSE emails
DEALLOCATE emails
Any suggestions would be much appreciated.
Thanks
Hi,
Any suggestions on my issue?
Thanks
Hi pinal
give me solution of two tables compair for reconcillation
Thank’s
Kishor
Great Article that helps me lot but i just want to know can i select multiple values in cursor for
Hi All,
I have an array of emails and i want to split every email and check every email from database and its correspondence Status.
Please Help!
Very nice example of Cursors thank u so much Pinal
Regards & ThanxX
Arif Shaik
Very nice example of Cursors thank u so much Pinal
Regards & ThanxX
Arif Shaik
nice one
Great Article that helps me lot
thank u pinal
Hi Pinal
Can u give an example of selecting top few rows from each group where we are getting the result by joining two or more tables.can we give an alias name to the joining of two tables, if yes please let me know how, i shall be thankful.
Thanks
Taruna
Based on what I have understood from your query I have done something…
Create table t_Employee
( EmployeeID Int,
EmployeeName nVarChar(20)
)
Create table t_Deduction
( DeductionID Int,
Description nVarChar(20)
)
Create table t_EmployeeDeductions
( EmployeeID Int,
DeductionID Int
)
INSERT TABLE t_Employee
Select 1,’ABC’
UNION ALL
Select 2,’DEF’
UNION ALL
Select 3,’GHI’
UNION ALL
Select 4,’CAT’
UNION ALL
Select 5,’ABC’
INSERT TABLE t_Deduction
Select 1,’PF’
UNION ALL
Select 2,’Phone Bill’
UNION ALL
Select 3,’Insurance’
INSERT TABLE t_EmployeeDeduction
Select 1,1
UNION ALL
Select 1,2
UNION ALL
Select 1,1
UNION ALL
Select 1,2
Select top 2 ED.*
from t_Employee AS Emp
INNER JOIN
t_EmployeeDeductions ED
ON Emp.EmployeeID = ED.EmployeeID
Now if you have any question please feel free to revert…
Regards
Malaya
Emil
when i am creating an SSIS, where I need to do a merge join between Excel source data and OLEDB source data(data from SQL table),ofcourse after sorting, it is giving me the following errors not on merge join but on retrieving data from the OLEDB source itself as the data is huge-in crores, so tell me the solution if u have one, wud b thankful, the reason why i am doing merge join is i have data in the excel file out of which some records are there in SQL database and i want to know what data is there and what is not , the data is in the related tables . Also i want the output containing the columns of excel as well as the columns (fields) of the tables in a single excel sheet so that the other persons or should i say the users of the data can get to know what data is useful and they want to compare.
i want to match on 5 fields, say firstname,lastname,city,state and the first 5 characters of the street address.how can i use LTRIM,RTRIM,LEFT and UPPER string functions on the data i have imported from excel?
Sorry, so many questions, but i really need help.
Thanks
Tanu
Emil
when i am creating an SSIS, where I need to do a merge join between Excel source data and OLEDB source data(data from SQL table),ofcourse after sorting, it is giving me the following errors not on merge join but on retrieving data from the OLEDB source itself as the data is huge-in crores, so tell me the solution if u have one, wud b thankful, the reason why i am doing merge join is i have data in the excel file out of which some records are there in SQL database and i want to know what data is there and what is not , the data is in the related tables . Also i want the output containing the columns of excel as well as the columns (fields) of the tables in a single excel sheet so that the other persons or should i say the users of the data can get to know what data is useful and they want to compare.
i want to match on 5 fields, say firstname,lastname,city,state and the first 5 characters of the street address.how can i use LTRIM,RTRIM,LEFT and UPPER string functions on the data i have imported from excel?
Sorry, so many questions, but i really need help.
Thanks
Tanu
Error: 0x80070070 at Data Flow Task, DTS.Pipeline: There is not enough space on the disk.
Error: 0x80070070 at Data Flow Task, DTS.Pipeline: There is not enough space on the disk.
Error: 0xC004704A at Data Flow Task, DTS.Pipeline: The buffer manager cannot extend the file “C:\DOCUME~1\tarunm\LOCALS~1\Temp\a\DTS1A93.tmp” to length 3473188. There was insufficient disk space.
Error: 0xC004704A at Data Flow Task, DTS.Pipeline: The buffer manager cannot extend the file “C:\DOCUME~1\tarunm\LOCALS~1\Temp\a\DTS1A94.tmp” to length 3473188. There was insufficient disk space.
Error: 0x80070070 at Data Flow Task, DTS.Pipeline: There is not enough space on the disk.
Error: 0x80070070 at Data Flow Task, DTS.Pipeline: There is not enough space on the disk.
Error: 0xC004704A at Data Flow Task, DTS.Pipeline: The buffer manager cannot extend the file “C:\DOCUME~1\tarunm\LOCALS~1\Temp\a\DTS1A95.tmp” to length 3473188. There was insufficient disk space.
Error: 0xC004704A at Data Flow Task, DTS.Pipeline: The buffer manager cannot extend the file “C:\DOCUME~1\tarunm\LOCALS~1\Temp\a\DTS1A96.tmp” to length 3473188. There was insufficient disk space.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Information: 0x4004800D at Data Flow Task, DTS.Pipeline: The buffer manager failed a memory allocation call for 3473184 bytes, but was unable to swap out any buffers to relieve memory pressure. 1 buffers were considered and 0 were locked. Either not enough memory is available to the pipeline because not enough are installed, other processes were using it, or too many buffers are locked.
Information: 0x4004800D at Data Flow Task, DTS.Pipeline: The buffer manager failed a memory allocation call for 3473184 bytes, but was unable to swap out any buffers to relieve memory pressure. 7 buffers were considered and 3 were locked. Either not enough memory is available to the pipeline because not enough are installed, other processes were using it, or too many buffers are locked.
Error: 0xC0047012 at Data Flow Task, DTS.Pipeline: A buffer failed while allocating 3473184 bytes.
Error: 0xC0047012 at Data Flow Task, DTS.Pipeline: A buffer failed while allocating 3473184 bytes.
Error: 0xC0047011 at Data Flow Task, DTS.Pipeline: The system reports 44 percent memory load. There are 8589111296 bytes of physical memory with 4764954624 bytes free. There are 2147352576 bytes of virtual memory with 104652800 bytes free. The paging file has 12660408320 bytes with 8538820608 bytes free.
Error: 0xC0047011 at Data Flow Task, DTS.Pipeline: The system reports 44 percent memory load. There are 8589111296 bytes of physical memory with 4764971008 bytes free. There are 2147352576 bytes of virtual memory with 104652800 bytes free. The paging file has 12660408320 bytes with 8538845184 bytes free.
Warning: 0x8007000E at Data Flow Task, Sort 1 [796]: Not enough storage is available to complete this operation.
Warning: 0x8007000E at Data Flow Task, Sort 1 [796]: Not enough storage is available to complete this operation.
Error: 0xC0047048 at Data Flow Task, DTS.Pipeline: The buffer manager cannot create a temporary storage file on any path in the BufferTempStoragePath property. There is an incorrect file name or no permission.
Error: 0xC020821A at Data Flow Task, Sort 1 [796]: The Sort transformation cannot queue a work item to its thread pool. There is not enough memory available.
Error: 0xC020821A at Data Flow Task, Sort 1 [796]: The Sort transformation cannot queue a work item to its thread pool. There is not enough memory available.
Warning: 0x8007000E at Data Flow Task, Sort 1 [796]: Not enough storage is available to complete this operation.
Error: 0xC0208296 at Data Flow Task, Sort 1 [796]: The input buffer could not be cloned. An out-of-memory condition occurred or there was an internal error.
Error: 0xC0047022 at Data Flow Task, DTS.Pipeline: The ProcessInput method on component “Sort 1” (796) failed with error code 0x80004005. The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.
Error: 0xC0047021 at Data Flow Task, DTS.Pipeline: Thread “WorkThread1” has exited with error code 0x80004005.
Error: 0xC0047039 at Data Flow Task, DTS.Pipeline: Thread “WorkThread3” received a shutdown signal and is terminating. The user requested a shutdown, or an error in another thread is causing the pipeline to shutdown.
Error: 0xC0047039 at Data Flow Task, DTS.Pipeline: Thread “WorkThread2” received a shutdown signal and is terminating. The user requested a shutdown, or an error in another thread is causing the pipeline to shutdown.
Error: 0xC0047021 at Data Flow Task, DTS.Pipeline: Thread “WorkThread2” has exited with error code 0xC0047039.
Error: 0xC0047021 at Data Flow Task, DTS.Pipeline: Thread “WorkThread3” has exited with error code 0xC0047039.
Error: 0xC02020C4 at Data Flow Task, OLE DB Source [187]: The attempt to add a row to the Data Flow task buffer failed with error code 0xC0047020.
Error: 0xC0047038 at Data Flow Task, DTS.Pipeline: The PrimeOutput method on component “OLE DB Source” (187) returned error code 0xC02020C4. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing.
Error: 0xC0047021 at Data Flow Task, DTS.Pipeline: Thread “SourceThread1” has exited with error code 0xC0047038.
Information: 0x40043008 at Data Flow Task, DTS.Pipeline: Post Execute phase is beginning.
Information: 0x40043009 at Data Flow Task, DTS.Pipeline: Cleanup phase is beginning.
Information: 0x4004300B at Data Flow Task, DTS.Pipeline: “component “Excel Destination” (4131)” wrote 0 rows.
Task failed: Data Flow Task
Warning: 0x80019002 at Package1: The Execution method succeeded, but the number of errors raised (32) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package “Package1.dtsx” finished: Failure.
The program ‘[4820] Package1.dtsx: DTS’ has exited with code 0 (0x0).
Hi Malaya
u are right but i dont need that solution as my problem is i am getting the data from three related tables.say, i am grouping on a field from Table A and against every group i am calculating sum of values from a field which is in table B and also i am sorting on this sum field and now i get say 20 -30 records for each group, but i want to have top 3-3 from each group.
I dont want to store the intermediate dat in any of temp tables.
I just want a single query.
If u can help, i really appreciate.
Thanks
Taruna
Refer this post
very good article
Thank you!! very very helpful!!
Nice one for beginners.
Awesome! code for beginners, simple and really works :D thanks a lot dude.
Thank you. it’s userfull
good example…
pinal plz share SSIS architecture
Some indentation would be nice. Leaving your page and googling for something clearer.