We help IT Professionals succeed at work.
Get Started

Optimizing storedprocedure

271 Views
Last Modified: 2010-03-19

I am using Following stored procedure and function to read vendor values.Is there any way to optmize these.


CREATE FUNCTION dbo.fnVendorRead ()  
RETURNS TABLE AS  
RETURN (SELECT VENDOR_KEY,
      VENDOR_NAME,
      VENDOR_NAME + ' (' + VENDOR_KEY + ')' AS 'Vendor Formatted Name',      
      FOB,
      TERMS,
      CRLIMIT,
      ACCTNUM
FROM dbo.VENDOR
UNION
SELECT VENDOR_KEY,
      VENDOR_NAME,
      VENDOR_NAME + ' (' + VENDOR_KEY + ')' AS 'Vendor Formatted Name',      
      FOB,
      TERMS,
      '',
      ''
FROM dbo.TempVendor)



CREATE PROCEDURE spVendorRead
@pVendorID VARCHAR(256)
AS

DECLARE @Phone INT
DECLARE @Fax INT
DECLARE @Cell INT

SET @Phone=1
SET @Fax=2
SET @Cell=3

SELECT VENDOR_KEY,
      VENDOR_NAME,
      VENDOR_NAME + ' (' + VENDOR_KEY + ')' AS 'Vendor Formatted Name',      
      FOB,
      TERMS,
      CRLIMIT,
      ACCTNUM,
      BuyerID,
      STATUS
FROM dbo.VENDOR
LEFT JOIN dbo.BuyerVendor ON VENDOR_KEY = VendorID
WHERE VENDOR_KEY = @pVendorID
UNION
SELECT VENDOR_KEY,
      VENDOR_NAME,
      VENDOR_NAME + ' (' + VENDOR_KEY + ')' AS 'Vendor Formatted Name',      
      FOB,
      TERMS,
      '',
      '',
      BuyerID,
      ''
FROM dbo.TempVendor
LEFT JOIN dbo.BuyerVendor ON VENDOR_KEY = dbo.BuyerVendor.VendorID
WHERE VENDOR_KEY = @pVendorID
ORDER BY 'Vendor Formatted Name'

SELECT dbo.Address.AddressID,
      OwnerID,
      REPLACE(Address,CHAR(253),' ') AS 'ADDR',
      City,
      State,
      Zip,
      Country,
      BuyerID
FROM dbo.Address
LEFT JOIN dbo.BuyerAddress ON dbo.Address.AddressID=dbo.BuyerAddress.AddressID
WHERE OwnerID  = @pVendorID
ORDER BY OwnerID ASC, dbo.Address.LastModifiedDate DESC

SELECT dbo.Contact.ContactID,
      OwnerID,
      ISNULL(Contact,'') AS 'Contact',
      BuyerID
FROM dbo.Contact
LEFT JOIN dbo.BuyerContact ON dbo.Contact.ContactID=dbo.BuyerContact.ContactID
WHERE OwnerID   = @pVendorID
ORDER BY OwnerID ASC,  dbo.Contact.LastModifiedDate DESC

SELECT PhoneID AS 'PhoneID',
      OwnerID AS 'ContactID',
      Phone
FROM dbo.Phone
WHERE OwnerID IN
(SELECT dbo.Contact.ContactID
FROM dbo.Contact
LEFT JOIN dbo.BuyerContact ON dbo.Contact.ContactID=dbo.BuyerContact.ContactID
WHERE OwnerID IN
(SELECT VENDOR_KEY FROM fnVendorRead ()   WHERE VENDOR_KEY = @pVendorID
))
AND PhoneTypeID = @Phone
ORDER BY 'ContactID' ASC,  dbo.Phone.LastModifiedDate DESC

SELECT PhoneID AS 'FaxID',
      OwnerID AS 'ContactID',
      Phone AS 'Fax'
FROM dbo.Phone
WHERE OwnerID IN
(SELECT dbo.Contact.ContactID
FROM dbo.Contact
LEFT JOIN dbo.BuyerContact ON dbo.Contact.ContactID=dbo.BuyerContact.ContactID
WHERE OwnerID IN
(SELECT VENDOR_KEY FROM fnVendorRead ()   WHERE VENDOR_KEY = @pVendorID
))
AND PhoneTypeID = @Fax
ORDER BY 'ContactID' ASC,  dbo.Phone.LastModifiedDate DESC

SELECT PhoneID AS 'CellID',
      OwnerID AS 'ContactID',
      Phone AS 'Cell'
FROM dbo.Phone
WHERE OwnerID IN
(SELECT dbo.Contact.ContactID
FROM dbo.Contact
LEFT JOIN dbo.BuyerContact ON dbo.Contact.ContactID=dbo.BuyerContact.ContactID
WHERE OwnerID IN
(SELECT VENDOR_KEY FROM fnVendorRead ()   WHERE VENDOR_KEY = @pVendorID
))
AND PhoneTypeID = @Cell
ORDER BY 'ContactID' ASC,  dbo.Phone.LastModifiedDate DESC

SELECT OnlineAddressID,
      OwnerID AS 'ContactID',
      OnlineAddress
FROM dbo.OnlineAddress
WHERE OwnerID IN
(SELECT dbo.Contact.ContactID
FROM dbo.Contact
LEFT JOIN dbo.BuyerContact ON dbo.Contact.ContactID=dbo.BuyerContact.ContactID
WHERE OwnerID IN
(SELECT VENDOR_KEY FROM fnVendorRead ()   WHERE VENDOR_KEY = @pVendorID
))
ORDER BY 'ContactID' ASC,  dbo.OnlineAddress.LastModifiedDate DESC
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE