?
Solved

Simple Sql Conditional Select

Posted on 2004-09-04
3
Medium Priority
?
868 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

649 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