문제

I have to create a view from more than one table in an MS SQL Server database, but I am not able to get the correct syntax for the same.

도움이 되었습니까?

해결책

You'll have to provide more information about how you are looking to return data from more than one table. Typically you use JOINs:

CREATE VIEW your_view_vw AS
   SELECT *
     FROM TABLE_A a
     JOIN TABLE_B b ON b.pk = a.fk

...where fk stands for "Foreign Key", and pk stands for "Primary Key" - assuming these constraints are in place. Maybe you need to use a Cross join instead? Here's a great visual representation of JOINs visually.

Reference:

다른 팁

You do this with JOINs, just like you would with a regular query.

If you can write a query that gets you the data, you should be able to write view nearly the exact same way.

Post what you have.

example

create view ViewCustomerOrders
as
select * from Customer c
join Order o on o.CustomerID = c.CustomerID 
create view viewname
as
select * from table a
join table b on b.col2 = a.col2

create view view_name as select * from table_A a join table_B b on a.column_id = b.column_id

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top