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
Solved

Build a SQL Select statement

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Make Temp Table Query Faster 5 50
SQL group by query to return records with highest value 6 25
Trouble with <> 2 21
Help with Oracle IF statment 5 26
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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