Stored Procedure Local Variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enreil
    New Member
    • Jan 2007
    • 86

    Stored Procedure Local Variables

    Hello,

    I'm relatively new to the world of Stored Procedures in SQL Server. This may be a silly question, but I've done some searching and haven't come up with any solid answers.

    Is is possible to assign the result of a SELECT statement e.g.
    Code:
     SELECT TOP 1 Cust_Name FROM Customers
    to a local variable within my stored procedure? If so, is this a good practice, or is there a better approach?

    Thank you.
  • BHARATH RS
    New Member
    • May 2007
    • 6

    #2
    You can use them in ur stored procedures. It mainly depends on the requirements.

    here is a sample procedure for ur question

    CREATE PROCEDURE Top_Customer AS
    begin

    Declare

    @l_top_customer varchar(100)


    set @l_top_customer = (select top 1cust_name from Bharath_Practis e.dbo.Cust_Reco rd)

    print @l_top_customer

    END
    GO

    Comment

    • enreil
      New Member
      • Jan 2007
      • 86

      #3
      Thanks! This works nicely.

      Originally posted by BHARATH RS
      You can use them in ur stored procedures. It mainly depends on the requirements.

      here is a sample procedure for ur question

      CREATE PROCEDURE Top_Customer AS
      begin

      Declare

      @l_top_customer varchar(100)


      set @l_top_customer = (select top 1cust_name from Bharath_Practis e.dbo.Cust_Reco rd)

      print @l_top_customer

      END
      GO

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Thanks for the great response BHARATH RS. Glad to have you around.

        Comment

        Working...