Results 1 to 4 of 4

Thread: Table name as stored proc variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    5
    I'm trying to write the following stored procedure where the @strStock variable represents a table in the SQL DB:

    CREATE PROCEDURE sp_KairosQueryMINMAXstock
    @strStock char(4),
    @strDate char(10),
    @strStartTime char(8),
    @strEndTime char(8)
    AS
    Select MIN(trade), MAX(trade), Count(*) from strStock
    where Date = @strDate
    and Time > @strStartTime
    and Time < @strEndTime

    If I run it as a straight query from VB, I have no trouble. But how do I write the query so that the table name variable @strStock is properly inserted into the stored proc.
    Thanks


  2. #2
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    if i remember correctly all you need to do is

    CREATE PROCEDURE sp_KairosQueryMINMAXstock
    @strStock char(4),
    @strDate char(10),
    @strStartTime char(8),
    @strEndTime char(8)
    AS
    Select MIN(trade), MAX(trade), Count(*) from @strStock
    where Date = @strDate
    and Time > @strStartTime
    and Time < @strEndTime

    could be wrong though
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  3. #3
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    ...

    build your sql string inside the stored proc and
    exec it...

    so
    CREATE PROCEDURE sp_KairosQueryMINMAXstock
    @strStock char(4),
    @strDate char(10),
    @strStartTime char(8),
    @strEndTime char(8)
    as
    begin
    declare @strline varchar(255)
    declare @strline1 varchar(255)
    select @strline = "select min(....... from " + @strStock
    select @strline = " where ....."

    -- then here

    exec (strline + strline2)

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    5
    Thanks for your help, expecially the tip about concatenating everything in the EXEC statement. After several hours running the sp through T-SQL Debugger (a pain to install, but worth it), I could see that any @Str with the @strStock in it failed to work. So I figured I had to concatenate it somehow. Then I checked back here and saw your reply. I've simplified everything, but this is finally what works. Don't forget to put double-quotes around the single quote.

    CREATE PROCEDURE sp_Kairos6
    @strStock char(4),
    @strDate char(10)
    AS
    begin
    declare @Str1 varchar(255)
    declare @Str2 varchar(255)
    select @Str1 = "select Count(*) from "
    select @Str2 = " where Date = " + "'" + @strDate +"'"
    exec (@Str1 + @strStock + @Str2)

    Thanks again
    end

Posting Permissions

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



Click Here to Expand Forum to Full Width