4 Comments

Well for ages, I created mine stored procedures like:

if exists (select 1 from sys.objects where name = 'GetSales' and type = 'p')
begin
    drop procedure [Reporting].[GetSales]
end
go

create procedure [Reporting].[GetSales]
    @parameter1 int,
    @parameter2 int
as
begin
    select @parameter1, @parameter2    
end
go

, but there is a slightly shorter notation:

if object_id('[Reporting].[GetSales]') is not null
begin
    drop procedure [Reporting].[GetSales]
end
go

create procedure [Reporting].[GetSales]
    @parameter1 int,
    @parameter2 int
as
begin
    select @parameter1, @parameter2    
end
go

 

 

You’re never to old to learn Winking smile.

4 Replies to “How to create a stored procedure with T-SQL”

  1. hahaahahahahahahahahahahaahahaha
    hahaahahahahahahahahahahaahahaha
    hahaahahahahahahahahahahaahahaha
    hahaahahahahahahahahahahaahahaha

    Nope, but some people believe the internet starts and ends at the knowledgebase πŸ™‚

  2. But now you’re deleting the GetSales Table !! πŸ™‚
    And then the create procedure fails.

  3. Nope, a stored procedure and a table can’t have the same name in the same schema, so you can’t have a table [Rapportage].[GetSales] and a table [Rapportage].[GetSales].
    So the code above will always drop the stored procedure [Rapportage].[GetSales] en then create it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts