question

Allenb avatar image
Allenb asked

How to Convert a Query to a CREATE VIEW

Hello, Here is my query sample. SELECT * INTO [Master_Table] FROM My_Table I like to use a CREATE VIEW that includes this query. Something like this. EXEC('CREATE VIEW [View_Master_Table] AS SELECT * INTO [Master_Table] FROM My_Table ') When I run the View an error occurred. SELECT * FROM [View_Master_Table] **Incorrect syntax near the keyword 'INTO'.** Any help would be much appreciated
sql-server-2008-r2
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
JohnM avatar image
JohnM answered
By default, the CREATE VIEW statement doesn't allow the INTO keyword. Furthermore, even if you could, you would only be able to execute the view once as the table will only be created once. Reference: https://msdn.microsoft.com/en-us/library/ms187956.aspx This type of logic probably should be in a stored procedure. I don't know what you are doing, but using a temporary table might be better suited than a physical table. Just throwing that out there. Hope that helps!
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I got your point. Thanks for your time.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.