473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

loop in a SQL stored procedure

I need to loop in a stored procedure for the variable @hdnIncomeIDNO1 and
@IncomeType_CODE11 to @hdnIncomeIDNO10 and @IncomeType_CODE10

Instead of writing the upadate statement 10 time(as I did below), Is there
any way where I can loop the @hdnIncomeIDNO
DECLARE @IncomeType_CODE CHAR(5)
BEGIN
SET @IncomeType_CODE=@IncomeType_CODE1
UPDATE ClientIncome_T1
SET
IncomeType_CODE=@IncomeType_CODE,
WHERE
ClientIncome_IDNO=@hdnIncomeIDNO1
END
Jun 27 '08 #1
16 1558
"iHavAQuestion" <iH***********@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
Instead of writing the upadate statement 10 time(as I did below), Is there
any way where I can loop the @hdnIncomeIDNO
Firstly, this isn't strictly an ASP.NET question per se - you'd probably get
a better response in one of the SQL Server newsgroups...

Anyway, if you need to do this all within SQL Server, you will need to
investigate dynamic SQL and sp_executesql - open up SQL BOL and do a search
for sp_executesql...

As for looping, something like WHILE...BREAK...CONTINUE would probably do
the trick - again, it's all in BOL.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #2
Here's a short example of looping in sql.
declare @var1 int
declare @var2 int

set @var1 = 10
set @var2 = 1
while @var2 < @var1
begin
set @var2 = @var2 + 1
print 'Now at: ' + cast(@var2 as char)
end
On Thu, 12 Jun 2008 11:39:02 -0300, iHavAQuestion
<iH***********@discussions.microsoft.comwrote:
I need to loop in a stored procedure for the variable @hdnIncomeIDNO1 and
@IncomeType_CODE11 to @hdnIncomeIDNO10 and @IncomeType_CODE10

Instead of writing the upadate statement 10 time(as I did below), Is
there
any way where I can loop the @hdnIncomeIDNO
DECLARE @IncomeType_CODE CHAR(5)
BEGIN
SET @IncomeType_CODE=@IncomeType_CODE1
UPDATE ClientIncome_T1
SET
IncomeType_CODE=@IncomeType_CODE,
WHERE
ClientIncome_IDNO=@hdnIncomeIDNO1
END


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jun 27 '08 #3
sql does not support looping thru passed parameters, nor does it have an
array parameter like c#. a better approach would be to pass an xml parameter
with the data. then you could do a set operation with the xml, or loop thru
the xml if you want to handle a statement at a time.

declare @xml xml
set @xml = '
<params>
<param><id>1</id><value>hello</value></param>
<param><id>2</id><value>bye</value>
</param>
</params>
'
UPDATE ClientIncome_T1
SET IncomeType_CODE=value
from ClientIncome_T1
join (
select
params.id.query('id').value('.','varchar(50)') as id,
params.id.query('value').value('.','varchar(50)') as value
from @xml.nodes('//param') as params(id))
) p on ClientIncome_IDNO=id
-- bruce (sqlwork.com)
"iHavAQuestion" wrote:
I need to loop in a stored procedure for the variable @hdnIncomeIDNO1 and
@IncomeType_CODE11 to @hdnIncomeIDNO10 and @IncomeType_CODE10

Instead of writing the upadate statement 10 time(as I did below), Is there
any way where I can loop the @hdnIncomeIDNO
DECLARE @IncomeType_CODE CHAR(5)
BEGIN
SET @IncomeType_CODE=@IncomeType_CODE1
UPDATE ClientIncome_T1
SET
IncomeType_CODE=@IncomeType_CODE,
WHERE
ClientIncome_IDNO=@hdnIncomeIDNO1
END
Jun 27 '08 #4
RE:
<< again, it's all in BOL >>

Mark, this sort of response is utterly unhelpful, at best, and easily
perceived as condescending. These news groups are one of many resources,
including BOL, google, printed books, etc. Pointing out that the answer is
easily found in an alternative resource usually isn't helpful.

Think about it - you could answer *any* question in a news group like this:
"just google it" or "look it up in BOL" which is really a variation of
"RTFM". These sorts of answers are *not* the stuff of a Microsoft-designated
"MVP".

If you are so incredibly inconvenienced by a question that you feel is
readily available via some other resource, then you could just pass and not
respond at all. That would definitely help the signal-to-noise ratio in this
NG.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"iHavAQuestion" <iH***********@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
>Instead of writing the upadate statement 10 time(as I did below), Is
there
any way where I can loop the @hdnIncomeIDNO

Firstly, this isn't strictly an ASP.NET question per se - you'd probably
get a better response in one of the SQL Server newsgroups...

Anyway, if you need to do this all within SQL Server, you will need to
investigate dynamic SQL and sp_executesql - open up SQL BOL and do a
search for sp_executesql...

As for looping, something like WHILE...BREAK...CONTINUE would probably do
the trick - again, it's all in BOL.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Jun 27 '08 #5
"Jordan S." <A@B.comwrote in message
news:eB**************@TK2MSFTNGP05.phx.gbl...

[top-posting corrected]
RE:
<< again, it's all in BOL >>

Mark, this sort of response is utterly unhelpful, at best, and easily
perceived as condescending.
You're entitled to your opinion, but I don't agree with it...

Firstly, I informed the OP that he'd posted in the wrong newsgroup,
suggesting that he try a more appropriate newsgroup which might get him a
better answer.

Secondly, I then gave him suggestions of what to use in SQL Server to solve
his problem, namely sp_executesql and WHILE...BREAK...CONTINUE.

Finally, I told him where to find more information on this.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #6
First I tend to agree with Mark that you are in the wrong group. While his
reply could be considered somewhat condescending to some, he has a point -
people need to learn to help themselves, or they will never acquire the
skills to become professsional developers.

The problem you have is probably one of how to pass delimited strings of one
or more parameters, split the strings into Table variables inside your
stored proc, and iterate over these to perform the multiple inserts. You can
use an easy to find User Defined Function called "fn_Split" to handle the
first problem. However, a discussion of Table Variables, how to create them
with primary keys that enable looping and so on is beyond the scope of a
newsgroup post and will simply require some serious study on your part.
Hence "BOL-RTFM".
Peter

"iHavAQuestion" <iH***********@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
>I need to loop in a stored procedure for the variable @hdnIncomeIDNO1 and
@IncomeType_CODE11 to @hdnIncomeIDNO10 and @IncomeType_CODE10

Instead of writing the upadate statement 10 time(as I did below), Is there
any way where I can loop the @hdnIncomeIDNO
DECLARE @IncomeType_CODE CHAR(5)
BEGIN
SET @IncomeType_CODE=@IncomeType_CODE1
UPDATE ClientIncome_T1
SET
IncomeType_CODE=@IncomeType_CODE,
WHERE
ClientIncome_IDNO=@hdnIncomeIDNO1
END
Jun 27 '08 #7
I'll have to second this sentiment. Reading some of his replies, it looks
like I'm not the only one Mark seems more interesting in arguing with that
trying to be helpful.

Jonathan

"Jordan S." <A@B.comwrote in message
news:eB**************@TK2MSFTNGP05.phx.gbl...
RE:
<< again, it's all in BOL >>

Mark, this sort of response is utterly unhelpful, at best, and easily
perceived as condescending. These news groups are one of many resources,
including BOL, google, printed books, etc. Pointing out that the answer is
easily found in an alternative resource usually isn't helpful.

Think about it - you could answer *any* question in a news group like
this: "just google it" or "look it up in BOL" which is really a
variation of "RTFM". These sorts of answers are *not* the stuff of a
Microsoft-designated "MVP".

If you are so incredibly inconvenienced by a question that you feel is
readily available via some other resource, then you could just pass and
not respond at all. That would definitely help the signal-to-noise ratio
in this NG.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>"iHavAQuestion" <iH***********@discussions.microsoft.comwrote in
message news:2A**********************************@microsof t.com...
>>Instead of writing the upadate statement 10 time(as I did below), Is
there
any way where I can loop the @hdnIncomeIDNO

Firstly, this isn't strictly an ASP.NET question per se - you'd probably
get a better response in one of the SQL Server newsgroups...

Anyway, if you need to do this all within SQL Server, you will need to
investigate dynamic SQL and sp_executesql - open up SQL BOL and do a
search for sp_executesql...

As for looping, something like WHILE...BREAK...CONTINUE would probably do
the trick - again, it's all in BOL.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Jun 27 '08 #8
On Jun 12, 10:39*am, iHavAQuestion
<iHavAQuest...@discussions.microsoft.comwrote:
I need to loop in a stored procedure for the variable @hdnIncomeIDNO1 and
@IncomeType_CODE11 to @hdnIncomeIDNO10 and @IncomeType_CODE10

Instead of writing the upadate statement 10 time(as I did below), Is there
any way where I can loop the @hdnIncomeIDNO

DECLARE @IncomeType_CODE * * *CHAR(5)
BEGIN
* * * * SET @IncomeType_CODE=@IncomeType_CODE1
* * * * * * UPDATE ClientIncome_T1
* * * * * * * * *SET
* * * * * * * * *IncomeType_CODE=@IncomeType_CODE,
* * * * * * WHERE
* * * * * * * * *ClientIncome_IDNO=@hdnIncomeIDNO1
END
if @hdnIncomeIDNO1 contains comma-delimited list of values, then you
can do something like this:

declare @update_command varchar(6666)
set @update_command = ' update ClientIncome_T1' +
' set IncomeType_CODE=' +
@IncomeType_Code +
' where ClientIncome_IDNO in ('
+ @hdnIncomeIDNO1 + ')'
... more at http://www.siccolo.com/articles.asp
Jun 27 '08 #9
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:u4**************@TK2MSFTNGP06.phx.gbl...
I'll have to second this sentiment. Reading some of his replies, it looks
like I'm not the only one Mark seems more interesting in arguing with that
trying to be helpful.
I totally disagree with this. I've always found Mark Rae and all the other
MVP's in here to be extremely helpful.

You on the other hand appear to have a serious issue with anyone who
disagrees with you.

DJ
Jun 27 '08 #10
I should know better and I have no idea what business this is of yours, but
I challenge you to back up the statement that I have a serious issue with
anyone who disagrees with me.

BTW, my comments here about Mark were confirming someone else's comments
about him. So if you're suggesting it's just me, you're way off.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"David Jackson" <so*****@somewhere.comwrote in message
news:e3****************@TK2MSFTNGP03.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:u4**************@TK2MSFTNGP06.phx.gbl...
>I'll have to second this sentiment. Reading some of his replies, it looks
like I'm not the only one Mark seems more interesting in arguing with
that trying to be helpful.

I totally disagree with this. I've always found Mark Rae and all the other
MVP's in here to be extremely helpful.

You on the other hand appear to have a serious issue with anyone who
disagrees with you.

DJ
Jun 27 '08 #11
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:uB**************@TK2MSFTNGP06.phx.gbl...
>>I'll have to second this sentiment. Reading some of his replies, it
looks like I'm not the only one Mark seems more interesting in arguing
with that trying to be helpful.

I totally disagree with this. I've always found Mark Rae and all the
other MVP's in here to be extremely helpful.

You on the other hand appear to have a serious issue with anyone who
disagrees with you.

I challenge you to back up the statement that I have a serious issue with
anyone who disagrees with me.
Your thread "XML Database" is a case in point. You started off by saying
that you wanted database functionality for your web site but didn't want to
use a database and asked if an XML file would be a good idea. You were told
that it wasn't a good idea but then got on your high horse because you had
already decided that you were going to use an XML file regardless of whether
anyone suggested a much better alternative.

Then you threw your toys out of the pram and said that you were leaving,
doubtless hoping that someone would beg you to stay. No-one did, and you're
still here.
Jun 27 '08 #12
David,
Your thread "XML Database" is a case in point. You started off by saying
that you wanted database functionality for your web site but didn't want
to use a database and asked if an XML file would be a good idea. You were
told that it wasn't a good idea but then got on your high horse because
you had already decided that you were going to use an XML file regardless
of whether anyone suggested a much better alternative.
First off, Stan's helpful comments in that same thread turned out to be
exactly right. He even included potential warnings, which just aren't an
issue in my case.

Even though I thought Mark's comments about Access not having a database, a
database being better than even a simple text file, and the usual comments
about top-down posting were off the mark, everything was cool and I welcomed
his replies. It wasn't until the <rolling eyescomment that I took the
liberty of pointing out that he didn't know my requirements better than I
do.
Then you threw your toys out of the pram and said that you were leaving,
doubtless hoping that someone would beg you to stay. No-one did, and
you're still here.
I'm sorry if you think I should just keep my mouth shut when someone starts
calling me names, but I said I was outta here only after being called names
by someone other than Mark, I meant out of that thread, and there was never
any reason for anyone to ask me to "stay." Besides, ASP.NET is my focus now
so I'll be around. In fact, with time, I may change my own MVP status (yet
again) to ASP.NET.

HTH.

Jonathan

Jun 27 '08 #13
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:uO*************@TK2MSFTNGP02.phx.gbl...
>Your thread "XML Database" is a case in point. You started off by saying
that you wanted database functionality for your web site but didn't want
to use a database and asked if an XML file would be a good idea. You were
told that it wasn't a good idea but then got on your high horse because
you had already decided that you were going to use an XML file regardless
of whether anyone suggested a much better alternative.

Even though I thought Mark's comments about Access not having a database,
To be fair, he didn't say that Access doesn't have a database. Quite the
opposite. What he said was that Access isn't a database but rather a tool
for creating database applications, and those database applications use the
Jet database by default. And he's right.
database being better than even a simple text file,
Surely you can't possibly argue that a database is better than a simple text
file for database operations?
and the usual comments about top-down posting were off the mark,
I don't agree. And you can obviously post correctly when you want to.
Jun 27 '08 #14
David,
To be fair, he didn't say that Access doesn't have a database. Quite the
opposite. What he said was that Access isn't a database but rather a tool
for creating database applications, and those database applications use
the Jet database by default. And he's right.
What he said was "there's no such thing as an Access database."

At any rate, I'm sorry, but I will speak up when I think someone is being
unfair. And your original characterization of me in the other thread was
neither fair nor accurate.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #15
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:un**************@TK2MSFTNGP04.phx.gbl...
>To be fair, he didn't say that Access doesn't have a database. Quite the
opposite. What he said was that Access isn't a database but rather a tool
for creating database applications, and those database applications use
the Jet database by default. And he's right.

What he said was "there's no such thing as an Access database."
Yes he did. And he's right about that too.
Jun 27 '08 #16
I'm sorry but I just add to add one final point.

There is a site that has links to people's personal websites at
http://www.edream.org/SiteRSS.aspx?skid=1. Users contribute links to their
own sites and list the customizations they made. One contributer puts the
following as his customizations:

"I built an XML based photo album system that stores the picture meta-data
in an XML file on disk as opposed to the database. This then lets me just
FTP up my pictures."

That user is none other than Scott Guthrie himself. If using XML this way is
good enough for him, maybe I do have a clue to my particular requirements
afterall!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"David Jackson" <so*****@somewhere.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:un**************@TK2MSFTNGP04.phx.gbl...
>>To be fair, he didn't say that Access doesn't have a database. Quite the
opposite. What he said was that Access isn't a database but rather a
tool for creating database applications, and those database applications
use the Jet database by default. And he's right.

What he said was "there's no such thing as an Access database."

Yes he did. And he's right about that too.
Jun 27 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
1
by: Eric Martin | last post by:
Hello, Does anyone know of a way to loop thru a SQL table using code in a stored procedure? I need to go thru each record in a small table and build a string using values from the fields...
0
by: Anthony Robinson | last post by:
I have a stored procedure that needs to loop through a record set, evaluate value, then either perform an operation or move tro the next record - depending on some criteria. Here's the procedure:...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
7
by: Jerry | last post by:
I'm trying to execute a stored procedure in a loop while paging through database table records but the stored procedure isn't running. I get the folowing error: The component 'adodb.connection'...
2
by: Chris Zopers | last post by:
Hello, I've created a stored procedure that loops through a cursor, with the following example code: DECLARE curPeriod CURSOR LOCAL for SELECT * FROM tblPeriods DECLARE @intYear smallint...
3
by: barmatt80 | last post by:
I finally got my call to a stored procedure on our db2 to work. However i might have to change what the stored procedure does, if I cannot get it to work how we want. Which i would like to make it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.