CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 1999
    Posts
    91

    HowTo: Pass a columnname as parametername in stored procedure

    Normally you pass values as parameters.
    But how can I pass a columnname as parameter in a stored procedure.

    The procedure should look like somehow.
    select * from A where @columname = 'Test'

    Regards,

    Maurice

  2. #2
    Join Date
    Apr 2002
    Location
    Haryana, India
    Posts
    198

    Re: HowTo: Pass a columnname as parametername in stored procedure

    Dear Maurice,

    You can pass column name as value of Parameter in Stored Procedure but the functionality you want to implement is not possible. You can achieve the functionality by changing your following line with my code:

    Your Line of Code
    select * from A where @columname = 'Test'

    My Line of Code
    DECLARE @sqlString VARCHAR(8000)

    SET @sqlString = 'select * from A where ' + @columname + ' = ''test'''

    execute(@sqlString)

    Hope this will solve u r problem.
    Enjoy,

    Gurdarshan Singh
    L.S.E. (Project Lead)
    InterGlobe Technologies Pvt. Ltd.
    Mobile #: 9891397798 (India)
    gurdarhan.singh@interglobetechnologies.com
    gurdarshan70@hotmail.com

    Always Think Positive whatever may be the Situation.

    Please rate my suggestion/response if you find it suitable or fulfill your requirement.

  3. #3
    Join Date
    Aug 1999
    Posts
    91

    Post Re: HowTo: Pass a columnname as parametername in stored procedure

    How can the parameter @columnname be descibed in the stored procedure?
    Maybe a small example would help.

    Is it like??
    CREATE PROCEDURE Test
    (
    DECLARE @sqlString VARCHAR(8000)
    @COLUMNNAME varchar(30)
    )
    AS
    SET @sqlString = 'select * from A where ' + @columname + ' = ''test'''

  4. #4
    Join Date
    Apr 2002
    Location
    Haryana, India
    Posts
    198

    Re: HowTo: Pass a columnname as parametername in stored procedure

    Dear Maurice,

    No, example like below:

    CREATE PROCEDURE Test
    (
    @COLUMNNAME varchar(30)
    )
    AS
    BEGIN

    DECLARE @sqlString VARCHAR(8000)

    SET @sqlString = 'select * from A where ' + @columname + ' = ''test'''
    execute(@sqlString)

    END
    Enjoy,

    Gurdarshan Singh
    L.S.E. (Project Lead)
    InterGlobe Technologies Pvt. Ltd.
    Mobile #: 9891397798 (India)
    gurdarhan.singh@interglobetechnologies.com
    gurdarshan70@hotmail.com

    Always Think Positive whatever may be the Situation.

    Please rate my suggestion/response if you find it suitable or fulfill your requirement.

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

Featured