?
Solved

Build a SQL Select statement

Posted on 2009-04-15
5
Medium Priority
?
249 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 375 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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