Solved

MS T-SQL Select SubQuery 101

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now