Solved

Simple Sql Conditional Select

Posted on 2004-09-04
3
859 Views
Last Modified: 2012-05-05
I have a table of the following type:

int Linkid
int LinkType
datetime LinkChangeddate
datetime LinkCheckeddate

How can I write a stored procedure which selects rows based on LinkType's Value.

For each row, it is returned if:

a. If Linktype = 1 and Today-LinkCheckeddate > 3 days
b. If Linktype = 2 and Today-LinkCheckeddate > 7 days
b. If Linktype = 3 and LinkChangeddate>LinkCheckeddate

Thanks!

L.Agmon.
0
Comment
Question by:l_agmon
  • 2
3 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11982179
You'll need to change the table name:

CREATE PROCEDURE up_LinkCheckDate @LinkType tinyint AS

IF @LinkType = 1 OR @LinkType = 2
 SELECT Linkid, LinkType, LinkChangeddate, LinkCheckeddate
 FROM LinkTable
 WHERE DateDiff(d,GetDate(),LinkCheckeddate) > CASE WHEN @LinkType = 1 THEN 3 ELSE 7 END
ELSE
 SELECT Linkid, LinkType, LinkChangeddate, LinkCheckeddate
 FROM LinkTable
 WHERE LinkChangeddate > LinkCheckeddate


--sample to use the procedure
--EXECUTE up_link 1
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11982237
Oops. Error on my sample to execute it. Instead use:

EXEC up_LinkCheckDate 1
EXEC up_LinkCheckDate 2
or
EXEC up_LinkCheckDate 3
0
 
LVL 3

Accepted Solution

by:
alexpreston earned 50 total points
ID: 11983250
Surely you want to return them all at once, so don't want to pass the LinkType as a parameter?

CREATE PROCEDURE spSomething AS

SELECT
      Linkid, LinkType, LinkChangeddate, LinkCheckeddate
FROM
      [tablename]
WHERE
      ( LinkType = 1 AND DATEDIFF(d,LinkCheckedDate,getdate())>3 )
      OR ( LinkType = 2 AND DATEDIFF(d,LinkCheckedDate,getdate())>7 )
      OR ( LinkType = 3 AND LinkChangedDate > LinkCheckedDate )

GO
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question