Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    52

    Unanswered: how to use @tablename

    i need to develop a stored procedue, in ehich i have to use variable table name.. as
    Code:
    Select * from @tableName
    but i m unable to do so..
    it says u need to define @tablename

    heres da code
    Code:
    CREATE PROCEDURE validateChildId
    (
    @childId int,
    @tableName varchar(50),
    @fid int output
    )
     AS
    (
    SELECT @fid=fid FROM @tableName
     where @childId= childId
    )
    
    if @@rowcount<1
    SELECT 
        @fid = 0
    GO

  2. #2
    Join Date
    Jan 2004
    Posts
    67

    write Dynamic SQL

    you need to write a Dynamic SQL Statement for this.

    Declare @SQL VarChar(1000)

    SELECT @SQL = 'SELECT * FROM '
    SELECT @SQL = @SQL + @TableName

    Exec ( @SQL).

    Something like this.

    Do let me know if you want the code for your scenario.

    Thanks
    Shankar

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    exec ('Select * from ' + @TableName)

    ...is, for its brevity and wanton violation of sound development principles, arguably the most concentrated example of bad code one could write.

    I would no more encourage you to do this than I would recommend one brand of cigarettes over another.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    Jan 2004
    Posts
    67
    i understand its the crudest way of writing code, if there's any other method which can solve this problem, please do let me know. I can Improve my knowledge too. :-)

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    I wasn't disparaging your code, which effectively solves the problem that was presented.

    The solution isn't the problem. The problem is the problem.

    It just isn't a good idea to develop applications with this type of functionality. It opens a gaping hole into the database that user's are free to jam anything into, intentionally or inadvertently.

    Waqas needs to reconsider the design of his application.

    I apologize if I inadvertently offended you.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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