Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


update cursor


update cursor

Author
Message
newbie_sql
newbie_sql
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 35
I have the following tables:
Tbl1
(sid int,
cid int)

Tbl2
(oid int,
tnum varchar(10))

Tbl3
(rec_id int identity,
otnum varchar(10),
ntnum varchar(10),
sid int)

Step1:I need to select tbl1.sid using a join of tbl1 & tbl2 based on tbl1.cid=tbl2.oid-working fine
step2:All the selected sid's need to be inserted into Tbl3-woking fine
step3:After this I need to run an update query to remove spaces from tbl2.tnum & also insert each updated record into Tbl3-stuck???

I am implementing this using a cursor as below-but cant formulate the update cursor to do Step 3


DECLARE @str varchar(20)

DECLARE c1 CURSOR
FOR
SELECT tbl1.sid
FROM tbl1,tbl2
WHERE tbl1.cid=tbl2.oid order by tbl1.sid desc

OPEN c1

FETCH NEXT FROM c1
INTO @str

WHILE @@FETCH_STATUS = 0
BEGIN

INSERT INTO Tbl3(sid)
VALUES (@str)

FETCH NEXT FROM c1
INTO @str

END
--step 3 not clear how to loop by using current row
Update Tbl2 set tnum=dbo.sp_trim_space(tnum)
OUTPUT deleted.tnum,inserted.tnum,@str
into tbl3 where <???current row???>

CLOSE c1


Thanks for the help
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21075 Visits: 18259
Have you considered using a set-based method instead of the cursor method?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

newbie_sql
newbie_sql
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 35
Im not aware of the set based method..Can you pls suggest how is that used?
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8960 Visits: 19020
charu.verma (12/11/2009)
Im not aware of the set based method..Can you pls suggest how is that used?

Like this:
DROP TABLE #Tbl1
DROP TABLE #Tbl2
DROP TABLE #Tbl3

CREATE TABLE #Tbl1 ([sid] int, cid int)
CREATE TABLE #Tbl2 (oid int, tnum varchar(10))
CREATE TABLE #Tbl3 (rec_id int identity, otnum varchar(10), ntnum varchar(10), [sid] int)

INSERT INTO #Tbl1 ([sid], cid)
SELECT 1, 10 UNION ALL
SELECT 2, 20 UNION ALL
SELECT 3, 30 UNION ALL
SELECT 4, 40 UNION ALL
SELECT 5, 50 UNION ALL
SELECT 6, 60 UNION ALL
SELECT 7, 70

INSERT INTO #Tbl2 (oid, tnum)
SELECT 10, '100 0' UNION ALL
SELECT 20, '2000' UNION ALL
SELECT 30, '300 0' UNION ALL
SELECT 40, '4000' UNION ALL
SELECT 50, '500 0'

-- Step1:I need to select tbl1.sid using a join of tbl1 & tbl2 based on tbl1.cid=tbl2.oid-working fine
-- step2:All the selected sid's need to be inserted into Tbl3-woking fine
INSERT INTO #Tbl3 ([sid])
SELECT tbl1.[sid]
FROM #tbl1 tbl1
INNER JOIN #tbl2 tbl2 ON tbl1.cid = tbl2.oid

-- step4:insert each updated record into Tbl3-stuck???
INSERT INTO #Tbl3 ([sid], otnum)
SELECT oid, REPLACE(tnum, ' ', '')
FROM #Tbl2 Tbl2
WHERE tnum LIKE '% %'

-- step3:After this I need to run an update query to remove spaces from tbl2.tnum &
UPDATE #Tbl2
SET tnum = REPLACE(tnum, ' ', '')
WHERE tnum LIKE '% %'

SELECT * FROM #Tbl3
SELECT * FROM #Tbl2



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
newbie_sql
newbie_sql
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 35
Thanks-but this will not work

The cloumns in Tbl3 have to be deleted tnum of tbl2,formatted tnum returned from fucntion & sid of tbl1 & not oid of tbl2
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8960 Visits: 19020
charu.verma (12/11/2009)
Thanks-but this will not work

The cloumns in Tbl3 have to be deleted tnum of tbl2,formatted tnum returned from fucntion & sid of tbl1 & not oid of tbl2


The code I provided does exactly what you wanted, according to the instructions Step1, Step2 and Step3 in your first post. If you know for certain what you want to do, then please say so in a clear and unequivocal manner.
Given the data sets which I provided for you, what should your output be?
If the data I provided for you is insufficient, please augment it until it is suitable for testing, using the same code - INSERT INTO .... SELECT.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21075 Visits: 18259
Thanks Chris for following up.

As for the requirements and desired output, Chris was spot on. If this does not do exactly what you envisioned - we need to have a better explanation.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8960 Visits: 19020
CirquedeSQLeil (12/11/2009)
Thanks Chris for following up.

As for the requirements and desired output, Chris was spot on. If this does not do exactly what you envisioned - we need to have a better explanation.


No probs Jason

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search