+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Member
    Join Date
    Aug 2008
    Posts
    54

    Question stored procedure helpppppp

     
    how can i write a stored procedure in which the table can be a variable?
    (i mean something like "FROM @table", but this doesn't work).

    help will be greatly appreciated.
    Last edited by Mostafa Nabil; February 16th, 2009 at 02:24 PM. Reason: wrong spelling

  2. #2
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,879
    you are right, you can not write the table name as a variable

    SO one option is to build a string and exec the string

    DECLARE @blah VARCHAR(255)
    DECLARE @execstring VARCHAR(255)

    SET @execstring = 'SELECT * FROM ' + @tablename + ' WHERE column = 4'

    exec @execstring

    if its a lengthy SP it can be very ugly and complicated, but functional

    been there done that, it was very messy

  3. #3
    Member
    Join Date
    Aug 2008
    Posts
    54
    i did it but i have a problem
    when i execute it , this exception appears
    (Could not find stored procedure 'SELECT Compound
    FROM H1 WHERE reactant =na'.)

    this is my stored procedure

    ALTER PROCEDURE dbo.StoredProcedure2

    (
    @reactant varchar(5),
    @tbl varchar(10)


    )

    AS
    DECLARE @blah VARCHAR(255)
    DECLARE @execstring VARCHAR(255)

    SET @execstring = 'SELECT Compound FROM ' + @tbl + ' WHERE reactant =' + @reactant

    exec @execstring
    RETURN


    i have a table called H1 and there is an element in reactants row called na

    what's wrong?

  4. #4
    Member
    Join Date
    Oct 2003
    Posts
    267
    try putting it in brackets

    EXEC (@execstring)

  5. #5
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,879
    the problem will be your single quotes around what I'm assuming is a string

    SO what you're doing right now is something like

    select compound from tablename where reactant = somereactant

    which doesn't bode well for sql

    So how do you do that? well with single quotes of course!
    select compound from tablename where reactant = 'somereactant'
    AH but you're embedding single quotes within single quotes which breaks the string etc etc

    SO the fun begins!
    You want to escape all of your single quotes for the embedded string (NOT the string you're building to execute

    select compound from tablename where reactant = \'somereactant\'

    which should bring you some fugly looking string like

    SET @execstring = 'SELECT Compound FROM ' + @tbl + ' WHERE reactant = \' ' + @reactant + '\''

    simple
    clear as mud?

    Because these are so horrible to debug (they're worse than triggers to debug I swear... ) I'd suggest putting in some debug code

    SET @execstring = 'SELECT Compound FROM ' + @tbl + ' WHERE reactant = \' ' + @reactant + '\''

    PRINT @execstring
    exec @execstring

    so in the query analyzer you can always see exactly what you're attempting to execute.. which may not look so good until you clean it up.
    Last edited by vass0922; February 18th, 2009 at 12:29 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Everything is being stored on D Drive
    By myoung6264 in forum Technical Support
    Replies: 10
    Last Post: January 5th, 2009, 01:28 PM
  2. Stored Procedure
    By Jigyasa in forum Webmastering and Programming
    Replies: 3
    Last Post: April 12th, 2006, 07:58 PM
  3. templates stored
    By VERT in forum Applications and Operating Systems
    Replies: 2
    Last Post: June 2nd, 2003, 07:50 PM
  4. install procedure
    By Sanman in forum Applications and Operating Systems
    Replies: 5
    Last Post: December 14th, 2002, 09:23 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews