Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to use union and minus in Sybase ?

Posted on 2010-09-15
9
13,384 Views
Last Modified: 2012-05-10
I have to compare 2 Sybase tables table_A and table_B , they have the same collum  name, they maybe have different data.
what i am thinking is :
select name, department,... from table_A
minus
select name,department,... from table_B

but Sybase does not support minus ?!

is there any similar SQL in Sybase?


0
Comment
Question by:SayYou_SayMe
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Accepted Solution

by:
alpmoon earned 300 total points
ID: 33688767
Do you have a key column? Is it ASE?

If name is primary key, I think this should work:

select * from table_A A
where not exists (select * from table_B B
                             where B.name = A.name)
0
 
LVL 6

Assisted Solution

by:sridharv9
sridharv9 earned 200 total points
ID: 33689025
As there is no minus operator available in ASE you can alternate option as above with "not exists" or "not in".
0
 

Author Comment

by:SayYou_SayMe
ID: 33690166
thanks guys

i have to compare all columns between tableA and tableB
just to compare the values in the tables if they are exactly the same
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Assisted Solution

by:sridharv9
sridharv9 earned 200 total points
ID: 33695175
Try this query to find any difference between two tables. Replace .d,.c,.b with table columns.

SELECT Table2.*
FROM Table2 LEFT JOIN Table1 ON (Table2.d = Table1.d) AND (Table2.c =
Table1.c) AND (Table2.b = Table1.b) AND (Table2.a = Table1.a) AND
(Table2.ID = Table1.ID)
WHERE (((Table1.ID) Is Null));
0
 

Author Comment

by:SayYou_SayMe
ID: 33696052
thanks guys

alpmoon--

your solution only works for checking one clumn Name, but i have to check all the fields are diferent .

another word, if the name is the same in both tables, it will show no result, whatever the other fields the same or not ( it do not match my request)

alpmoon--

your solution seems not work




0
 
LVL 13

Assisted Solution

by:alpmoon
alpmoon earned 300 total points
ID: 33698625
That was the reason I asked whether you have a primary key. If you need to compare all columns, you can add others in where clause:

select * from table_A A
where not exists (select * from table_B B
                             where B.name = A.name and B.department = A.department and ....)
0
 

Author Comment

by:SayYou_SayMe
ID: 33700602
thanks alpmoon

if there are more than 200 columns , is there any solution?
0
 
LVL 13

Assisted Solution

by:alpmoon
alpmoon earned 300 total points
ID: 33702591
Having a primary key or a better data model in general
0
 

Author Comment

by:SayYou_SayMe
ID: 33740220
I have 2 sybase tables
table_A
source, name, department,
New_York  Bill         IT
London        mike     HR
table_B
name,department
Bill   IT
Mike   HR

Table_B will have the data of New York or London

I want to create a stored procedure to compare the 2 tables if the values of columns are exactly the same and return a message


CREATE PROC data_chk
@source      VARCHAR(10)
as
begin
CREATE TABLE #t (source      VARCHAR(10)     null,
                   update_status            VARCHAR(100)    null)
select * from table_B a
where not exists (select * from table_A b
                  where isnull(a.name,' ')=isnull(b.name,' ')
                  and isnull(a.department,' ')=isnull(b.department,' ')
                  and a.source=@source)
if      @@rowcount = 0
  insert into #t
         values(@source,
                 'no update')
                 
                 
select      *
from      #t
return      0
end

I can run the stored procedure with no error, but I can not call it from Informatica, I will give error message
 --basically because it returna 2 result set
   One is :select * from table_B …
 One is select * from #T
But if  I  create the store procedure like this , it works
CREATE PROC data_chk
@source      VARCHAR(10)
as
begin
CREATE TABLE #t (source      VARCHAR(10)     null,
                   update_status            VARCHAR(100)    null)
/*select * from table_B a
where not exists (select * from table_A b
                  where isnull(a.name,' ')=isnull(b.name,' ')
                  and isnull(a.department,' ')=isnull(b.department,' ')
                  and a.source=@source)
if      @@rowcount = 0 */
  insert into #t
         values(@source,
                 'no update')
                 
                 
select      *
from      #t
return      0
end

question:
how to create this Sybase stored procedure to achieve my request?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question