|
how to get the result intersection of two queries.
can any one give code ???
I tried the following query
select * from tableA
intersect
select * from tableB
but it showing the error like:
Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'intersect'.
please anybody help.
|
|
|
|
|
there is no intersect in SQL Server 2000
select * from tableA where colA not in (select colA from tableB)
this returns all rows from tableA which has no corresponding row matching for colA in tableB
Regards
KP
|
|
|
|
|
There are only 3 set operators i.e. union, except, crossjoin!!
However if you want the intersection to be done you could either use where exists , not exists or inner joins
examples:
SELECT DISTINCT city
FROM authors
WHERE EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
SELECT DISTINCT authors.city
FROM authors INNER JOIN publishers
ON authors.city = publishers.city
SELECT DISTINCT city
FROM authors
WHERE NOT EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
Gautham
|
|
|
|
|
Thanks for solution but, still my problem is not solvedi will make clear what is my problem:
tableA tableB
------------------------------------
id skl.name id skl.name
--------- ---------
1 vb 2 java
2 c# 2 EJB
2 vb.net 3 J2me
3 asp.net 4 jsp
in these two table i need fetch the data like:
id skl.name
-------------
2 c#
2 vb.net
2 java
2 ejb
3 asp.net
3 j2me
regards,
PRT
|
|
|
|
|
Hi
I have little doubt on your question. Do you want to intersect those two tables based on the column ID ??
If Table1 has {1,2,2,3} and Table 2 {2,2,3,4} then you want {2,3,4} display values from two tables.
Rate this message. Thank you. Harini
|
|
|
|
|
Hi Parasu,
Your solution is here:
<br />
SELECT TABLEA.* FROM TABLEA<br />
WHERE EXISTS<br />
(SELECT * FROM TABLEB WHERE TABLEA.ID = TABLEB.ID)<br />
UNION <br />
SELECT TABLEB.* FROM TABLEB<br />
WHERE EXISTS <br />
(SELECT * FROM TABLEA WHERE TABLEA.ID = TABLEB.ID)<br />
Please note the difference here:
UNION
This removes duplicate results
UNION ALL
Incorporates all rows into the results, including duplicates. If not specified, duplicate rows are removed.
Hope this solves your problem
Rate this message. Thank you. Harini
|
|
|
|
|
Thank you very much Harini
|
|
|
|
|
Select * from tablea,tableb where tablea.columnname=tableb.columnname.
Generally Intersection means having data inboth the tables should be displayed.
For this we have to put a column in both tables which contains same data
So, Select * from two tables where columnin tablea=column in tableb are equal
Priya
|
|
|
|
|
Is it possible to run a .sql file on sql server through .net code?
Nana
|
|
|
|
|
NanaAM wrote: Is it possible to run a .sql file on sql server through .net code?
Yes.
However, remember that GO is not part of SQL. It is a preprocessor commant to the Query Analyser that tells it where to split the Script into individual batches. You must make the same splits when issuing the SQL via the SqlCommand.
I blogged in more detail about this here[^] - It will show you how to handle the situation with GOs too.
|
|
|
|
|
Thanks for your help.
But I wanted to ask whether we can run the script file, the .sql file direclty?
For this I need to store the file contents in string and pass it to commandText
Nana
|
|
|
|
|
NanaAM wrote: But I wanted to ask whether we can run the script file, the .sql file direclty?
For this I need to store the file contents in string and pass it to commandText
Yes, my previous answer shows you how to do that. Please look at the source code to the application to see how that is done.
|
|
|
|
|
Dear All,
i want to know how to get top three salary getters from the employee(eid , ename, salary) table
i tried this
select top 3 salary from employee order by salary desc
but it gives me top three salary record say there is salary 1000,1200,1300,1300,1500
then my query return me 1500,1300,1200 whereas i want to 1500,1300,1300,1200
how can i do it
please help
thanks
regards
imran khan
|
|
|
|
|
SELECT * FROM Employee
WHERE (Salary >=SELECT TOP 1 Salary FROM (SELECT DISTINCT TOP 3 Salary FROM Employee ORDER BY Salary DESC) DERIVEDTBL ORDER BY Salary))
|
|
|
|
|
Here is the pur SQL query. This will work irrespective of any RDBMS
select eid , ename, salary from employee
where salary in
(
(select max(salary) from employee),
(select max(salary) from employee
where salary<
(select max(salary) from employee)),
(select max(salary) from employee
where salary<(select max(salary) from employee
where salary<(select max(salary) from employee)))
)
order by salary desc
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
Hello,
Can someone tell me how can I get the entire script using query of table.
What I mean is, in SQL server, if we right click on a table, and say script table as-create to-New Query Editor Window, we get the entire script of the table.
I want that using a query.
Please help me its urgent.
Nana
|
|
|
|
|
please enlighten us more . give us an example
|
|
|
|
|
Thanks Sam.
I got the answer.
We can user SMO namespace
Nana
|
|
|
|
|
Hi all,
if we want to use Stored procedures in VB.Net page, then what are the namespaces we have to import?? and how can we implement Transaction management in the stored procedure??
thanks,
Rahi
If you look at what you do not have in life, you don't have anything,
If you look at what you have in life, you have everything... "
|
|
|
|
|
system.data.sqlclient
transaction can be maintained by simple way of begin transaction and thereby commit transaction or rollback the transaction.
If not you can use the concurrency levels too!!!
Gautham
|
|
|
|
|
hello
i'm trying to create One to One and One to Many relationships between two tables.
can anyone plz tell me the queries for
1. One to One
2. One to Many
supposing i have two table Table1 and Table2.
plz tell query for Onne to One relationship between Table1 and TAble2 and also One to many relationship between Table1 and Table2 through Alter table query(using Sql Server 2000)
Thanks for the time
Saira
|
|
|
|
|
The difference between One-to-One and One-to-Many is mostly conceptual. A query for either will be exactly the same. However, it can be enforced using a combination of a Foreign Key constraint and a Unique constraint. What this will do is enforce the relationship for UPDATE, INSERT and DELETE, but again, not SELECT.
If your schema looks like this:
<br />
CREATE TABLE Table1 (<br />
[ID] INT,<br />
BLAH,<br />
BLAH2<br />
)<br />
<br />
CREATE TABLE Table2 (<br />
[ID] INT,<br />
Table1ID INT,<br />
BLAH,<br />
BLAH2<br />
)<br />
<br />
Then you would create your constraints like this:
<br />
ALTER TABLE Table2 ADD CONSTRAINT FK_Table2_Table1 FOREIGN KEY(<br />
Table1ID<br />
) REFERENCES Table1 (<br />
ID<br />
)<br />
<br />
<br />
ALTER TABLE Table2 ADD CONSTRAINT UK_Table2 UNIQUE(<br />
Table1ID<br />
)<br />
|
|
|
|
|
Sorry, I forgot to specify. If you have a One-to-Many relationship you would like to enforce then all you need is the FOREIGN KEY constraint. If you have a One-to-One relationship then you need both the FOREIGN KEY and the UNIQUE constraints.
|
|
|
|
|
i have 3 columns in my table the room,timeStart,timeEnd.
For example i have inserted this data
room: room1
timeStart: 2007-03-02 8:00 AM
timeEnd: 2007-03-02 3:00 PM
Here's the data that will not be accepted if try to insert
room: room1
timeStart: 2007-03-02 9:00 AM
timeEnd: 2007-03-02 4:00 PM
because the timestart is between the timeStart and the timeEnd. Room1 will only be occupied after 3:00 PM.
Can you help me with this?.
Thank you very Much
Bernie
|
|
|
|
|
Use Trigger and put your condition there
|
|
|
|