SQL SERVER – Simple Example of Cursor – Sample Cursor Part 2

I have recently received email that I should update SQL SERVER – Simple Example of Cursor with example of AdventureWorks database.

Simple Example of Cursor using AdventureWorks Database is listed here.

USE AdventureWorks
GO
DECLARE @ProductID INT
DECLARE
@getProductID CURSOR
SET
@getProductID = CURSOR FOR
SELECT
ProductID
FROM Production.Product
OPEN @getProductID
FETCH NEXT
FROM @getProductID INTO @ProductID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT
@ProductID
FETCH NEXT
FROM @getProductID INTO @ProductID
END
CLOSE
@getProductID
DEALLOCATE @getProductID
GO


Reference : Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
,
Previous Post
SQL SERVER – 2005 – A Simple Way To Defragment All Indexes In A Database That Is Fragmented Above A Declared Threshold
Next Post
SQLAuthority News – SQL Server 2005 is The Data Platform Leader

Related Posts

69 Comments. Leave new

  • Id, DoctypeCode, SubtypeCode, Batch_Type,
    1 ACC cmp rd

    From Document Number, Thru Document Number, Street
    R o p

    I have 129 column in a table. I want to query a table to find out for each doctypecode and subtypecode how many records exists with ‘o’ and ‘r’
    I want to see the result not the count.

    Please help me on this

    Reply
  • hello sir my name is happy i want to backup of 200 tables but 300 table in 1 database. if i want to full backup use database right click backup.
    i don’t know use this query
    ok sir

    Reply
  • sindhu Jaganathan
    June 4, 2012 11:31 am

    why we are using procedures?function is always return the value.the procedure may or may not return the value..

    Reply
  • Sir I am huge fan of your’s . I often read your articles. recently I got a task to insert data in two tables by one subquery . I couldnot perform that task . please give me some solutions

    Reply
  • Hi Iam irfan ,It such a nice blog thanks for your service

    Reply
  • Hi All ,In need of your help,
    Here is scenario
    Table A :
    invoice_no Product
    ————— —————–
    1 Pencil
    1 Box
    1 Pen
    2 Pen
    2 Box
    3 Pencil

    i need to create a cursor such that i store in #details(Temporary table)
    Desired Output :
    Select * from #details

    Invno Products
    ——– ———————–
    1 Pencil,Box,Pen
    2 Pen,Box
    3 Pencil

    Reply
  • I have a table
    Id Value
    — ——–
    1 aa,ab,av
    2 ba
    3 ca,cb
    4 da,db,dc,dd
    and I want to create a table like this
    id Value
    — ——
    1 aa
    1 ab
    1 av
    2 ba
    3 ca
    3 cb
    4 da
    4 db
    4 dc
    4 dd

    Can you suggest me code for this

    Reply
  • I have table tab1 in which there are four flavours
    flavor 1 flavor 2 flavor 3 and flavor 4 and a column called match%

    now if flavor 1 of one row matches with either flavor 1 or flavor 2 or flavor 3 or flavor 4
    or flavor 2 of one row matches with either flavor 1 or flavor 2 or flavor 3 or flavor 4
    or flavor 3 of one row matches with either flavor 1 or flavor 2 or flavor 3 or flavor 4
    or flavor 4 of one row matches with either flavor 1 or flavor 2 or flavor 3 or flavor 4

    accordingly the match% should be updated

    like

    flavor 1 || flavor 2|| flavor 3|| flavor 4|| match%
    ==================================
    diet || peach || orange || tonic ||100%
    orange || diet || tonic || peach ||100%
    pineapple || peach || soda ||soda ||25%

    Reply
  • plz display example

    Reply
  • Hi Dave I have been following your blog since 2009 and there is not one time i went back without solution. You have been my Guru from all years along. Thanks so much for your time and posts.

    Reply
  • Mustafa Mahmood
    August 17, 2016 1:57 pm

    hi dave i need help i am very upset . i want to deploy report in ssrs on other machine or other reporting server . plz tell me how can i do it

    Reply

Leave a Reply

Menu