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

MS T-SQL Select SubQuery 101

Posted on 2010-08-26
5
395 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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 the fundamental information of how to create a table.

790 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