Solved

Build a SQL Select statement

Posted on 2009-04-15
5
201 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

11 Experts available now in Live!

Get 1:1 Help Now