Solved

SQL - What am I doing wrong in this SP

Posted on 2014-07-29
20
128 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
[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
  • 10
  • 8
20 Comments
 
LVL 66

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 49

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 49

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 49

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 49

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 49

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 49

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 49

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 49

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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