Lowyat.NET Forums

Welcome Guest ( Log In | Register )

LYN wins Intel-Lenovo-Tangs Blogathon challenge. Thank you everybody!
 
RSS feedBump TopicReply to this topicStart new topicStart Poll

Outline · [ Standard ] · Linear+

> Stored procedure, how to...

LOOOOOOL
post Oct 26 2009, 11:12 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #1


Getting Started
**

Group: Junior Member
Posts: 165
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Oct 2007
From: Wangsa Maju





erm from my query, i get this (already ORDER BY):


idx
data hp number
1 abc +60161111111
2 afg +60129999999
3 aop +60129999999
4 zzz +60193333333
5 yyy +60193333333
6 uuu +60193333333


CODE

declare @string varchar(50)
declare @hp varchar(20)


how do i set the @string, where hp number are same( idx 2 & 3 are combine )

e.g @string = 'afg, aop'
@hp = '60129999999'

i'm using fetch, i stored the entire @string into another table until i get a different @hp.
how do i stop the fetch where current hp number is different with previous??then i start combine the string, store into table, then continue fetch next...

like this:

idx__data________hp number
1 __afg,aop______+60129999999
2 __zzz,yyy,uuu__+60193333333

This post has been edited by LOOOOOOL: Oct 26 2009, 11:14 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
hoilok
post Oct 26 2009, 11:23 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #2


Enthusiast
*****

Group: Senior Member
Posts: 702
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Apr 2007






use pivot
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
LOOOOOOL
post Oct 26 2009, 11:28 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #3


Getting Started
**

Group: Junior Member
Posts: 165
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Oct 2007
From: Wangsa Maju





pivot is to rotate the table @_@
how can it related to my problem?? i'm not very pro in SP
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
ketnave
post Oct 26 2009, 11:29 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #4


Getting Started
**

Group: Junior Member
Posts: 53
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Sep 2009




QUOTE(LOOOOOOL @ Oct 26 2009, 11:12 AM)
erm from my query, i get this (already ORDER BY):


idx
    data    hp number
1 abc    +60161111111
2 afg    +60129999999
3 aop    +60129999999
4      zzz      +60193333333
5      yyy      +60193333333
6      uuu      +60193333333
CODE

declare @string varchar(50)
declare @hp varchar(20)


how do i set the @string, where hp number are same( idx 2 & 3 are combine )

e.g @string = 'afg, aop'
    @hp = '60129999999'

i'm using fetch, i stored the entire @string into another table until i get a different @hp.
how do i stop the fetch where current hp number is different with previous??then i start combine the string, store into table, then continue fetch next...

like this:

idx__data________hp number
1  __afg,aop______+60129999999
2  __zzz,yyy,uuu__+60193333333
*



use 2 cursors

1st cursor
select distinct mobile from tbl

2nd cursor (within 1st cursor)
select data from tbl where mobile = @mobile


User is offlineProfile CardPM
Go to the top of the page
+Quote Post
LOOOOOOL
post Oct 26 2009, 11:32 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #5


Getting Started
**

Group: Junior Member
Posts: 165
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Oct 2007
From: Wangsa Maju





QUOTE(ketnave @ Oct 26 2009, 11:29 AM)
use 2 cursors

1st cursor
select distinct mobile from tbl

2nd cursor (within 1st cursor)
select data from tbl where mobile = @mobile
*



wow, nice. i go try out..thx ya biggrin.gif biggrin.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Eventless
post Oct 26 2009, 11:36 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #6


On my way
****

Group: Senior Member
Posts: 645
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Jan 2003




You could do everything within a single cursor as long as the records are ordered by hp number.

When the hp number in current row is different from the current hp number, save the existing hp number, names and idx number into you table. After storing the data, increment your idx, set the current hp number to the new hp number and set your string to the current data.

If the hp number in the current row is still the same as the current hp number, append the data in the current row to your string variable.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
LOOOOOOL
post Oct 26 2009, 12:08 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #7


Getting Started
**

Group: Junior Member
Posts: 165
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Oct 2007
From: Wangsa Maju





QUOTE(Eventless @ Oct 26 2009, 11:36 AM)
You could do everything within a single cursor as long as the records are ordered by hp number.

When the hp number in current row is different from the current hp number, save the existing hp number, names and idx number into you table. After storing the data, increment your idx, set the current hp number to the new hp number and set your string to the current data.

If the hp number in the current row is still the same as the current hp number, append the data in the current row to your string variable.
*



your method gona use UPDATE clause rite?? its working too...haha
just done using 2 cursor, hmm..maybe using the UPDATE method code will be shorter laugh.gif

User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Eventless
post Oct 26 2009, 01:57 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #8


On my way
****

Group: Senior Member
Posts: 645
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Jan 2003




QUOTE(LOOOOOOL @ Oct 26 2009, 12:08 PM)
your method gona use UPDATE clause rite?? its working too...haha
just done using 2 cursor, hmm..maybe using the UPDATE method code will be shorter  laugh.gif
*



I thought you were storing the new row (idx,data,hp number) in a different table based on your description. You mean you want to put the resulting row back into the original table that the cursor is based on? I don't think you can do that with a cursor in your case.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
LOOOOOOL
post Oct 27 2009, 11:30 AM
Show posts by this member only |This post's rating (0+, 0-) | Post #9


Getting Started
**

Group: Junior Member
Posts: 165
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Oct 2007
From: Wangsa Maju





QUOTE(Eventless @ Oct 26 2009, 01:57 PM)
I thought you were storing the new row (idx,data,hp number) in a different table based on your description. You mean you want to put the resulting row back into the original table that the cursor is based on? I don't think you can do that with a cursor in your case.
*



yes, stored the new row into a new table smile.gif
i can update the row by append the data each time i get the same hp number from my cursor.

but now I'm using 2 cursor, for me, the code look more understandable.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Bump TopicReply to this topicTopic OptionsStart new topic
 



----debug section please ignore----
Lo-Fi Version Time is now: 24th November 2009 - 12:57 AM
All Rights Reserved 2003-2009 Vijandren Ramadass (~living on a prayer~)