Solved

Simple Sql Conditional Select

Posted on 2004-09-04
3
857 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now