Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to create view??

Posted on 2000-05-07
1
Medium Priority
?
413 Views
Last Modified: 2010-05-18
How can i create a view in a stored procedure?

Here's my sample code:

---------------------------------------
PROCEDURE sp_CreateView (@Model char(3)) AS

declare @ViewName char(6)

select @ViewName = 'vw_' + @Model

create view @ViewName AS
select * from Serial where substring(SerialNo,1,3) = @Model
---------------------------------------
it always prompt me a message 'CREATE VIEW must be the first command in a query batch.'
0
Comment
Question by:yyjulie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
chigrik earned 60 total points
ID: 2786909
You can use dynamic SQL statement.
Read about EXEC statement from Books Online.
This is the example:

CREATE PROCEDURE sp_CreateView (@Model char(3)) AS
DECLARE @ViewName char(6),
        @exec_str varchar (255)
SELECT @ViewName = 'vw_' + @Model
SELECT @exec_str = 'CREATE VIEW ' + @ViewName + ' AS
       SELECT * FROM Serial WHERE substring(SerialNo,1,3) = ' + @Model
EXEC (@exec_str)
GO
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question