Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Posts
    29

    Unanswered: Selected a field that is variable

    How can this be done:

    I want to call a procedure with one of the parameters being a column:

    exec sp_statistics home_runs

    sp_statistics(@stat_field CHAR(10))

    select @stat_field
    from batting_table


    In this example, @stat_field would be home_runs, a field in the batting_table

  2. #2
    Join Date
    Jun 2004
    Location
    Paris, France
    Posts
    43
    you can use an "execute immediate" command :

    create procedure calcAvg
    @yourField varchar(30),
    @yourTable varchar(30) as
    begin
    declare @instruction varchar(255)

    -- We suppose that @yourField is a number field
    select @instruction = "select avg(" + @yourField + ")
    from " + @yourTable

    execute (@instruction)
    end
    go

    exec calcAvg "aField", "ofThisTable"
    go

    that should work (with ASE 12.5.03 it does)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •