all groups > sql server programming > august 2007 >
You're in the

sql server programming

group:

Refresh Views


Refresh Views JCP
8/30/2007 11:48:02 PM
sql server programming:
I'm using the store procedure below to drop and recreate all my views.
It works fine if the string is <=4000 char.
I tried to change data type for @text parameter from nvarchar(4000) to
varchar(8000) but the error still occur.

Question.
How can I do to refresh (drop and re-recreate) all views including with
strings upper 4000 characters?
What is wrong in my store procedure?

Procedures
-------------------------------------------------------
EXEC "T1_RefreshViews"
-------------------------------------------------------
ALTER PROCEDURE T1_RefreshViews
AS

SET NOCOUNT ON

DECLARE @name nvarchar(128)
DECLARE @text nvarchar(4000)

SELECT dbo.sysobjects.name, dbo.syscomments.text
INTO dbo.[#TempObjects]
FROM dbo.sysobjects INNER JOIN
dbo.syscomments ON dbo.sysobjects.id =
dbo.syscomments.id
WHERE (dbo.sysobjects.xtype = 'V') AND (dbo.sysobjects.category = 0)
ORDER BY dbo.sysobjects.name


DECLARE Object_Refresh CURSOR FOR SELECT name,text FROM #TempObjects

OPEN Object_Refresh

FETCH NEXT FROM Object_Refresh INTO @name,@text
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('DROP VIEW ' + '"' + @name + '"')
EXEC (@text)
PRINT ('Droped and re-created ' + @name)
FETCH NEXT FROM Object_Refresh INTO @name,@text
END

CLOSE Object_Refresh
DEALLOCATE Object_Refresh

DROP TABLE #TempObjects
-------------------------------------------------------------------------

Thanks for any help

jcp

--
Re: Refresh Views Erland Sommarskog
8/31/2007 12:00:00 AM
JCP (JCP@discussions.microsoft.com) writes:
[quoted text, click to view]

The problem is that if the definition is more than 4000 characters, it
is split over several rows in syscomments.

But the real question, why do you do this at all? What's wrong with
sp_refreshview?


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Refresh Views Tibor Karaszi
8/31/2007 12:00:00 AM
You want to see the source code for sp_helptext? If so, execute:

USE master
GO
EXEC sp_helptext 'sp_helptext'

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


[quoted text, click to view]
Re: Refresh Views Erland Sommarskog
8/31/2007 12:00:00 AM
JCP (JCP@discussions.microsoft.com) writes:
[quoted text, click to view]

Or run sp_refreshview. Or stop using SELECT *, which does not belong in


[quoted text, click to view]

I have a more radical solution: put your source code under source control.
Or at least have it on disk. The database is not the place to have
source code, that's just a container for binaries. (Nevermind that the
binaries are very similar to the original.)

[quoted text, click to view]

Which is a lot easier to do if you read from disk.

As I said in my initial post, you run into problem when the view definition
is over more than 4000 characters, because it extends over several rows in
syscomments. There is not really any way in SQL 2000 to extract that
definition and reexecute it using T-SQL. In SQL 2005 it is possible, thanks
to the new MAX data types. However, it's still much better to store your
source on disk or in a version-control system.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Refresh Views JCP
8/31/2007 2:02:01 AM
1. Because if I do modifications in a table, the columns views doesn't match
to columns tables.
If I drop and re-creat with the SP resolve this issue.

2. With this procedure I can store the views in a table (not temp) If I do a
mistake or if I'm testing, from table views I can put the original views back

3. I I want encription, from the table where I keep de views, I can drop the
existing views and create new encrypted views.

Probably, already exist a sp to do this, but I don't know.
Do you know any solution to resolve my questions?

I would like to do the same for functions and SP
Thanks



--
Jose


[quoted text, click to view]
Re: Refresh Views Shiju Samuel
8/31/2007 3:10:48 AM
[quoted text, click to view]
strings upper 4000 characters?

Your code assumes that syscomments have a single column for each
objects.
The syscomments text column is nvarchar(4000) and when the text for of
a objects goes over 4000 it is put on an another row by incrementing
colid.

If you do a sp_helptext of sp_helptext you get a code to handle this.

Hope this will help

Thanks
Shiju Samuel
Re: Refresh Views JCP
8/31/2007 3:40:01 AM
From query analyzer I run exec "sp_helptext" and give the following error:

Server: Msg 201, Level 16, State 4, Procedure sp_helptext, Line 0
Procedure 'sp_helptext' expects parameter '@objname', which was not supplied.

Where can I find this SP? I would like to see the script

Thanks
jcp
--
Jose


[quoted text, click to view]
Re: Refresh Views ML
8/31/2007 3:54:01 AM
The sp_helptext system procedure is located in the master database. You can
find more information on this and other system procedures in Books Online.


ML

---
Matija Lah, SQL Server MVP
Re: Refresh Views ML
8/31/2007 4:14:01 AM
LOL! That was too obvious, even for me. :)


ML

---
Matija Lah, SQL Server MVP
Re: Refresh Views Alex Kuznetsov
8/31/2007 6:31:18 AM
[quoted text, click to view]

Jose,

Let me repeat Erland's question: What's wrong with
sp_refreshview? Did you try it?

Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/
AddThis Social Bookmark Button