?
Solved

SQL - What am I doing wrong in this SP

Posted on 2014-07-29
20
Medium Priority
?
129 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 2000 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

801 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