Solved

Build a SQL Select statement

Posted on 2009-04-15
5
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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