473,651 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert data into table after validation

2 New Member
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 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
Jun 3 '10 #2
pavelcc
2 New Member
@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
2510
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...
6
7000
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...
14
4286
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...
16
17004
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...
8
6284
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: -------------------------------------------
4
5473
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...
2
3194
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)
3
2305
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, , )
6
3712
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),
1
2645
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
0
8275
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,...
0
8694
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 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...
1
8457
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,...
0
8571
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 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...
0
7294
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, 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...
1
6157
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 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...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 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...

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.