I need to insert data into a table after validating that the data doesn't already exist on the table.
In addition to that I need to update data if there are any changes.
I got the insert part as: - DECLARE
-
@SUGAR_ID VARCHAR (150)
-
SELECT @SUGAR_ID = ID FROM DEVSUGARCRM.DBO.ACCOUNTS --@SUGAR_ID
-
IF NOT EXISTS (SELECT * FROM ADVERTISER WHERE SUGAR_ID = @SUGAR_ID) --NOT EXISTS
-
BEGIN
-
INSERT INTO DBO.ADVERTISER
-
SELECT
-
A.NAME,
-
B.NAME AS PARENT,
-
AM.USER_NAME AS AM,
-
DIVISION_C, A.BILLING_ADDRESS_COUNTRY,
-
AE.USER_NAME AS AE, A.ID AS SUGAR_ID, OOID_C
-
FROM DEVSUGARCRM.DBO.ACCOUNTS A
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.ACCOUNTS B ON A.PARENT_ID = B.ID
-
JOIN DEVSUGARCRM.DBO.ACCOUNTS_CSTM C ON A.ID = C.ID_C
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AM ON USER_ID6_C = AM.ID
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AE ON A.ASSIGNED_USER_ID = AE.ID
-
WHERE A.DELETED = 0
-
AND B.DELETED = 0
-
-
END
-
-
-
--PRINT @SUGAR_ID
-
/*
-
-
SELECT * FROM DBO.ADVERTISER
-
-
DELETE ADVERTISER WHERE SUGAR_ID = '12273c93-8120-791b-a624-4baa1f681215'
-
-
*/
I still need the update part.
What I need is, to verify each rows/columns to find out if there is any changes. If changed then I will need to update them.
2 2592 ck9663 2,878
Recognized Expert Specialist
If you're looking at a small number of records, just update the entire table. It might take it longer to compare each column if there are changes than a direct update. Here's the update syntax.
Happy Coding!!!
~~ CK
@pavelcc
I found the solved it: - DECLARE
-
@NAME VARCHAR(150),
-
@PNAME VARCHAR(150),
-
@AM VARCHAR(150),
-
@DIVISION VARCHAR(150),
-
@B_ADDRESS VARCHAR(150),
-
@AE VARCHAR(150),
-
@SUGAR_ID VARCHAR(150),
-
@OOID_C VARCHAR(150)
-
-
SELECT @SUGAR_ID = ID FROM DEVSUGARCRM.DBO.ACCOUNTS -- GETTING ID AN @SUGAR_ID
-
IF EXISTS (SELECT * FROM ADVERTISER WHERE @SUGAR_ID = SUGAR_ID) -- EXISTS VARIFICATION
-
-
--***************************Update****************************************************************
-
BEGIN
-
SELECT
-
@NAME = A.NAME,
-
@PNAME = B.NAME,
-
@AM = AM.USER_NAME,
-
@DIVISION = DIVISION_C,
-
@B_ADDRESS = A.BILLING_ADDRESS_COUNTRY,
-
@AE = AE.USER_NAME,
-
@SUGAR_ID = A.ID,
-
@OOID_C = OOID_C
-
FROM DEVSUGARCRM.DBO.ACCOUNTS A
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.ACCOUNTS B ON A.PARENT_ID = B.ID
-
JOIN DEVSUGARCRM.DBO.ACCOUNTS_CSTM C ON A.ID = C.ID_C
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AM ON USER_ID6_C = AM.ID
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AE ON A.ASSIGNED_USER_ID = AE.ID
-
WHERE A.DELETED = 0
-
AND B.DELETED = 0
-
-
UPDATE ADVERTISER SET
-
OOID = @OOID_C ,
-
Company_Name = @NAME,
-
Parent_Company = @PNAME,
-
Account_Manager = @AE,
-
Country_Code = @B_ADDRESS,
-
Division = @DIVISION,
-
Account_Executive= @AE
-
-
WHERE Sugar_ID = @SUGAR_ID
-
END
-
--*************************Update Ends here**************************************************
-
--*************************Added to Insert if not updates***************************************
-
ELSE
-
SELECT @SUGAR_ID = ID FROM DEVSUGARCRM.DBO.ACCOUNTS
-
IF NOT EXISTS (SELECT * FROM ADVERTISER WHERE SUGAR_ID = @SUGAR_ID)
-
BEGIN
-
INSERT INTO DBO.ADVERTISER
-
SELECT
-
OOID_C,
-
A.NAME, -- Company Name
-
B.NAME, -- AS PARENT
-
AM.USER_NAME, -- AS AM
-
A.BILLING_ADDRESS_COUNTRY, -- country code
-
DIVISION_C, -- Division
-
AE.USER_NAME, --AS AE,
-
A.ID --AS SUGAR_ID
-
-
FROM DEVSUGARCRM.DBO.ACCOUNTS A
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.ACCOUNTS B ON A.PARENT_ID = B.ID
-
JOIN DEVSUGARCRM.DBO.ACCOUNTS_CSTM C ON A.ID = C.ID_C
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AM ON USER_ID6_C = AM.ID
-
LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AE ON A.ASSIGNED_USER_ID = AE.ID
-
WHERE A.DELETED = 0
-
AND B.DELETED = 0
-
-
END
-
--*****************Ends Here**********************
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Howard Hinnant |
last post by:
I recently asked for a survey of multimap insert with hint behavior, in
support of a paper I'm writing concerning lwg issue 233. My sincere
thanks to Beman Dawes, Raoul Gough, Russell Hind, Bronek Kozicki, Nicola
Musatti, John Potter and Maxim Yegorushkin for helping with that survey.
Since I started work on this paper at least two people I respect very
much have expressed interest in nailing down the "insert without hint"
function a...
|
by: Mark P |
last post by:
Some time ago I posted here about inserting into a set with a hint:
http://groups-beta.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/fb75b00f73e979db/018b8d0eadb38dbf?q=%22STL+insert+with+hint%22+%22Mark+P%22&rnum=1&hl=en#018b8d0eadb38dbf
I quoted the SGI STL docs describing a.insert(p, t), where p is the hint
iterator and t is the inserted object:
"Insert with hint is logarithmic in general, but it is amortized...
|
by: serge |
last post by:
I have a scenario where two tables are in a One-to-Many relationship
and I need to move the data from the Many table to the One table so
that it becomes a One-to-One relationship.
I need to salvage the records from the many table and without going
into detail, one of the reasons I can't do the opposite as
there are records in the ONE table that I need to keep even if they
don't have any child records in the MANY table.
Below I created...
|
by: Philip Boonzaaier |
last post by:
I want to be able to generate SQL statements that will go through a list of
data, effectively row by row, enquire on the database if this exists in the
selected table- If it exists, then the colums must be UPDATED, if not, they
must be INSERTED.
Logically then, I would like to SELECT * FROM <TABLE>
WHERE ....<Values entered here>, and then IF FOUND
UPDATE <TABLE> SET .... <Values entered here> ELSE
INSERT INTO <TABLE> VALUES <Values...
|
by: Carl |
last post by:
Hi,
I hope someone can share some of their professional advice and help me out
with my embarissing problem concerning an Access INSERT query. I have never
attempted to create a table with one-to-one relationship but on this
occasion
I must keep username/password details within a seperate table.
Here's the basic specs and database schema:
-------------------------------------------
| |
by: Chris Kratz |
last post by:
Hello all,
We have run into what appears to be a problem with rules and subselects in
postgres 7.4.1. We have boiled it down to the following test case. If
anyone has any thoughts as to why this would be happening, we would
appreciate feedback.
We have tested on 7.3.4, 7.3.6 and 7.4.1 and all exhibit the same behavior.
Test case one tries to populate table2 from table1 with records that are not
in table2 already. Table2 gets...
|
by: Geoffrey KRETZ |
last post by:
Hello,
I'm wondering if the following behaviour is the correct one for
PostGreSQL (7.4 on UNIX).
I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching
the following request :
INSERT INTO temp_tab VALUES (1,2,3)
|
by: MP |
last post by:
Hi
Posted this several hours ago to another ng but it never showed up thought
i'd try here.
using vb6, ado, .mdb, jet4.0, no access
given table tblJob with field JobNumber text(10)
'The example I had to go by
'INSERT INTO tblCustomers (CustomerID, , )
|
by: lenygold via DBMonster.com |
last post by:
Hi everybody:
What is the best way to
I have 10 tables with similar INSERT requiremnts.
INSERT INTO ACSB.VAATAFAE
WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS
( SELECT AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP
FROM VAATAFAA
WHERE AB_TP_ACNT_STAT_CD <0),
|
by: EJO |
last post by:
with sql 2000 enterprise
Trying to build a stored procedure that will take the rows of a parent
table, insert them into another table as well as the rows from a child
table to insert into another table and be able to maintain the
relationships between the parent/child rows of the new records.
Something like
old_id
new_id
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |