Search code examples
sql-servert-sqlsql-view

How can I create a view from more than one table?


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.


Solution

  • 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: