473,378 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Create view from cursor

I have multiple locations that I want to create views for each
individual location.

I am using a cursor to create the views for each location. So, the
cursor grabs site #1 then <should> create view_site_#1, then grab site
#2 and <should> create view_site_#2.

For some reason it doesn't like the view name with the @site in it.
Any ideas of how to get this done?

Here's the cursor...

declare @site varchar(5)

declare c_site cursor for
select station from VHAISLCAUDIA.VISN_SITE
order by station
open c_site
fetch from c_site
into @site

while (@@fetch_status = 0)
begin

CREATE VIEW Site_All_Data_+ @site
AS
SELECT *
FROM dbo.[600_All_Suggested_Data]
WHERE (Site = @site)
Print 'View for ' + @site + ' Created'

fetch next from c_site into @site
end
close c_site
deallocate c_site
return

end

May 12 '06 #1
2 12714
Stu
This is actually one of the few times that a cursor and dynamic SQL can
be useful; this administrative scripting is a great target for this
sort of stuff.

Anyway, you need to use dynamic SQL for this:

DECLARE @tSite TABLE (site varchar(5))
INSERT INTO @tSite
SELECT 'ABCDE'
UNION ALL
SELECT 'FGHIJ'

declare @site varchar(5)
DECLARE @SQL nvarchar(2000)

declare c_site cursor for
select site from @tsite
open c_site
fetch from c_site
into @site

while (@@fetch_status = 0)
begin

SET @SQL = 'CREATE VIEW Site_All_Data_' + @site + '
AS
SELECT *
FROM dbo.[600_All_Suggested_Data]
WHERE Site = ''' + @site + ''''

exec (@SQL)

Print 'View for ' + @site + ' Created'

fetch next from c_site into @site
end
close c_site
deallocate c_site
HTH,
Stu

May 12 '06 #2
Worked like a charm!

Thanks for helping a developer that forgets the 'simple' stuff
sometimes.

db55

May 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sci | last post by:
Could someone help me by answering the questions below? What's a cursor? What's difference between Query and View? Is a RecordSet just part of a table? Can it be part of a query of view? If...
4
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure...
1
by: KemperR | last post by:
Dear All, I want to achieve that the combination of values over multiple columns in a table are unique. I know this works for SQL Server, but how do I declare this in an ACCESS 2000 data base...
0
by: Muhammad Aftab Alam | last post by:
Hi All, I am facing a problem during changing the cursor on the mouse move event of a List View control. the event is captured but when I try to change the cursor as with the following code...
7
by: alessandro menchini | last post by:
Hello, First of all: i apologize for my english...i'm still learning... If i create a view it seems that DB2 manage this view like an updateable view. But I need to create a view "read only",...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
1
by: clir | last post by:
Hi, I'm trying to create views on all my existing tables and for that I'd like to create a script or so. I don't want to specify the '*' for the columns in the create view statement. I prefer...
3
by: mckbill | last post by:
Is there a way I can direct the cursor to a specific field (variable) in a form by typing the field name while in form view? I have a form with many fields, and it would be nice if there were...
2
by: Mohit | last post by:
Hi all, I am working on a windows application with a list view on a form. Now I wanted to show hand cursor when mouse is over list view item and default(arrow) cursor at other places. List...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.