Stored Procedures Tips and tricks

Here I am posting a Stored procedure created using few of the effective techniques that I have been using frequently over the past few days. Here are the ingredients of the SP given below,
  1. At certain point in time we ought to test the application for sanity over the Production machine and don't want the data to be visible later on but for very obvious reasons we can't remove that data since there are a lot of dependencies (Foreign References ) over it. We can hide it using the techniques demonstrated in the SP below in Red Color.
  2. Using the "Cases" in the SQL that can let one output understandable statements based on the given Auto or ID.
  3. It also demonstrated the fact that we try to do most of the data processing work in the SPs rather than totally relying on the code. Generally, most of us use the SP to get our datasets filled with the raw unformatted and unprocessed data whose kinks are ironed out in the code in the later stages. Trivial issues like date formatting, Text/string concatenation and modifications, passing any value in case the required value is null, aliasing . Since after building the exes and the Dlls, we are in complete limbo if all of the sudden a bug appears.

CREATE PROCEDURE [dbo].[GetAllPendingGradings]
@STATUSAUTO INT = NULL
,@sellerAuto INT
AS
SET NOCOUNT ON
BEGIN

DECLARE @IncludeTestData BIT
SET @IncludeTestData = (SELECT TOP (1) IncludeTestData FROM GFCompany)

IF StatusAuto is Not Null

BEGIN

SELECT
DISTINCT (OFFER.offerauto) as offerauto,
ISNULL(FEDEXREFNO,'') AS TRACKNO
,OFFERID
,FIRSTNAME+' ' +LASTNAME AS SELLER
,SELLERID,
OFFER.SHIPMODEAUTO,

CASE OFFER.SHIPMODEAUTO
WHEN 1 THEN 'FAST'
WHEN 2 THEN 'FASTER'
WHEN 3 THEN 'FASTEST'
WHEN 4 THEN 'SHIP MYSELF'
WHEN 5 THEN 'FEDEX 2DAY'
END AS SHIPMODE

,convert(varchar, Offer.[InsDate] ,110)+' '+ Offer.[InsTime]OfferDateTime
,REPLACE(REPLACE(REPLACE(REPLACE(ISNULL(dbo.SellerInfo.DayPhone,'N/A'),'(',''),')',''),'-',''),' ', '') AS ContactNumber,



FROM OFFER
INNER JOIN SELLERINFO ON OFFER.SELLERAUTO =SELLERINFO.SELLERAUTO
INNER JOIN OfferStatusTrack OST ON OST.OFFERAUTO=OFFER.OFFERAUTO
WHERE OST.STATUSAUTO = @STATUSAUTO
AND
OST.trackauto = (SELECT MAX(OST1.trackauto) from offerstatustrack OST1 WHERE OST1.OFFERAUTO=OFFER.OFFERAUTO AND OST1.STATUSAUTO <> 14)
AND
(@IncludeTestData = 1 OR (@IncludeTestData = 0 AND SELLERINFO.Email NOT LIKE '%@e-Bizsoft.com%'))

END
ELSE
BEGIN

DECLARE @tempStatus TABLE(OFFERAUTO NUMERIC(22), STATUSAUTO NUMERIC(22))
INSERT INTO @tempStatus SELECT OFFERAUTO, STATUSAUTO FROM OFFERSTATUSTRACK
INNER JOIN (SELECT MAX(TRACKAUTO) TRACKAUTO FROM OFFERSTATUSTRACK WHERE OFFERAUTO IN (SELECT OFFERAUTO FROM OFFER WHERE SELLERAUTO=@sellerAuto) GROUP BY OFFERAUTO) A
ON A.TRACKAUTO = OFFERSTATUSTRACK.TRACKAUTO
END
END

Comments

Popular posts from this blog

SPFx: Develop using SharePoint Framework without Installing all the dependecies.

SharePoint Online: Elevated Permissions....with love

Powershell: Filling up an Existing Excel Sheet with data from SQL Server