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 2581
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...
|
by: Mark P |
last post by:
Some time ago I posted here about inserting into a set with a hint:
...
|
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...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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,...
|
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...
|
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: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
| |