Results 1 to 2 of 2

Thread: Pass field names to stored procedure

  1. #1
    Lively Member
    Join Date
    Nov 00
    Posts
    82

    Pass field names to stored procedure

    I'm trying to pass the name of a field to an SP. The fieldname will change from time to time. The resulting SQL statement (if done in VB) would look like
    Code:
    "SELECT * FROM tblMytbl WHERE " & myField & "=" & myVar
    Here's what I've got, but needless to say it will not work.
    Code:
    CREATE PROCEDURE myProc
    
    @Parm1 varchar(20)     -- The field
    @Parm2 varchar(20)     -- The variable
    
    AS
    SELECT * FROM tblMyTbl WHERE @Parm1 = @Parm2
    Can anyone help? Thanks.

  2. #2
    Frenzied Member
    Join Date
    Aug 99
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Try this:

    CREATE PROCEDURE myProc

    @Parm1 varchar(20) -- The field
    @Parm2 varchar(20) -- The variable

    AS
    Declare @Sql_Statement varchar(255)

    SELECT @Sql_Statement = 'SELECT * FROM tblMyTbl WHERE ' + @Parm1 + '=' + @Parm2

    EXEC(@Sql_Statement)
    Go


    I'll leave it to you to figure out how to make this work if @parm2 is a string...
    Last edited by JHausmann; Jun 28th, 2002 at 02:07 AM.

Posting Permissions

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