Solved

How to create view??

Posted on 2000-05-07
1
398 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
1 Comment
 
LVL 8

Accepted Solution

by:
chigrik earned 20 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now