Solved

How to create view??

Posted on 2000-05-07
1
406 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 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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Many to one in one row 2 40
Building JSON Results Table FROM DB 9 38
sql server cross db update 2 21
T-SQL: Need Group By to use "fuzzy logic"?? 3 27
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

730 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