Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server Multitable Select with IF-Else
    primarykey
    data
    text
    <p>I have the following sql which returns Projects and related Contractors as well as document size info from three different tables. The sequence is: First Contractors are entered in the system, then Projects are entered for each Contractors, during with document sizes (sheets) are entered. So far so good: The following sql returns all projects along with appropriate Contractors and Sheets fine:</p> <pre><code>SELECT dbo.generalcontractors.name, dbo.Projects.*, dbo.sheets.sheetsize, dbo.sheets.sheetid FROM dbo.generalcontractors INNER JOIN dbo.Projects ON (dbo.generalcontractors.uid = dbo.Projects.generalcontractorid) INNER JOIN dbo.sheets ON (dbo.Projects.sheetid = dbo.sheets.sheetid) ORDER BY dbo.Projects.projectdate DESC </code></pre> <p>Now, what needs to happen is that documents for individual projects are entered in a <code>ProjectDocuments</code> table. I have the following code which checks to see documents exist in the <code>ProjectsDocuments</code> table and return appropriate values; currently, the project id is hard-coded and it work. </p> <p>Question is: how can I combine the following sql into the sql above? I am looking for one query result which will show any matching rows from the <code>ProjectsDocument</code> table and if no matching then will show 'Not Found'.</p> <p>This is for SQL Server 2005.</p> <p>Thanks!</p> <pre><code>IF (SELECT COUNT(*) FROM dbo.projectsdocuments, dbo.Projects WHERE projectsdocuments.projectid = Projects.projectid AND Projects.projectid = 5) &gt; 0 SELECT * FROM projectsdocuments ELSE SELECT 'Not Found' AS HighPrice </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload