Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help converting a query to stored procedure

Status
Not open for further replies.

patrichek

MIS
Nov 18, 2003
632
US
Hi guys,
many thanks to all who've helped me so far.
I recently upsized my mdb to adp and need help converting this query to a stored procedure:

SELECT F.Date, F.Status, F.Notes, C.FirstName, C.LastName, C.HomePhone
FROM (Contacts AS C INNER JOIN tblFilmStatus AS F ON C.ContactID = F.ContactID) INNER JOIN [SELECT ContactID, Max([Date]) As LastDate FROM tblFilmStatus
WHERE [Date]<Date()-8 AND Status<>'Received' GROUP BY ContactID
]. AS L ON (F.Date = L.LastDate) AND (F.ContactID = L.ContactID)
WHERE F.Status<>'Received';

thanks!
 
Convert your single quotes to double quotes.
Change Date() to
DateAdd(dd, DateDiff(dd, 0, GetDate()), 0)
Change square brackets around SELECT subquery to parentheses and remove trailing period.

tack on

CREATE PROCEDURE Whatever
AS

to the beginning and you should have a really good start.
 
er, sorry, got the first one backward, single quotes is correct (blush). :)
 
HI,
How would I make this statement only give the most recent date. I have older records as well as new for the same person and I want just the latest.
here's my statement so far but i can't figure out the criteria for only the newest records:

ALTER PROCEDURE dbo.filmstatusquery
AS SELECT dbo.tblFilmStatus.Date, dbo.tblFilmStatus.Type, dbo.tblFilmStatus.Status, dbo.tblFilmStatus.Notes, dbo.Contacts.FirstName, dbo.Contacts.LastName,
dbo.Contacts.EmailName, dbo.Contacts.HomePhone
FROM dbo.tblFilmStatus INNER JOIN
dbo.Contacts ON dbo.tblFilmStatus.ContactID = dbo.Contacts.ContactID
WHERE (dbo.tblFilmStatus.Status <> N'Received')

thanks!
 
I believe the default for date arthimetic is days so you should just be able to do the same thing.

WHERE (dbo.tblFilmStatus.Status <> N'Received'
and [Date] < getdate() -8)
 
hmm...its still pulling all the records even the old ones!, any other ideas?
 
Do you only want the last 8 days? Then it needs to be >

[Date] > getdate() -8
 
getdate() is like Now()... there's no equivalent of the Date function. So be careful how exactly you are doing it.

Please explain your data more. What is are the key values of the tables you're working with? How do you relate records in the same table about the same film that are on different dates? And is there a uniquely identifying single column for each record?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top