Solved

Simple Sql Conditional Select

Posted on 2004-09-04
3
860 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL 10 35
Split data on commas and insert into another table in separate records 26 70
SQL query and VBA 5 46
Error when creating an UPDATE Trigger in SQL 6 21
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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 information from SQL Server on Database, Connection and Server properties

860 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