473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Set based solution instead of Cursor

77 New Member
Can anyone provide a simple example of a set based solution to this? Not sure if there is one, but before I put this thing into production I thought I would ask.

Get all records matching condition in Table A
If record does not exist in Table B Insert it
Otherwise Update the existing record in Table B from Table A values

Thanks in advance.
May 21 '09 #1
3 2146
ck9663
2,878 Recognized Expert Specialist
Two queries:

1. UPDATE for all existing records.

2. INSERT INTO for the new ones


--- CK
May 22 '09 #2
stoogots2
77 New Member
CK,

I guess the problem I have is that I don't have a unique constraint to restrict the INSERT INTO from duplicating all records. I'd rather not modify that.

Thanks for the answer.
May 26 '09 #3
ck9663
2,878 Recognized Expert Specialist
Cant' you handle that in WHERE clause?

Something like...

Expand|Select|Wrap|Line Numbers
  1.  
  2. INSERT INTO TargetTable 
  3. SELECT s.* from SourceTable s
  4. where not exists (selec 1 from TargetTable t where t.key = s.key)
  5.  
  6.  
May 26 '09 #4

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

Similar topics

7
by: Philip Mette | last post by:
Does anyone have any good references they could recommend on Cursor based SQL writing? I have to create SQL that can loop though records simular to VB loops and I have been told that this is the...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
3
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
13
by: JayCallas | last post by:
I know this question has been asked. And the usual answer is don't use cursors or any other looping method. Instead, try to find a solution that uses set-based queries. But this brings up...
1
by: klalonde | last post by:
I was researching opinions on using cursors in stored procedures and found a thread http://www.thescripts.com/forum/thread143091.html discussing (generally) why not to use them. I took a...
0
by: crusoe | last post by:
I have some queries which return rowsets. I want to make XHTML tables from these sets in a single report file. My preliminary solution is a procedure, which returns varchar(8000) containing XHTML...
1
by: Simon | last post by:
Julian, you wrote about a possible solution for my problem, it's a way I never thought about, it looks perfect. But onfortunately, I don't know enough about javascript to convert your solution...
0
by: rkandas | last post by:
Order by based on input Select col1, col2, col3 from table 1 order by col1 <ASC/DESC>, col2 <ASC/DESC>, col3 <ASC/DESC> The sort order for col1, col2, col3 are parameters to the program. Can...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
0
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...
0
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
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
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
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
agi2029
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,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.