Hi,
I want to buld te where clause of a select statement dynamically. But SQL2005 won't let me do this ("An object or column name is missing or empty"etc). What am I doing wrong here?
Thanks! - SELECT
-
d.naam,
-
e.naam,
-
h.ts_week,
-
h.ts_jaar,
-
l.nummer
-
FROM
-
departments d
-
JOIN emp_dep ON d.id=emp_dep.dep_id
-
JOIN employees e ON emp_dep.emp_id=e.id
-
JOIN hours h ON e.id=h.employee_id
-
JOIN locations l ON h.locatie_id=l.id
-
WHERE
-
1=1
-
IF @employee_name !=""
-
AND e.naam=@employee_name
-
IF @department_name!=""
-
AND d.naam=@department_name
-
IF @timesheet_week !=""
-
AND h.ts_week=@timesheet_week
-
IF @timesheet_year !=""
-
AND h.ts_jaar=@timesheet_year
-
IF @location_nummer !=""
-
AND l.nummer=@location_nummer
5 33641
Use the Case statement in the SELECT clause that's how it's done.
for example:
select case @Employee when 'SAM' THEN 1
when 'SUE' THEN 2
else '' end as EmployeeName, date, class
from mytable
Thanks! I found out how to.
Once you post a problem on a tech forum, it's very valuable to other people if you take the time to post the solution, even if you found it yourself. I see so many threads like this that end with "Never mind, I figured it out." This just creates blind alleys for people who are having the same problem.
Try this: - SELECT
-
d.naam,
-
e.naam,
-
h.ts_week,
-
h.ts_jaar,
-
l.nummer
-
FROM
-
departments d
-
JOIN emp_dep ON d.id=emp_dep.dep_id
-
JOIN employees e ON emp_dep.emp_id=e.id
-
JOIN hours h ON e.id=h.employee_id
-
JOIN locations l ON h.locatie_id=l.id
-
WHERE
-
e.naam like '%' + @employee_name
-
AND d.naam like % + @department_name
-
AND h.ts_week like % + @timesheet_week
-
AND h.ts_jaar like '%' + @timesheet_year
-
AND l.nummer like '%' + @location_nummer
Let's examine this: e.naam like '%' + @employee_name
In this case if @employee_name = "" e.naam like '%' + ""
the same as e.naam like '%' - means everything
If @employee_name = "poolman" e.naam like '%' + "poolman"
the same as e.naam like '%poolname"
So if name is passed it will be found and if nothig passed all namew will be returned.
Good Luck.
Just want to say Serutan, I agree .. I stumbled across this thread looking for answers. And like so many other threads that just fizzle out with "S'ok, I figured it out"... this one left me frustrated. I had a similar problem and for others with the same problem as this.. the solution is to use single quotes - not double quotes. In the xample above..
IF @employee_name !="" ... should be ... IF @employee_name !=''
In my case it was declaring tsql variables .. like
@Postcode varchar(10) = "" ... when it should be ... @Postcode varchar(10) = '',
Hope this helps other people as its not an obvious one to spot (I wasn't even writing an insert query so SQL error message really didn't help me!)
Cheers
Sign in to post your reply or Sign up for a free account.
Similar topics
by: malcolm |
last post by:
Example, suppose you have these 2 tables
(NOTE: My example is totally different, but I'm simply trying to setup
the a simpler version, so excuse the bad design; not the point here)
CarsSold {...
|
by: aj70000 |
last post by:
This is my query
select ano,max(date),a_subject from MY_TAB where table_name='xyz' and
ano=877
group by a_subject,ano order by a_subject
ANO max(Date) A_Subject
877 2005-01-20...
|
by: JJ_377 |
last post by:
Can someone tell me why SQL seems to ignore my order by clause?
I tried to run through the debugger, but the debugger stops at the
select statement line and then returns the result set; so, I have...
|
by: Chris, Master of All Things Insignificant |
last post by:
I have come to greatly respect both Herfried & Cor's reponses and since the
two conflicted, I wanted to get some clarification.
My orginal post:
Herfried, maybe your example here can get you to...
|
by: Sean Shanny |
last post by:
To all,
We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3
server, 8GB mem, attached to a fully configured 3.5TB XRaid box via
fibre channel.
I think we have run into this...
|
by: GreatAlterEgo |
last post by:
Hi,
This is my query which is embedded in a COBOL program.
EXEC SQL
SELECT DATE, AGE, DURATION, AMT
INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT
FROM TAB1
WHERE CODE = :KEY.CODE
AND...
|
by: metaperl.etc |
last post by:
A very old thread:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e
discusses the optional "else:"...
|
by: Jim.Mueksch |
last post by:
I am having a problem with using calculated values in a WHERE clause.
My query is below. DB2 gives me this error message: Error: SQL0206N
"APPRAISAL_LESS_PRICE" is not valid in the context where...
|
by: pwiegers |
last post by:
Hi,
I'm trying to use the result of a conditional statement in a where
clause, but i'm getting 1)nowhere 2) desperate :-)
The query is simple:
--------
SELECT
idUser,...
|
by: jackal_on_work |
last post by:
Hi Faculties,
I have two queries which give me the same output.
-- Query 1
SELECT prod.name, cat.name
FROM products prod INNER JOIN categories cat
ON prod.category_id = cat.id
WHERE cat.id...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |