USE statement inside create view statement possible?

  • I would like to create a view in a database that gets data from another database.  Here is beginning of statement:

    create view data_01..EDISent as
    USE edi_01
    GO
    ......

    The USE statement is underlined in red. Can i not have a USE statement inside a create view statement?

  • Why not just use a the 3-part naming convention when creating your view?

    USE Tempdb;
    GO
    CREATE VIEW MyView
    AS
    SELECT CustomerID
    FROM Galactic.dbo.Customer;

    I'm creating a view inside one database (tempdb) that gets data from another database (Galactic).

  • pietlinden - Saturday, February 17, 2018 1:31 PM

    Why not just use a the 3-part naming convention when creating your view?

    USE Tempdb;
    GO
    CREATE VIEW MyView
    AS
    SELECT CustomerID
    FROM Galactic.dbo.Customer;

    You're right.  USE is not allowed in views or stored procedures apparently.  Thanks.

  • Jackie Lowery - Saturday, February 17, 2018 1:35 PM

    pietlinden - Saturday, February 17, 2018 1:31 PM

    Why not just use a the 3-part naming convention when creating your view?

    USE Tempdb;
    GO
    CREATE VIEW MyView
    AS
    SELECT CustomerID
    FROM Galactic.dbo.Customer;

    You're right.  USE is not allowed in views or stored procedures apparently.  Thanks.

    That's because the USE statement isn't a query element, it's a batch directive, similar to GO in that respect.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply