Solved

SQL - What am I doing wrong in this SP

Posted on 2014-07-29
20
125 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 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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