Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Looping in stored procedure

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2008-04-25 : 16:20:20
Hi, I need to code a stored procedure. It will select all the rows from a table. I then need some sort of looping mechanism to filter through all the rows. It is too difficult to handle the filtering with a simple 'WHERE' clause. I will have to do some data manipulation on fields within the rows to determine which ones I could use. I need for the SP to return all the rows which qualify. Could someone provide me with a link and/or some skeleton code to give me somewhere to start on how to code the SP?

Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 16:21:24
You need to show us via sample data what you intend to do.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2008-04-25 : 16:32:53
Okay. Here is my SP thus far.

ALTER procedure [taser_iuser].[tmptable_query] (@condition_cl varchar(100)) as begin declare @sSQL varchar(2000) set @sSQL='select * from #personnel_table' exec(@sSQL) end

I need to loop through this entire table. For each row I need to determine if the individual is >= 18 years of age. Within this table is a date of birth (DOB) field. I don't think the DATEDIFF function inside of a 'WHERE' clause can handle this so I have to figure it out some other way. If they are >= 18 then I need to keep the row. Once looping is complete, I need to return this resultset.

Does this help any? Thanks.
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2008-04-25 : 16:48:11
create table #test (name varchar(25), dob datetime)

insert into #test values('bill', '1/1/1982')
insert into #test values('rick', '1/1/1990')
insert into #test values('dave', '1/1/1992')
insert into #test values('russ', '1/1/1955')
insert into #test values('randy', '1/1/1987')
insert into #test values('Van', '1/1/1997')
insert into #test values('jim', '1/1/1967')

select * from #test where datediff(yyyy,dob,getdate()) >= 18

select * from #test where datediff(dd,dob,getdate()) >= (365.25*18)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 16:49:00
fralo, why are you using dynamic SQL?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2008-04-25 : 17:02:43
Tara, my SP actually needs to receive a dynamically created 'WHERE' clause like such:

set @sSQL='select * from #temp_table' +@condition_cl

I simply took it out of my original post on this forum so as to eliminate it as a factor for discussion. But yes, to be honest, it is something I will have to take into consideration. The where clause generated depends on user input in a .NET application I'm writing. They can enter one to many textboxes. Based on which ones they choose will determinely what the clause looks like. So it needed to be dynamic.

Hope I'm making sense..
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 17:09:51
You should read this article then, which shows you how to achieve a dynamic WHERE clause without dynamic SQL:
http://www.sqlteam.com/article/implementing-a-dynamic-where-clause

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-25 : 17:18:30
Duplicate post
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=101747



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2008-04-25 : 17:25:37
Sorry for the duplicate post. It's just that whenever I did the original post I thought that I could handle my issue with an innovative 'WHERE' clause, and not using a gigantic function as I learned. So I decided to start a new thread. Apologies.

So Tara, thanks for your help. But in addition to the dynamic sql article, I still could use some help with the structural format of how my SP should look to handle my original request.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 17:37:12
Why does it matter how big the function is? You don't need to write it or understand it, just create it in your database and then use it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2008-04-28 : 11:36:55
I have been testing with Van's suggestion and it seems to be what I'm looking for.

select * from #test where datediff(dd,dob,getdate()) >= (365.25*18)

By testing on the day level of the date I have received accurate results. Thanks to you all for you suggestions.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-28 : 16:44:57
For getting real birthdays, and not just an average date
please read
http://www.sqlteam.com/article/datediff-function-demystified

there is a function at bottom.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -