开发者

Sql query for stored procedure

开发者 https://www.devze.com 2023-03-05 20:53 出处:网络
I have five tables \"ArtBook,Eng,comm,Law,mgt\" and every table has a same column and i want to be search all the information that particular book by its id

I have five tables "ArtBook,Eng,comm,Law,mgt" and every table has a same column and i want to be search all the information that particular book by its id e.g-

select * from ArtBook,Eng,Comm,Law,mgt where @BookId=ArtBooks.BookId
or @BookId=CommBooks.Boo开发者_JAVA百科kId
or @BookId=Eng.BookId
or @BookId=Law.BookId
or @BookId=mgt.BookId


If all the tables store books of different categories, then combine them back into one table and add a 'category' column. Categorising into separate tables like this isn't a good idea for the reasons you've demonstrated.


select * 
from ArtBook
where BookId = @BookId
union all
select * 
from Eng
where BookId = @BookId
union all
select * 
from Comm
where BookId = @BookId
union all
select * 
from Law
where BookId = @BookId
union all
select * 
from Mgt
where BookId = @BookId


One could use the UNION or UNION ALL operator.

(SELECT * FROM ArtBook)
UNION ALL
(SELECT * FROM Eng)
...
WHERE @BookId=ArtBooks.BookId or
       @BookId=CommBooks.BookId or 
       @BookId=Eng.BookId or 
       @BookId=Law.BookId or 
       @BookId=mgt.BookId

et cetera, et cetera. Though you may consider restructuring how your data is stored to make this sort of thing easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消