Solved

MS T-SQL Select SubQuery 101

Posted on 2010-08-26
5
397 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 125 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

730 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