Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
CREATE VIEW buy
As
select Fname,Lname,Branch_Name,Hrs
from customer,Branch,Buysfrom
where Branch.Branch_id=Buysfrom.Bid
And Branch.Branch_id=customer.Customer_id2
select *
from buy


What I have tried:

Msg 156, Level 15, State 1, Procedure buy, Line 7 [Batch Start Line 101]
Incorrect syntax near the keyword 'select'.

Completion time: 2022-12-13T20:02:23.1563244+02:00
Posted
Updated 14-Dec-22 0:15am
v2

You can't create a view using two separate SELECT statements - they each create a table and that confuses things.

Think about wnat exactly you want the view to do, probably you want a UNION between them.
 
Share this answer
 
Comments
Dave Kreskowiak 13-Dec-22 14:18pm    
Even better, the second SELECT is grabbing all the records from the very view he's creating!
OriginalGriff 13-Dec-22 14:52pm    
So it is ... I didn't notice that!
Assuming you meant to create the view, then select everything from it, you need two separate statements.

You'll also want to use the proper JOIN syntax, rather than the ancient and obsolete pre-ANSI-92 syntax you've currently got.
SQL
CREATE VIEW buy
As
    SELECT 
        Fname,
        Lname,
        Branch_Name,
        Hrs
    FROM 
        customer
        INNER JOIN Branch ON Branch.Branch_id = customer.Customer_id2
        INNER JOIN Buysfrom ON Buysfrom.Bid = Branch.Branch_id
GO
select *
from buy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900