[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Stored Procedure with Cursor Replace then Update

Posted on 2004-10-05
8
Medium Priority
?
1,022 Views
Last Modified: 2012-06-27
I have not done any work with cursor's before in a stored procedure, I've researched it, but I can seem to get my head around it

I have a variable @message char(1000)

I have table contacts

I need to query table contacts where userid = @userid
With the results, I need to add records to another table cque
With that though, the varaiable @message may have text in it that I need to replace before adding it to the cque table

So I dont know how this would all work... but logically, I see it

---------------------------------------------------------------------------------
ALTER      PROCEDURE [dbo].[sp_broadcast]
      @userid int,
                @message char(1000)

-- Declare cursor and query contacts table
DECLARE c CURSOR

For Select * from contacts where userid = @userid

open c
WHILE (@@FETCH_STATUS=0) BEGIN

/*
Here's where I get confused
I need to get fields from the cursor and do a replace such as
REPLACE(@message,'-fname-',cursorfnamevalue)
REPLACE(@message,'-lname-',cursorlnamevalue)
REPLACE(@message,'-phone-',cursorphonevalue)
*/
END

close c

deallocate c
-------------------------------------------------------------

I think I'm halfway there... I just dont know how to put it all together... I think what I dont know how to do is how to get the values out of the cursor... not sure

Help!
0
Comment
Question by:Michael Krumpe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:vidnan123
ID: 12234799
I think it is pretty simple....

You could do the following...

---------------------------------------------------------------------------------
ALTER      PROCEDURE [dbo].[sp_broadcast]
     @userid int,
                @message char(1000)

-- Declare cursor and query contacts table
DECLARE c CURSOR FOR
Select fname,lname,phone from contacts where userid = @userid

open c
FETCH NEXT FROM c INTO @fname,@lname, @phone

WHILE (@@FETCH_STATUS=0) BEGIN

       REPLACE(@message,'-fname-',@fname)
       REPLACE(@message,'-lname-',@lname)
       REPLACE(@message,'-phone-',@phone)

FETCH NEXT FROM c INTO @fname,@lname, @phone
END

close c
deallocate c
-------------------------------------------------------------

Let me know if u have further questions...

Here I presume the table contacts has columns like fname,lname,phone...
0
 
LVL 34

Expert Comment

by:arbert
ID: 12234815
Why are you using a cursor?

insert into yourothertable (value1, value2)
select replace(@message,'-fname-', fname),
replace(@message,'-lname-', lname),
replace(@message,'-phone-', phonevalue)
from contacts


FNAME, LNAME, and Phonevalue would be the actual column names from your contacts table...

Brett
0
 
LVL 12

Expert Comment

by:ill
ID: 12234839
--  you must specify all variables from select, when opening cursor. so choose only those, you need and with correct datatype.
ALTER      PROCEDURE [dbo].[sp_broadcast]
     @userid int,
                @message char(1000)
declare @curVariableForCol1 int,@curVariableForCol2 varchar(20),@curVariableForCol3 int
DECLARE c CURSOR
For Select col1, col2,col3, ... from contacts where userid = @userid
open c
fetch next from c into @curVariableForCol1, @curVariableForCol2, @curVariableForCol3 /* from now col1,col2 and col3 are stored in variables declared above */
WHILE (@@FETCH_STATUS=0) BEGIN
/*

REPLACE(@message,'-fname-',cursorfnamevalue)
REPLACE(@message,'-lname-',cursorlnamevalue)
REPLACE(@message,'-phone-',cursorphonevalue)
*/
fetch next from c into @curVariableForCol1, @curVariableForCol2, @curVariableForCol3

END
close c
deallocate c


--rem: but do you really need a cursor?  something like this query is not working?
insert into cque
select clumn1, column2,..., REPLACE(@message,'-fname-',cursorfnamevalue)
from contacts where userid = @userid

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 34

Expert Comment

by:arbert
ID: 12234845
actually, you would have one more value so the columns match:

insert into yourothertable (value1, value2,value3)

value1, value2, value3 are the columns in the table you want to insert into.

Maybe if you expand a little more on what you're trying to do.  I don't see, based on your post, why you need a cursor.  Cursors should be avoided unless needed...
0
 
LVL 5

Expert Comment

by:hkamal
ID: 12235354
Agree with arbert. I don't think you need a cursor. A combination of CASE / REPLACE statements should do it
Cursors are slower, more complicated, and usually, avoidable
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 12235682
if message contains all the values:

ALTER      PROCEDURE [dbo].[sp_broadcast]
     @userid int,
                @message char(1000)

insert into yourothertable (column)
select replace(replace(replace(@message,'-fname-', fname),'-lname-', lname),'-phone-', phonevalue)
from contacts
where userid = @userid

GO

in the cursor sample, it would be similar, I think you simply miss the assignment:

ALTER      PROCEDURE [dbo].[sp_broadcast]
     @userid int,
                @message char(1000)

-- Declare cursor and query contacts table
DECLARE c CURSOR FOR
Select fname,lname,phone from contacts where userid = @userid

open c
FETCH NEXT FROM c INTO @fname,@lname, @phone

WHILE (@@FETCH_STATUS=0) BEGIN

       SET @message= REPLACE(@message,'-fname-',@fname)
       SET @message= REPLACE(@message,'-lname-',@lname)
       SET @message= REPLACE(@message,'-phone-',@phone)
 
      insert into yourtable ( yourcolumn ) values (@message )

FETCH NEXT FROM c INTO @fname,@lname, @phone
END

close c
deallocate c


Note that for this kind of work, the CURSOR will be MUCH SLOWER than the single-insert version...

CHeers
0
 
LVL 4

Author Comment

by:Michael Krumpe
ID: 12237536
So much great feedback. I only chose a cursor as I didnt know that I could do a replace inside a select, inside an insert statement. I will try that.
0
 
LVL 34

Expert Comment

by:arbert
ID: 12237818
"So much great feedback. I only chose a cursor as I didnt know that I could do a replace inside a select, inside an insert statement. I will try that."

Definately--only think about using a cursor as a last resort :)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question