SQL, Store Procedure

Hi Experts, can some one tell me that the purpose of type='p' and why "select 1 from "

if exists (select 1 from sys.objects where name = 'sp_rept_EmployeePerformance' and type = 'P')
	drop procedure sp_rept_TitleLoan_EmployeePerformance

Open in new window


Thank you,
tanj1035Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
'P' means Stored Procedure. 'F' is function, v is view, etc.  It's used in the above T-SQL just in case somebody created a non-stored procedure object named sp_rept_EmployeePerformance
Brian CroweDatabase AdministratorCommented:
type "p" is a procedure.  The objects tables also contains, tables, views, etc. and this is just a safeguard to make sure you are getting the precise object you want.
Brian CroweDatabase AdministratorCommented:
BTW you should never prepend your procedures with "sp_".  It will work but you are adding unnecessary overhead as the compiler assumes that it is a system procedure and will look there first.

https://www.mssqltips.com/sqlservertutorial/169/naming-conventions-for-sql-server-stored-procedures/
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

tanj1035Author Commented:
Thank Jim and Brian's explanations.
Why there is a "select 1 from" and what does 1 mean ?
Jim HornMicrosoft SQL Server Data DudeCommented:
Select the first column.  In the context of IF EXISTS() all that really matters is if a row is returned or not.  It could have easily said select name, name, type, or *.

The DROP PROC will execute if the proc exists, and will no execute if it doesn't.
Brian CroweDatabase AdministratorCommented:
Another way to do the same thing if you aren't worried about another object having the same name is:

IF OBJECT_ID('sp_rept_EmployeePerformance') IS NOT NULL
   DROP PROCEDURE sp_rept_EmployeePerformance;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the split, good luck with your code.  -Jim
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.