Solved

Variable in SQL Select Statement

Posted on 2014-10-13
6
114 Views
Last Modified: 2014-10-15
I am trying to show a list of teams and individuals. What I want to do is show a Person's Name that has been entered in a textbox when the Person's Name is null in the database. I do not want to use the Update Statement because it will only be temporary.  
string varPerson = Request.Form["PersonName"];
SELECT NumGames, PersonTeam, CASE WHEN PersonName IS NULL THEN varPerson END AS PersonName
FROM
(
Select NumGames, PersonTeam, PersonName FROM TeamData
)
TABLE1
Order by PersonTeam

Open in new window

0
Comment
Question by:RecipeDan
[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
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40377546
Try using the ISNULL function:
ISNULL(PersonName, varPerson)
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40377634
I tried this and nothing happens:
SELECT NumGames, PersonTeam, ISNULL(PersonName, varPerson) as PersonName
FROM
(
Select NumGames, PersonTeam, PersonName FROM TeamData
)
TABLE1
Order by PersonTeam

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40377639
I get an invalid column name varPerson
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40377663
Are you using a stored procedure for you SQL, or are you building your query in your .NET code?
If it is a stored procedure, you need to pass your .NET variable into it. If you are building the query in .NET, you need to "add" it to your query.

...PersonTeam, ISNULL(PersonName, '" + varPerson + "') as PersonName...

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40378417
Thank you that works. Will Not Null be the same way?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40381957
When you use NOT NULL in a query, it is used for comparison. (Does this field have a value?) The ISNULL function uses the first parameter in the list that is not null. The CASE statement in your original query would have worked if you had included an ELSE condition. The ISNULL function is a little cleaner to use.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

717 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