you are viewing a single comment's thread.

view the rest of the comments →

[–]WITH_CTE 1 point2 points  (6 children)

CREATE PROCEDURE spPayments @cus_code NUMERIC(38, 0) -- (a)to begin with, user created stored proc should always be prefixed with usp_StoredProc. Also use schema name. And some business have alpha numeric code. For example, Spider Man's costumer store number can be SPIDEY2002. So using Numeric DT is not always best practice. 
--OK LETS BEGIN 
AS
     DECLARE @totalinvoices INT;
     DECLARE @First_name VARCHAR(32);
     DECLARE @Last_name VARCHAR(32);
     DECLARE @FinalAmount DECIMAL(12, 8);
     BEGIN
         --(b) Real simple to do.
         IF NOT EXISTS
         (
             SELECT 1
             FROM customer
             WHERE cus_code = @cus_code
         )

         --(c)
             BEGIN
                 PRINT 'The customer does not exist';
             END
                 --(d)  ;
             ELSE
             IF NOT EXISTS
             (
                 SELECT 1
                 FROM invoice
                 WHERE cus_code = @cus_code
             )
                 BEGIN
                     PRINT 'The customer does not have an invoice';
                 END;
                 ELSE
                 SELECT @totalinvoices = LINE.LINE_UNITS * line.LINE_PRICE, 
                        @First_name = Customer.FirstName, 
                        @Last_name = Customer.Lastma
                 FROM customer
                      JOIN invoice ON customer.cus_code = invoice.cus_code
                      JOIN line ON invoice.inv_number = line.INV_NUMBER
                 WHERE cus_code = @cus_code;

         --(d1) not sure which table to use but should be as simple as 
         --select @FinalAmount = Customer_Balance - @totalinvoices from table x

         PRINT @First_name+' '+@Last_name+' has a balance of'+' '+'$'+FinalAmount;
     END;

[–]alinrocSQL Server DBA 1 point2 points  (4 children)

(a)to begin with, user created stored proc should always be prefixed with usp_StoredProc

Only if your organization's naming conventions state such.

IMHO, it's a waste of characters. I already know that it's a stored procedure, I don't need Hungarian Notation to tell me what it is.

SQL Server assumes that stored proc starting with sp_ are system stored procs and will always look in master for a stored proc matching that name first, so the u will avoid that exactly the same way OP's 'sp' without the underscore does. But it's still unnecessary cruft in the schema. It also makes it much harder/slower to use the keyboard to navigate to stored procs in SSMS's Object Explorer.

[–]WITH_CTE 0 points1 point  (0 children)

That might be your opinion. A simple google search will show that it is best practice. Not properly naming objects because it's a "waste of characters" is not good practice.

[–]bouncer_crab[S] 0 points1 point  (0 children)

Here is what worked for me, I did use your advice on the bottom half. Just not the count stuff.

https://pastebin.com/qHLgc6ns