Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS T-SQL Select SubQuery 101

Posted on 2010-08-26
5
Medium Priority
?
400 Views
Last Modified: 2012-05-10
I seem to be having a SQL brain meltdown this morning with my parameters - hoping someone can point out what I'm doing wrong here?  It may very well just be I need more coffee this morning.

I have two tables, and I'm trying to build a list of entries based on values contained mainly in table1 and a couple values from table2.

Table1 Data
Project            Task            Workorder      Description            
PD123                                                Overhead
PD123            001                                    
PD123            001                  AE                  
PD199                                                Overhead
PD199            001                                    
PD199            001                  EE                  Overhead

Table2 Data
Project            Task            Workorder      ClientNumber            Status                  ChargeType
PD123                                                ClientA                        A                        B
PD123            001                                    ClientA                        A                        B
PD123            001                  AE                  ClientA                        A                        B
PD199                                                ClientB                        A                        B
PD199            001                                    ClientB                        A                        B
PD199            001                  EE                  ClientB                        A                        B

So I construct this query using a variable that I eventually want to be able to pass through, but for now I'm setting manually to test:

	Declare @ClientNumber varchar(30), @ProvOvhd decimal(18,4)
	Set @ClientNumber = 'ClientA'
	Set @ProvOvhd = 1.1234

select Table1.Project, Table1.Task, Table1.Workorder, Table2.Description, Table2.ProvOvhd  
	from Table1, Table2
Where Table1.Project = Table2.Project
AND Table1.Task = ''
AND Table1.Workorder = ''
AND 
Table2.ClientNumber = @ClientNumber
AND Table2.Status = 'A' AND Table2.ChargeType = 'B'
	AND Table1.Description = 'Overhead'

Open in new window



But I return nothing.

Now what throws me is I must be missing something with how to handle variables in the subquery, because when I do this:

select Table1.Project, Table1.Task, Table1.Workorder, Table2.Description, Table2.ProvOvhd  
	from Table1, Table2
Where Table1.Project = Table2.Project
AND Table1.Task = ''
AND Table1.Workorder = ''
AND 
Table2.ClientNumber = 'ClientA'
AND Table2.Status = 'A' AND Table2.ChargeType = 'B'
	AND Table1.Description = 'Overhead'

Open in new window



It works just fine.


0
Comment
Question by:Scudboy
[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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33532718
>ClientNumber <
is the data type of the variable or column CHAR instead of VARCHAR ?
please check that ...
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33532746
what is the data type of the Column - Table2.ClientNumber..Is it a Char? check and see if you have same datatypes
0
 

Author Closing Comment

by:Scudboy
ID: 33533217
Nailed it angelllll -
It was a varchar column, but varchar(32) not varcahr(30)

So much thanks to you, and more coffee for me.

Thank you again!

To summarize - your variables really need to be EXACTLY the column type you plan on using as a parameter.
And I need to drink more coffee in the morning.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33533292
for CHAR, it must indeed be exactly the size
but for VARCHAR, you variable needs to be at least as big as the data in it.

so, for a VARCHAR(50) column, with though the max(len(yourcolumn)) having 30, your variable being varchar(30) would work, as well as a variable of type VARCHAR(100) would work
0
 

Author Comment

by:Scudboy
ID: 33535701
Awesome.  Thank you for the superb follow up as well!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

618 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