473,511 Members | 15,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested select SQL within an Update query rotated across multiple columns

11 New Member
G'day all, thanks in advance for reading.

I've got two tables, one that's full of data regularly externally updated called "tblEmployeeLicences":
Expand|Select|Wrap|Line Numbers
  1. EmployeeID,Name,Licences
  2. 1001,Bill,Drivers
  3. 1001,Bill,Forklift
  4. 1002,Ted,Drivers
  5. 1002,Ted,Forklift
  6. 1002,Ted,Crane
The other table is one that is depended upon by an ID-card printing program, and as such only allows a single row per employee, called "IDProjectData":
EmployeeID,Name,Licence01,Licence02,Licence03...16

I've developed a nested updated/select query to update only the first Licence for each person (it's trivial to get all of them with slight variations on it):
Expand|Select|Wrap|Line Numbers
  1. UPDATE IDProjectData 
  2. RIGHT JOIN [SELECT TOP 1 Licences 
  3. FROM tblEmployeeLicences 
  4. WHERE EmployeeID = [IDProjectData].[EmployeeID] ]. AS Results 
  5. ON IDProjectData.EmployeeID=Results.EmployeeID 
  6. SET IDProjectData.IDWLicence01 = Results.Licences;
The above successfully updates the first licence into the IDProjectData table, but the query prompts the user to enter in the CandidateNo for BOTH tables upon running it.

How do I restructure the query so that it just rolls over them all, updating the 'Licence01' column for each employee?
Or should I be going about this a different way?
Jan 19 '09 #1
8 10218
modernshoggoth
11 New Member
Update - the query is now as follows
Expand|Select|Wrap|Line Numbers
  1. UPDATE IDProjectData LEFT JOIN [SELECT TOP 1 Licences
  2. FROM tblEmployeeLicences
  3. WHERE EmployeeID = [IDProjectData].[EmployeeID] ]. AS Results ON IDProjectData.EmployeeID =Results.EmployeeID SET IDProjectData.IDWLicence01 = Results.Licences;
It still prompts for both EmployeeID's but now it updates ALL employees with that particular Employee's first Licence - how do I get it to look at each employees and update their own first Licence?
Jan 19 '09 #2
FishVal
2,653 Recognized Expert Specialist
Hello.

It looks like you have two copies of the same data which generally is not a good idea.
Since you update [IDProjectData] with values from [tblEmployeeLicences] I guess data primary source is the latter table.
So, why don't you want to use crosstab query to get dynamically pivotted data from [tblEmployeeLicences]?
Jan 19 '09 #3
modernshoggoth
11 New Member
I'm aware that two copies of the same data isn't the best way to do things, but the ID-card software this is designed for uses the [IDProjectData] table, and is very strict about how data is accessed.

I haven't used a crosstab query because I've primarily dealt with SQL up until now - crosstabs and pivots are mostly new to me, and I can't figure out a way to get it to update pivot'd data.
Jan 19 '09 #4
FishVal
2,653 Recognized Expert Specialist
@modernshoggoth
What does it mean ?
Jan 19 '09 #5
modernshoggoth
11 New Member
The software that accesses the database can only use THAT particular table, and can only use certain cells in certain ways.
What I'm trying to to develop is a way to replicate data from other tables into that table. The way the data arrives into these other tables is also out of my control - therefore I must make a way to put data in this other table via some form of the above update query, or some sort of updating Crosstab or pivot query - and I've no idea where to get started on CT's or Pivots when it comes to updating data rather than simply selecting it.
Jan 19 '09 #6
FishVal
2,653 Recognized Expert Specialist
  • First of all it is very probable that the software connecting to [IDProjectData] table could connect to crosstab query with the same name. Check it please.
  • Crosstab query being not updateable will give a unupdateable join with [IDProjectData]. So, I suspect it is not possible to update [IDProjectData] this way.
  • [IDProjectData] could be a temporary table created from crosstab query via SELECT INTO.
  • [IDProjectData] could be updated via recordset.
Jan 20 '09 #7
modernshoggoth
11 New Member
Unfortunately it doesn't like working from a selected crosstab query - I've tried that. The program refuses to open, giving nothing in the way of error messages =(

I haven't tried the other possible solutions - I've just solved it now. I've used VBA to parse and execute SQL to be fed back into the main update query. It's slow, but it doesn't need to be fast.

Thanks, guys!
Jan 20 '09 #8
NeoPa
32,557 Recognized Expert Moderator MVP
That may be your only solution :(

I'm pretty sure that subqueries in SQL stop it being updatable too.

It's sometimes possible to find sneaky ways around these restrictions, but if you have it processing in code and speed is not a big issue, then that seems like a suitable solution.
Jan 20 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2400
by: skidvd | last post by:
Hello: I have just recently converted to using the InnoDB table type so that I can enforce FOREIGN key constraints. I have been using MyISAM tables (accessed via JDBC) successfully for some...
3
16928
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
17
4972
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
7
5656
by: Anthony Robinson | last post by:
Have been encountering an odd issue. Every now and again, certain packages of stored procedures just become invalid. I'm aware that dropping or altering an underlying table would render a package...
3
6425
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
2347
by: Mark Flippin | last post by:
I'm evidently not understanding nested triggers and I'm looking for some help. I've an Invoice table (see below) that I want to enforce two actions via after triggers. The first trigger...
6
4816
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
6
4842
by: Carsten | last post by:
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: ...
9
4376
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
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...
0
7508
tracyyun
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...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.