Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 31st, 2008, 11:30 AM
imax36581 imax36581 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 imax36581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 29 sec
Reputation Power: 0
Sql store procedure for searching two tables

hi my friends
i must create store procedure that search two table at the same time and show the answer with this discription:
on table have start date ,end date and code field and table2 have many fields but the most important one is Room field.
i must search to find any answer that show me all start date and end date values that the code field of table 2 not be the same of room field of table 2
for example i want all answers exept the answer that room=code
i write this:
.
.
.
where startdate<=@sdate and enddate>=edate and room<>code

but in this way it show me all the rows that have this condition and again all he row of table 2(all of code value)
i want to all my conditions used when show me the answers...
thanks

Reply With Quote
  #2  
Old June 2nd, 2008, 08:37 PM
dykebert's Avatar
dykebert dykebert is offline
Contributing User
Click here for more information
 
Join Date: Apr 2008
Posts: 429 dykebert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 30 m 5 sec
Reputation Power: 3
I'm not sure I understand your question.

You have 2 tables. Table 1 has the fields:
Start Date
End Date
Code

Table 2 has the field:
Room

I don't understand what you are trying to get out of the 2 tables. Can you give a small example of data in the 2 tables and what you want the query results to be?

Reply With Quote
  #3  
Old June 4th, 2008, 03:17 AM
imax36581 imax36581 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 imax36581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by dykebert
I'm not sure I understand your question.

You have 2 tables. Table 1 has the fields:
Start Date
End Date
Code

Table 2 has the field:
Room

I don't understand what you are trying to get out of the 2 tables. Can you give a small example of data in the 2 tables and what you want the query results to be?

room,code is a numeric field and startdate and enddate is a bigint field.
i want to have a result that show me all of table2 content exept that room of this table equal to code of table 1
let me show you an example:
table1:
start date ** end date ** code
123456 1265487 102
254687 545475 205
57787 545447 104


table2:
*** *** *** *** room
xxx xxxx xxx xxx 101
xxx xxxx xxx xxx 102
xxx xxxx xxx xxx 103
xxx xxxx xxx xxx 104
xxx xxxx xxx xxx 201
xxx xxxx xxx xxx 202
xxx xxxx xxx xxx 203
xxx xxxx xxx xxx 204
xxx xxxx xxx xxx 205

now i want to have this answer:
*** *** *** *** room
xxx xxxx xxx xxx 101
xxx xxxx xxx xxx 103
xxx xxxx xxx xxx 201
xxx xxxx xxx xxx 202
xxx xxxx xxx xxx 203
xxx xxxx xxx xxx 204


102,104,205 that exist in table2 was not shown.

plz answer me as soon as possible
thanks

Reply With Quote
  #4  
Old June 4th, 2008, 01:29 PM
dykebert's Avatar
dykebert dykebert is offline
Contributing User
Click here for more information
 
Join Date: Apr 2008
Posts: 429 dykebert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 30 m 5 sec
Reputation Power: 3
OK Now I get it. Thanx.

Not a problem

SELECT <field1>, <field2>, ..., Room
FROM <table2> Left Outer Join (SELECT Code FROM <table1> WHERE startdate<=@sdate and enddate>=edate) As CdTbl
ON <table2>.Room = CdTbl.Code
WHERE CdTbl.Code Is Null

Enjoy!

Reply With Quote
  #5  
Old June 6th, 2008, 01:33 AM
imax36581 imax36581 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 imax36581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 29 sec
Reputation Power: 0
thanks a lot !
the result is true but all of the fields like startdate and enddate is empty in gridview.
gridview just show me a code field .
i try to bind startdate and enddate for gridview and again the result is empty field.
how can i fix this?

Reply With Quote
  #6  
Old June 6th, 2008, 08:38 AM
dykebert's Avatar
dykebert dykebert is offline
Contributing User
Click here for more information
 
Join Date: Apr 2008
Posts: 429 dykebert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 30 m 5 sec
Reputation Power: 3
They are blank becuase they are not part of the SELECT clause. In order to have them returned as part of the recordset you'll need to add them to the SELECT clause like so:

SELECT <field1>, <field2>, ..., Room, @sdate, edate
FROM <table2> Left Outer Join (SELECT Code, @sdate, edate FROM <table1> WHERE startdate<=@sdate and enddate>=edate) As CdTbl
ON <table2>.Room = CdTbl.Code
WHERE CdTbl.Code Is Null


Since sdate is a parameter you might be able to get away with just adding them to the first SELECT (the one with Room) you might want to simplify the expression by trying it.

Reply With Quote
  #7  
Old June 7th, 2008, 07:33 AM
imax36581 imax36581 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 imax36581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 29 sec
Reputation Power: 0
i try this but when i add @sdate to the first select an error appear that say must declare the variable "@sdate"
im adding without @ and i have no error but again the fields is empty
...?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Sql store procedure for searching two tables


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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.


© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek