Solved

SQL - What am I doing wrong in this SP

Posted on 2014-07-29
20
120 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now