Solved

Variable in SQL Select Statement

Posted on 2014-10-13
6
113 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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…

749 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