Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

update cursor Expand / Collapse
Author
Message
Posted Thursday, December 10, 2009 3:55 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 27, 2010 8:09 AM
Points: 28, 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
Post #832634
Posted Thursday, December 10, 2009 8:24 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:07 PM
Points: 21,351, Visits: 15,032
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #832688
Posted Friday, December 11, 2009 2:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 27, 2010 8:09 AM
Points: 28, Visits: 35
Im not aware of the set based method..Can you pls suggest how is that used?
Post #832775
Posted Friday, December 11, 2009 4:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:56 AM
Points: 7,137, Visits: 13,527
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
Post #832827
Posted Friday, December 11, 2009 8:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 27, 2010 8:09 AM
Points: 28, 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
Post #832965
Posted Friday, December 11, 2009 8:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:56 AM
Points: 7,137, Visits: 13,527
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
Post #832973
Posted Friday, December 11, 2009 9:08 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:07 PM
Points: 21,351, Visits: 15,032
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #833009
Posted Friday, December 11, 2009 9:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:56 AM
Points: 7,137, Visits: 13,527
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
Post #833014
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse