update query with insertion? - sql

i need a query which have to do both update and insert..
first i have to check this condition
SELECT TOP 1 * FROM NEC_Customer_DB_Map where DB_AvailabilityFlag = 'Y'
and if DB_AvailabilityFlag = 'Y' i have to update this 'Y' as 'ASSIGNED' and also i
have to insert by using
INSERT INTO NEC_Customer_DB_Map(NEC_CustomerCode,NEC_CustomerName) VALUES(#NEC_CustomerCode,#NEC_CustomerName)
can anyone combine these into one query..Any Suggestion?

Since your question is tagged with sql-server-2008 you could consider usinge the MERGE-statement:
http://technet.microsoft.com/en-us/library/bb510625.aspx

You could use a where clause to make the insert conditional:
INSERT INTO NEC_Customer_DB_Map(NEC_CustomerCode,NEC_CustomerName)
SELECT #NEC_CustomerCode,#NEC_CustomerName
WHERE EXISTS (SELECT * FROM NEC_Customer_DB_Map where DB_AvailabilityFlag = 'Y')

Related

How to rewrite this sql query, that updates multiple rows in a single column, WITHOUT using CASE

I am working on a new project and am trying to update multiple rows in a single column. It appears any query using the CASE syntax is throwing an error. I am needing to rewrite this query so that the CASE syntax is not used. Any help here is appreciated, i have:
UPDATE tableA
SET
column_a = CASE WHEN column_a = 'conserve' THEN 'fixed'
WHEN column_a = 'balance' THEN 'mod'
WHEN column_a = 'balance growth' THEN 'mod growth'
WHEN column_a = 'aggressive' THEN 'mod/agressive'
END;
The error I am seeing in our pipleine is:
Caused by: liquibase.exception.DatabaseException: ERROR: syntax at or near "column_a"
I am looking for alternatives to using CASE when updating multiple rows in a single column.
Not sure what a reason to not use CASE (coz imo it's a more useful option), but here's dbfiddle with a couple of alternatives for the UPDATE statement (REPLACE and CTE):
-- replace example
UPDATE tableA
SET
column_a = REPLACE(REPLACE(REPLACE
(column_a, 'conserve', 'fixed'),
'balance', 'moderate'),
'aggressive','moderate/agressive');
-- CTE example
UPDATE tableA
SET
column_a = tmp.tmp_val
FROM (
SELECT 'conserve' as tmp_key, 'fixed' as tmp_val
union select 'balance', 'moderate'
union select 'balance growth', 'moderate growth'
union select 'aggressive', 'moderate/agressive'
) tmp
WHERE tmp.tmp_key = tableA.column_a
;
I would write this query like as follows. It's not pritty but should get the job done.
UPDATE tableA
SET column_a = REPLACE(column_a, 'conserve', 'fixed'),
column_a = REPLACE(column_a, 'balance', 'moderate'),
...

multiple values to oracle case statement then

Can some one please explain how to pass multiple values to oracle case statement Then
SELECT *
FROM impl_debitor_information
WHERE soft_delete='F'
AND SHOP_ID ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND (debitor_type IS NULL
OR debitor_type IN (CASE
WHEN (SELECT techfund_debitor_enabled
FROM impl_shop
WHERE shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099') = 'YES' THEN ('T','D')
ELSE 'D'
END))
If values from
select techfund_debitor_enabled from impl_shop where shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099' is "YES" then I need to pass 2 values to in clause, if not single value
Thanks in advance
CASE will only return a single value. You must rewrite your query. Something like this:
SELECT *
FROM impl_debitor_information i, impl_shop where shop_id s
WHERE d.soft_delete='F'
AND d.shop_id ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND d.shop_id = s.shop_id
AND (d.debitor_type IS NULL
OR (d.debitor_type IN ('T','D') AND s.techfund_debitor_enabled = 'YES')
OR (d.debitor_type IN ('D') AND s.techfund_debitor_enabled <> 'YES'))
There might be errors in it, I didn't test the query.

How to summarize SQL table to return value conditionally

I have a table with several rows, and several columns. It looks like this:
Name Description
X PASS
X PASS
X FAIL
I want it to return only one row. If all of them are PASS, return PASS.
If one or more of them are FAIL, then return FAIL.
What's the best way to go about achieving this in SQL Server 2008?
EDIT: The values in the name column will always be the same.
Depending on the database indexes, and assuming you want one row returned per unique name, I would look at the performance of
select
name,
min([description]) as description
from
tableA
group by
name
compared to the other solutions
SELECT TOP 1 CASE Description WHEN 'FAIL' THEN 'FAIL' ELSE 'PASS' END
FROM DaTable
ORDER BY Description
OP: Is it possible that the table is empty? In that case this query won't return any rows, obviously.
EDIT
According to aquinas's comment I created a modified query without ordering:
SELECT CASE COUNT(Description) WHEN 0 THEN 'FAIL' ELSE 'PASS' END
FROM DaTable
WHERE Description = 'FAIL'
This query will return PASS if DaTable is empty.
This is the simplest solution you will find:
SELECT MIN(Description) FROM tbl
If there's at least one FAIL, then our result column will contain FAIL, otherwise, it will contain PASS.
You can use EXISTS to get the existance of a row containing "FAIL".
You could also try something like:
SELECT TOP 1 COALESCE(tFail.Description,t.Description)
FROM myTable AS t
LEFT JOIN myTable AS tFail ON tFail.Name = t.Name AND tFail.Description = 'FAIL'
WHERE t.Name = 'x'
Here is the query:
--DROP TABLE result
CREATE TABLE result(Name varchar(10),Description varchar(20))
--select * from result
INSERT INTO result
VALUES('X','PASS'),('X','PASS'),('X','FAIL')
;WITH CTE(descp,cnt) as (SELECT [description],COUNT(*) as cnt FROM result group by [description])
SELECT CASE WHEN COUNT(*) > 1 then 'FAIL' when COUNT(*)=1 then MAX(descp) else 'PASS' END FROM CTE

2 SQL Updates in 1 statement

Im trying to do something like the following:
UPDATE table SET fieldA='valueA' WHERE id='id1', SET fieldB='valueB' WHERE id='id2';
But cant seem to get it to work. Is there a way of doing this, or will I need to use two separate SQL commands?
If you really need to do it in one statement, you can use this:
UPDATE table
SET fieldA = case when id = 'id1' then 'valueA' else fieldA end,
fieldB = case when id = 'id2' then 'valueB' else fieldB end
WHERE id in ('id1', 'id2');
But for clarity (and thus maintainability), it would be much better to use two statements.
This is best done as 2 different UPDATE statements, because you have 2 different WHERE clauses:
UPDATE table SET fieldA='valueA' WHERE id='id1'
UPDATE table SET fieldB='valueB' WHERE id='id2'
MERGE INTO YourTable
USING ( VALUES ( 'id1', 'valueA', NULL ),
( 'id2', NULL, 'valueB' ) )
AS source ( id, fieldA , fieldB )
ON YourTable.id = source.id
WHEN MATCHED THEN
UPDATE
SET fieldA = COALESCE(source.fieldA, YourTable.fieldA),
fieldB = COALESCE(source.fieldB, YourTable.fieldB);
You'll need to use 2 separate SQL-statements because the set operator works on the whole resultset that's filtered by the where-clause.
UPDATE table SET fieldA='valueA' WHERE id='id1'; UPDATE table SET fieldB='valueB' WHERE id='id2';
UPDATE table SET fieldA='valueA',fieldB='valueB' WHERE id in ('id1','id2')

How to return multiple values using case statement in oracle

I want to return multiple values from a query in oracle. For ex:
select count(*)
from tablename a
where asofdate='10-nov-2009'
and a.FILENAME in (case
when 1 = 1 then (select distinct filename from tablename
where asofdate='10-nov-2009' and isin is null)
else null
end);
I am getting error: ora 01427 single row subquery returns more than one row
Please advice.
Thanks, Deepak
A CASE statement cannot return more than one value, it is a function working on one value.
It is not required for your statement, this statement should work:
select count(*)
from tablename a
where asofdate='10-nov-2009'
and a.FILENAME in (select distinct filename
from tablename
where asofdate='10-nov-2009'
and isin is null);
Maybe you have another usage scenario in mind? Something like this:
Select *
From aTable
Where in CASE
WHEN Then
WHEN Then
ELSE END
Then using CASE may not be the right scenario. Maybe this helps you in the right direction:
Select *
From aTable
Where <Case1> and column1 in <Subselect1>
Or <Case2> and column1 in <Subselect2>
OR Not (<Case1> Or <Case2>) and column1 in <Subselect3>
But this will probably be quite some work for the optimizer ...
The distinct in your Case statement is attempting to return multiple values when only one is allowed, and your SELECT statement will only return one value in one row currently. If you're trying to get the count of each filename, do
SELECT FileName, Count(*)
FROM tablename
WHERE asofdate='10-nov-2009' and isin is null
GROUP BY FileName
Run this query:
select distinct filename from tablename
where asofdate='10-nov-2009' and isin is null
You'll see that it returns more than a single row which causes the ORA-01427.
For all I can tell, you're looking for something like:
select a.filename, count(*)
from tablename a
where a.asofdate = '10-nov-2009'
and exists (
select *
from tablename b
where b.isin is null
and a.asofdate = '10-nov-2009'
and a.filename = b.filename
)
group by a.filename
This would find the count of filenames for a day, for which there exists at least one row where isin is null.
If you edit your question and add an explanation of what you're looking for, you might get better answers.