Results 1 to 5 of 5

Thread: Get Row of Column By Value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Get Row of Column By Value

    Using the TSQL code doesn't work when I want to search a column with a value..

    Code:
    CREATE PROCEDURE [dbo].[SearchPeople]
    	@FieldName	NVARCHAR(50), --Column Name
    	@FieldValue	NVARCHAR(50)  --Value To Search
    AS
    	SELECT *							  	 
    	FROM [dbo].[People]
    	WHERE @FieldName = @FieldValue 
    GO

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Get Row of Column By Value

    Right... because @FieldName is just a value... How's SQL supposed to know that you meant for it to be the field name versus a value... I mean, take even your own query for example:
    Code:
    	SELECT *							  	 
    	FROM [dbo].[People]
    	WHERE @FieldName = @FieldValue
    LEt's say @FieldName = 'Field1' and @FieldValue = 'Field1' ... is it supposed to search Field1 for "Field1" or is it supposed to look for where Field1 = Field1 ... it's ambiguous... so parameters are always treated as values, not objects.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: Get Row of Column By Value

    thanks, so what should I do to correct this code?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Get Row of Column By Value

    well... that depends... I mean the way to fix it is to specify the field name in the where...
    Code:
    	SELECT *							  	 
    	FROM [dbo].[People]
    	WHERE MyField = @FieldValue
    That's just how it is.

    There is a way to do it dynamically... but it takes some setup, and is probably more trouble than it's worth in this case.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Get Row of Column By Value

    Sorry but i couldn't try it but it should be working, including wildcards

    Code:
    CREATE PROCEDURE [dbo].[SearchPeople]
    	@FieldName	NVARCHAR(50), --Column Name
    	@FieldValue	NVARCHAR(50)  --Value To Search
    AS
    declare @FULLQUERY AS NVARCHAR(max)
    set @FULLQUERY='SELECT * FROM [dbo].[People] WHERE ['+@FieldName+'] LIKE '''+@FieldValue+''''
    EXEC (@FULLQUERY)
    GO


    And by the way, what's this doing in Mobile development?
    Last edited by TDQWERTY; Jan 14th, 2014 at 05:13 AM. Reason: Typo
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

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