Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 20th, 2004, 09:54 AM
Kumaresan Kumaresan is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 17 Kumaresan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 34 sec
Reputation Power: 0
Thumbs up Need help For Using Conditions inside the Stored Procedure?

Dear All,

Let me Explain the Process First:

I have Four parameters to send To Sql server Stored Procedure.

I have Recived all the parameters in the stored procedure successfully.

In that five parameters, i have some parameters as null and some parameters have the value.

I have to select the values from data base table using that values.


when i am using the query in the front end, i can use like below.

(ex:

deptid,desigid,sectionid,gradeid are parameters to select a record from the database.

Sql = "Select * from tblEmployee where EmpCd='Emp001'"
if txtDeptId.Value <> "" then
sql = sql & " and Deptid = "' & txtDeptId.Value & '"
end if
if txtDesigId.Value <> "" then
sql = sql & " and Deptid = "' & txtDesigId.Value & '"
end if
if txtSectionId.Value <> "" then
sql = sql & " and Deptid = "' & txtSectionId.Value & '"
end if
if txtGradeId.Value <> "" then
sql = sql & " and Deptid = "' & txtGradeId.Value & '"
end if

So finally the string sql will have all the combinations of query.

So it is easy to select the records in any combination.

So, I want to do this in stored procedure. i have to send all parameters to stored procedure and i have to do query for all Combinations.

Can i write the stored procedure for that, if yes please give the SP.


Pls, It's very urggently needed for me.


Thank you,
by,
Kumaresan

Reply With Quote
  #2  
Old October 20th, 2004, 11:44 PM
Leslie's Avatar
Leslie Leslie is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Honolulu
Posts: 184 Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 20 h 29 m 2 sec
Reputation Power: 8
Loook at this website:

http://vyaskn.tripod.com/passing_ar..._procedures.htm

which contains several examples of dynamically building your SQL in an stored procedure. You should be able to modify one of these for your needs.

Reply With Quote
  #3  
Old October 27th, 2004, 03:48 PM
HBARBER HBARBER is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 HBARBER User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try this

CREATE PROCEDURE dbo.spSelect @paramDeptID Char(10), @paramDesigId Char(10),

@paramSectionId Char(10), @paramGradeId Char(10)

As

Select * from tblEmployee where EmpCd='Emp001'" And

(CASE WHEN @paramDeptID Is Not Null and Deptid= @paramDeptID THEN 1

WHEN @paramDeptID Is Null THEN 1

ELSE 0

END) = 1 AND

(CASE WHEN @paramDesigId Is Not Null and desigid = @paramDesigId THEN 1

WHEN @paramDesigId Is Null then 1

ELSE 0

END) = 1 AND

(CASE WHEN @paramSectionId Is Not Null and sectionid= @paramSectionId THEN 1

WHEN @paramSectionId Is Null then 1

ELSE 0

END) = 1 AND

(CASE WHEN @paramGradeId Is Not Null and gradeid= @paramGradeId THEN 1

WHEN @paramGradeId Is Null then 1

ELSE 0

END) = 1


Quote:
Originally Posted by Kumaresan
Dear All,

Let me Explain the Process First:

I have Four parameters to send To Sql server Stored Procedure.

I have Recived all the parameters in the stored procedure successfully.

In that five parameters, i have some parameters as null and some parameters have the value.

I have to select the values from data base table using that values.


when i am using the query in the front end, i can use like below.

(ex:

deptid,desigid,sectionid,gradeid are parameters to select a record from the database.

Sql = "Select * from tblEmployee where EmpCd='Emp001'"
if txtDeptId.Value <> "" then
sql = sql & " and Deptid = "' & txtDeptId.Value & '"
end if
if txtDesigId.Value <> "" then
sql = sql & " and Deptid = "' & txtDesigId.Value & '"
end if
if txtSectionId.Value <> "" then
sql = sql & " and Deptid = "' & txtSectionId.Value & '"
end if
if txtGradeId.Value <> "" then
sql = sql & " and Deptid = "' & txtGradeId.Value & '"
end if

So finally the string sql will have all the combinations of query.

So it is easy to select the records in any combination.

So, I want to do this in stored procedure. i have to send all parameters to stored procedure and i have to do query for all Combinations.

Can i write the stored procedure for that, if yes please give the SP.


Pls, It's very urggently needed for me.


Thank you,
by,
Kumaresan

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Need help For Using Conditions inside the Stored Procedure?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway