Solved

SQL - What am I doing wrong in this SP

Posted on 2014-07-29
20
122 Views
Last Modified: 2014-07-30
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[GetEmailInformation]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetEmailInformation];
GO
CREATE PROCEDURE [dbo].[GetEmailInformation]
(
	@PersonnelID INT,
	@AbandonedVehicleFinderID INT
)
AS
BEGIN
	  Select
	  [Personnel].[FirstName],
	  [Personnel].[LastName],
	  [Personnel].[Phone1],	 
	  [Personnel].[Email],
	  [VehicleFinder].[Make],
	  [VehicleFinder].[Model],
	  [VehicleFinder].[Year],	 
	  [VehicleFinder].[MatchDate]	  
FROM [dbo].[Personnel] 
JOIN [dbo].[VehicleFinder] ON  [Personnel].[AbandonedVehicleFinderID] = [VehicleFinder].[AbandonedVehicleFinderID]
JOIN [dbo].[VIN_Audit] ON  [Personnel].[AbandonedVehicleFinderID] = [VehicleFinder].[AbandonedVehicleFinderID]
WHERE [Personnel].[PersonnelID] = @PersonnelID AND [Personnel].[AbandonedVehicleFinderID] = @AbandonedVehicleFinderID AND [VIN_Audit].[EmailSent] = 0

END
GO

Open in new window

0
Comment
Question by:CAMPzxzxDeathzxzx
  • 10
  • 8
20 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40228301
Stating the error message you received when this SP is executed would be an excellent idea.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228387
It might not be an error? perhaps it isn't meeting your functional requirements? What are you trying to achieve?

There isn't much detail for us to work with.
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228418
Hey Portlet - I owe you money:)
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228421
for what? I didn't do anything, promise:)

what's the problem that caused this question? is it an error?
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228425
2
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228427
My brain is cooked.  I can't see why I'm getting 4 rows when [VIN_Audit].[EmailSent] = 0
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228432
try amending the second join (look carefully)
SELECT
      [Personnel].[FirstName]
    , [Personnel].[LastName]
    , [Personnel].[Phone1]
    , [Personnel].[Email]
    , [VehicleFinder].[Make]
    , [VehicleFinder].[Model]
    , [VehicleFinder].[Year]
    , [VehicleFinder].[MatchDate]
FROM [dbo].[Personnel]
      JOIN [dbo].[VehicleFinder]
                  ON [Personnel].[AbandonedVehicleFinderID] = [VehicleFinder].[AbandonedVehicleFinderID]
      JOIN [dbo].[VIN_Audit]
                  ON [Personnel].[AbandonedVehicleFinderID] = [VIN_Audit].[AbandonedVehicleFinderID]
WHERE [Personnel].[PersonnelID] = @PersonnelID
      AND [Personnel].[AbandonedVehicleFinderID] = @AbandonedVehicleFinderID
      AND [VIN_Audit].[EmailSent] = 0
;

Open in new window

you were not actually joining personnel directly to audit
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228445
afraid I cannot read your mind clearly

I'm guessing for the last 2 lines you really want one line?

what happens with this?

select * from
FROM [dbo].[Personnel]
where firstName = 'Marc' and LastName = 'Suther' and Phone1 = ' put phone no here '

Is there more than one record?

is I have to ask - because I cannot see your data

---------------
btw: are those real names and phone numbers?
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228465
Sorry, I need just 1 line returned.  In code, I roll through the 2 lines separately.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40228480
could you answer all questions?
what happens with this?

select * from
FROM [dbo].[Personnel]
where firstName = 'Marc' and LastName = 'Suther' and Phone1 = ' put phone no here '

Is there more than one record?

is I have to ask - because I cannot see your data
sorry, but can yu imagine trying to answer questions about my database - and having no idea what the data was?
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228528
No there are 2 records there.   Mine and another.  Sorry, there are real passwords in the table.
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228540
I got it.

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[GetEmailInformation]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetEmailInformation];
GO
CREATE PROCEDURE [dbo].[GetEmailInformation]
(
	@PersonnelID INT,
	@AbandonedVehicleFinderID INT
)
AS
BEGIN
	 SELECT
      [Personnel].[FirstName]
    , [Personnel].[LastName]
    , [Personnel].[Phone1]
    , [Personnel].[Email]
    , [VehicleFinder].[Make]
    , [VehicleFinder].[Model]
    , [VehicleFinder].[Year]
    , [VehicleFinder].[MatchDate]
FROM [dbo].[Personnel]
      JOIN [dbo].[VehicleFinder]
                  ON [Personnel].[AbandonedVehicleFinderID] = [VehicleFinder].[AbandonedVehicleFinderID]
      JOIN [dbo].[VIN_Audit]
                  ON [VehicleFinder].[VehicleFinderID] = [VIN_Audit].[VehicleFinderID]
WHERE [Personnel].[PersonnelID] = @PersonnelID
      AND [Personnel].[AbandonedVehicleFinderID] = @AbandonedVehicleFinderID
      AND [VIN_Audit].[EmailSent] = 0

END
GO

Open in new window

0
 

Author Closing Comment

by:CAMPzxzxDeathzxzx
ID: 40228541
Thanks
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228542
IF
select * from
FROM [dbo].[Personnel]
where firstName = 'Marc' and LastName = 'Suther' and Phone1 = ' put phone no here '
produces 2 records

so why are you surprised that the final outcome is 2 records? it should.

I have no idea what " Mine and another." means however
2 recordsIf you really MUST return a single record, then you have to take one of these paths:

use row_number() as select where that equals 1
use a group by
use distinct

If you leave out Make, Model, Year then distinct might work. Otherwise try row_number()

something along these lines
SELECT
      *
FROM (
            SELECT
                  [Personnel].[FirstName]
                , [Personnel].[LastName]
                , [Personnel].[Phone1]
                , [Personnel].[Email]
                , [VehicleFinder].[Make]
                , [VehicleFinder].[Model]
                , [VehicleFinder].[Year]
                , [VehicleFinder].[MatchDate]
                , ROW_NUMBER() OVER (PARTITION BY [Personnel].[FirstName]
                  , [Personnel].[LastName]
                  , [Personnel].[Phone1]
                  , [Personnel].[Email]
                  ORDER BY
                  [VehicleFinder].[Make]
                  , [VehicleFinder].[Model]
                  , [VehicleFinder].[Year]
                  , [VehicleFinder].[MatchDate]
                  ) AS rn
            FROM [dbo].[Personnel]
                  JOIN [dbo].[VehicleFinder]
                              ON [Personnel].[AbandonedVehicleFinderID] = [VehicleFinder].[AbandonedVehicleFinderID]
                  JOIN [dbo].[VIN_Audit]
                              ON [Personnel].[AbandonedVehicleFinderID] = [VIN_Audit].[AbandonedVehicleFinderID]
            WHERE [Personnel].[PersonnelID] = @PersonnelID
                  AND [Personnel].[AbandonedVehicleFinderID] = @AbandonedVehicleFinderID
                  AND [VIN_Audit].[EmailSent] = 0
      ) AS sq
WHERE rn = 1
;

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228544
ok, so it's done. thanks for the grading, cheers, Paul
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228545
JOIN [dbo].[VIN_Audit]
                  ON [VehicleFinder].[VehicleFinderID] = [VIN_Audit].[VehicleFinderID]
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40228549
yes, you didn't see that? http:#a40228432
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228551
Brain dead
0
 

Author Comment

by:CAMPzxzxDeathzxzx
ID: 40228902
Okee Dokee
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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