Solved

update columns with variable from within cursor

Posted on 2008-10-29
7
880 Views
Last Modified: 2012-06-21
Can please someone look at my code with my comments and see what could I do be doing wrong.  Where do i need to call my SP. If I call it after the cursor is finished it works but then i don't achieve my results since I have some other variables that need to be validated as well for doing any updates on my column.  Any suggestion would be appreciated.  Thanks
------bunch of variables---------
 
select @PromoDownloadMinDt = min(Download_Date) from tblA
select @PromoDownloadMaxDt = max(Download_Date) from tblA
set @NumMonths = datediff(mm,@PromoDownloadMinDt,@PromoDownloadMaxDt)
set @column = 'M'+ convert(char(5),@NumMonths)
 
Declare MyCur cursor for
select distinct EOM from tblA
open MyCur 
 
fetch next from MyCur into @PromoMonYr
 
While @@FETCH_STATUS = 0 
Begin 
	
----------the insert works fine------------
 
	insert into tbl_Report(col1,col2,....)
	select distinct (co1l,col2,...)
	from tblA
where quantity =1 and eom ='mar 08'
and eom = @PromoMonYr
 
 
----------this part doesn't do the update------------
	while  @numMonths > 0
		begin 
			Exec SP_Update @Column
			---the above SP has a dynamic query for updating columns dynamically
			---this doen't work here in this section if I put it all the way
			--at the end after the cursor it works
			set @NumMonths = @NumMonths - 1
			set @column = 'M'+ convert(char(5),@NumMonths)
		end 
 
 
fetch next from MyCur into @PromoMonYr
 
end
CLOSE MyCur
DEALLOCATE MyCur

Open in new window

0
Comment
Question by:zafridi
[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
7 Comments
 
LVL 6

Expert Comment

by:openshac
ID: 22832226
What's the error message?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22832256
sp_update is a BAD name for a procedure.  Two reasons, user procedures should not start with sp_.  Secondly, update what?  It's too generic.

Ok... moving on.
SP_Update @Column

You are updating column, but you're not saying what record in the table to update the column for and to what value.  I don't know what you have in that procedure, but I think you may be missing some parameters?  
0
 

Author Comment

by:zafridi
ID: 22832438
The name isn't SP_Update I just changed it.  The Stored procedure dynamically updates a specific column based on the parameter it gets from the above query.  For now im just updating the columns to value 1.  There is no issue with SP.  Following is the code for the stored procedure

ALTER  proc DynamicUpdate
@column char(4)

as
declare @SQL varchar (2000)
Set @SQL = 'update tbl_Report' +
      ' set '+ @column + ' =1' +
      ' from tbl_Report as r' +
      ' join Products as p' +
      ' on r.acct = p.acct and p.quantity =1'

Execute (@Sql)

The existing code just doesn't update the table at all but if i call the stored procedure after the cursor is closed then it works.
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22832553
But every time you run that procedure, you are updating that column for every record.  You have no context of "only run this update against a single record".
0
 

Author Comment

by:zafridi
ID: 22832715
for now i'm updating all the records but later on i will tune it to update the ones the i want to update. But that's not the issue right now.  What I want right now is to somehow run it within the cursor and update a COLUMN based on the passed parameter(which tells the SP what column to update). Right now it doesn't update any column whatsoever. I hope it makes it clear what i'm looking for.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 22832878
You are decrementing @numMonths to 0 the first pass through the cursor and not resetting it to the datediff.  It therefore will never be >0 after the first fetch/insert/update completes.
----------this part doesn't do the update------------
        while  @numMonths > 0
                begin 
                        Exec SP_Update @Column
                        ---the above SP has a dynamic query for updating columns dynamically
                        ---this doen't work here in this section if I put it all the way
                        --at the end after the cursor it works
                        set @NumMonths = @NumMonths - 1
                        set @column = 'M'+ convert(char(5),@NumMonths)
                en

Open in new window

0
 
LVL 7

Expert Comment

by:Cedric_D
ID: 22833174
1) your WHILE cycle will execute once, at the first CURSOR iteration.
    On next step @numMonths will become 0 forever.
    I think u shud update to use 1 additional @i variable:

-- this is inside cursor:

.....
       set @i = @numMonths
       while  @i > 0
                begin
                        set @column = 'M'+ convert(char(5),@i)
                        Exec SP_Update @Column
                        ---the above SP has a dynamic query for updating columns dynamically
                        ---this doen't work here in this section if I put it all the way
                        --at the end after the cursor it works
                        set @i = @i - 1
                end
.......





2) :) make sure you dont forget to remove debug string:

where quantity =1 and eom ='mar 08'
and eom = @PromoMonYr

soud be
where quantity =1 and eom = @PromoMonYr  ?

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

724 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