Solved

Build a SQL Select statement

Posted on 2009-04-15
5
213 Views
Last Modified: 2012-05-06
Hi Guys

I am trying to build a simple Select statement.

DECLARE @BasicField AS VARCHAR(50)
set @BasicField = 'City'
Select * from dbo.AMGR_Client_Tbl
where  @BasicField = 'Slough'

This does not bring any data back.

i thought the select statement would be:
Select * from dbo.AMGR_Client_Tbl
where City = 'Slough'
This brings a number of rows back.

Any help please!!
0
Comment
Question by:Shezad Ahmed
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24147741
DECLARE @BasicField AS VARCHAR(50)
set @BasicField = 'City'
Select * from dbo.AMGR_Client_Tbl
where  @BasicField = 'Slough'
This query would be trying to find rows where 'City'='Slough' which will never return any rows.


0
 
LVL 60

Accepted Solution

by:
chapmandew earned 125 total points
ID: 24147746
DECLARE @BasicField AS VARCHAR(50), @sql nvarchar(2000)
set @BasicField = 'City'
set @sql = 'Select * from dbo.AMGR_Client_Tbl '
set @sql = @sql + ' where ' + @basicfield + ' = ''''slough'''

exec sp_executesql @sql
0
 

Author Closing Comment

by:Shezad Ahmed
ID: 31570430
Thanks.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24148609
Since this answered your question, by the rules on EE, it cannot be a B answer. Please change it to an A.
0
 

Author Comment

by:Shezad Ahmed
ID: 24155472
How do I change it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now