Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IF EXISTS query

Author  Topic 

Ads
Starting Member

23 Posts

Posted - 2012-06-18 : 12:32:15
Hi,

I currently have the below query which works really well. However I would like to have the query so that it reads like this:

IF EXISTS - UPDATE and INSERT ELSE INSERT

Is it possible to integrate the and insert?

IF EXISTS (SELECT * FROM [QuasarTest2].[dbo].[tradersaddresses] WHERE traderid = 'ADLA01') UPDATE [QuasarTest2].[dbo].[tradersaddresses] set isackaddress = 0, isdeladdress = 0, isinvaddress = 0, ismainaddress = 0, isstataddress = 0 WHERE [QuasarTest2].[dbo].[tradersaddresses].[traderid] like 'ADLA01' ELSE INSERT INTO [QuasarTest2].[dbo].[tradersaddresses] (traderid, addressid, street, city, county, postcode, countryid, areaid, currencyid, vatcatid, nlcatid, isexportaddress, isforeigndomcurrency, isexchfixedatorder, pricelistid, eutransportid, languageid, isackaddress, isdeladdress, isinvaddress, ismainaddress, isstataddress, tradertype, createddate, companyid, olversionnumber) VALUES ('ADLA01', 1, 'TEST', 'TEST2', 'TEST3', 'TEST4', 'GB', 'UK', 'STG', 1, 0, 0, 0, 0, 'PLUK', 1, 'GBR', 1, 0, 1, 1, 1, 'C', getdate(), 'RFS', 0)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 12:35:46
then make it like

IF EXISTS (SELECT *
FROM [QuasarTest2].[dbo].[tradersaddresses]
WHERE traderid = 'ADLA01'
)
BEGIN
UPDATE [QuasarTest2].[dbo].[tradersaddresses]
set isackaddress = 0, isdeladdress = 0,
isinvaddress = 0, ismainaddress = 0,
isstataddress = 0
WHERE [QuasarTest2].[dbo].[tradersaddresses].[traderid] like 'ADLA01'
END

INSERT INTO [QuasarTest2].[dbo].[tradersaddresses]
(traderid, addressid,
street, city,
county, postcode,
countryid, areaid,
currencyid, vatcatid,
nlcatid, isexportaddress,
isforeigndomcurrency, isexchfixedatorder,
pricelistid, eutransportid,
languageid, isackaddress,
isdeladdress, isinvaddress,
ismainaddress, isstataddress,
tradertype, createddate,
companyid, olversionnumber)
VALUES ('ADLA01', 1, 'TEST',
'TEST2', 'TEST3', 'TEST4',
'GB', 'UK', 'STG',
1, 0, 0,
0, 0, 'PLUK',
1, 'GBR', 1,
0, 1, 1,
1, 'C', getdate(),
'RFS', 0)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Ads
Starting Member

23 Posts

Posted - 2012-06-19 : 04:12:06
Thanks very much!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-19 : 14:13:16
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -