473,416 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to insert data into table after validation

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:

Expand|Select|Wrap|Line Numbers
  1. DECLARE 
  2. @SUGAR_ID VARCHAR (150)    
  3. SELECT @SUGAR_ID =  ID FROM DEVSUGARCRM.DBO.ACCOUNTS --@SUGAR_ID 
  4.     IF NOT EXISTS (SELECT * FROM ADVERTISER WHERE SUGAR_ID = @SUGAR_ID)  --NOT EXISTS 
  5.       BEGIN
  6.         INSERT INTO DBO.ADVERTISER
  7.             SELECT      
  8.                 A.NAME, 
  9.                 B.NAME AS PARENT, 
  10.                 AM.USER_NAME AS AM, 
  11.                 DIVISION_C, A.BILLING_ADDRESS_COUNTRY, 
  12.                 AE.USER_NAME AS AE, A.ID AS SUGAR_ID, OOID_C
  13.             FROM  DEVSUGARCRM.DBO.ACCOUNTS A 
  14.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.ACCOUNTS B ON A.PARENT_ID = B.ID
  15.                   JOIN DEVSUGARCRM.DBO.ACCOUNTS_CSTM C ON A.ID = C.ID_C
  16.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AM ON USER_ID6_C = AM.ID
  17.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AE ON A.ASSIGNED_USER_ID = AE.ID
  18.             WHERE A.DELETED = 0
  19.             AND   B.DELETED = 0
  20.  
  21.       END
  22.  
  23.  
  24. --PRINT @SUGAR_ID
  25. /*
  26.  
  27. SELECT * FROM DBO.ADVERTISER
  28.  
  29. DELETE ADVERTISER WHERE SUGAR_ID = '12273c93-8120-791b-a624-4baa1f681215'
  30.  
  31. */
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.
Jun 2 '10 #1
2 2581
ck9663
2,878 Expert 2GB
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
Jun 3 '10 #2
@pavelcc
I found the solved it:
Expand|Select|Wrap|Line Numbers
  1. DECLARE 
  2.     @NAME VARCHAR(150), 
  3.     @PNAME VARCHAR(150), 
  4.     @AM VARCHAR(150), 
  5.     @DIVISION VARCHAR(150), 
  6.     @B_ADDRESS VARCHAR(150), 
  7.     @AE VARCHAR(150), 
  8.     @SUGAR_ID VARCHAR(150), 
  9.     @OOID_C VARCHAR(150)
  10.  
  11. SELECT @SUGAR_ID =  ID FROM DEVSUGARCRM.DBO.ACCOUNTS                -- GETTING ID AN @SUGAR_ID 
  12. IF EXISTS (SELECT * FROM ADVERTISER WHERE @SUGAR_ID = SUGAR_ID)  -- EXISTS VARIFICATION
  13.  
  14. --***************************Update****************************************************************
  15. BEGIN
  16. SELECT      
  17.     @NAME        =    A.NAME, 
  18.     @PNAME        =    B.NAME, 
  19.     @AM            =    AM.USER_NAME, 
  20.     @DIVISION    =    DIVISION_C, 
  21.     @B_ADDRESS    =    A.BILLING_ADDRESS_COUNTRY, 
  22.     @AE            =    AE.USER_NAME, 
  23.     @SUGAR_ID    =    A.ID, 
  24.     @OOID_C        =    OOID_C
  25.             FROM  DEVSUGARCRM.DBO.ACCOUNTS A 
  26.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.ACCOUNTS B ON A.PARENT_ID = B.ID
  27.                   JOIN DEVSUGARCRM.DBO.ACCOUNTS_CSTM C ON A.ID = C.ID_C
  28.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AM ON USER_ID6_C = AM.ID
  29.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AE ON A.ASSIGNED_USER_ID = AE.ID
  30.             WHERE A.DELETED = 0
  31.             AND   B.DELETED = 0
  32.  
  33.         UPDATE    ADVERTISER SET 
  34.                     OOID = @OOID_C ,
  35.                     Company_Name = @NAME, 
  36.                     Parent_Company = @PNAME, 
  37.                     Account_Manager = @AE,
  38.                     Country_Code = @B_ADDRESS, 
  39.                     Division = @DIVISION, 
  40.                     Account_Executive= @AE 
  41.  
  42.         WHERE Sugar_ID = @SUGAR_ID
  43. END
  44. --*************************Update Ends here**************************************************
  45. --*************************Added to Insert if not updates***************************************
  46. ELSE 
  47. SELECT @SUGAR_ID =  ID FROM DEVSUGARCRM.DBO.ACCOUNTS                        
  48.     IF NOT EXISTS (SELECT * FROM ADVERTISER WHERE SUGAR_ID = @SUGAR_ID)        
  49.       BEGIN
  50.         INSERT INTO DBO.ADVERTISER
  51.             SELECT      
  52.                 OOID_C,
  53.                 A.NAME,                            -- Company Name
  54.                 B.NAME,                            -- AS PARENT 
  55.                 AM.USER_NAME,                    -- AS AM 
  56.                 A.BILLING_ADDRESS_COUNTRY,        -- country code
  57.                 DIVISION_C,                        -- Division                 
  58.                 AE.USER_NAME,                    --AS AE, 
  59.                 A.ID                            --AS SUGAR_ID 
  60.  
  61.             FROM  DEVSUGARCRM.DBO.ACCOUNTS A 
  62.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.ACCOUNTS B ON A.PARENT_ID = B.ID
  63.                   JOIN DEVSUGARCRM.DBO.ACCOUNTS_CSTM C ON A.ID = C.ID_C
  64.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AM ON USER_ID6_C = AM.ID
  65.                   LEFT OUTER JOIN DEVSUGARCRM.DBO.USERS AE ON A.ASSIGNED_USER_ID = AE.ID
  66.             WHERE A.DELETED = 0
  67.             AND   B.DELETED = 0
  68.  
  69.       END
  70. --*****************Ends Here**********************
Jun 3 '10 #3

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

Similar topics

3
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...
6
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: ...
14
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...
16
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...
8
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...
4
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...
2
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 :...
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...
6
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,...
1
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...
0
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
0
BarryA
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...
1
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...
0
Oralloy
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,...
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...
0
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
isladogs
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...

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.