-
Jan 10th, 2014, 09:35 PM
#1
Thread Starter
Fanatic Member
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
-
Jan 10th, 2014, 09:54 PM
#2
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
-
Jan 10th, 2014, 10:08 PM
#3
Thread Starter
Fanatic Member
Re: Get Row of Column By Value
thanks, so what should I do to correct this code?
-
Jan 10th, 2014, 10:32 PM
#4
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
-
Jan 14th, 2014, 05:03 AM
#5
Fanatic Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|