Solved

MS T-SQL Select SubQuery 101

Posted on 2010-08-26
5
394 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 142

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 142

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

776 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