January 17, 2016 at 12:01 am
Hi all
How can I recursive check sp_refreshsqlmodule in sql server?
Sample:
1- I create one table:
CREATE TABLE [dbo].[L1](
[ID] [int] NULL,
[Name] [nchar](10) NULL,
[Family] [nchar](10) NULL
) ON [PRIMARY]
2-I create first view:
CREATE VIEW [dbo].[L2]
AS
SELECT Name, Family, ID
FROM dbo.L1
3-I create second View use with first view
create view [dbo].[L3]
as
select Name + ' ' + Family Fullname
from [dbo].[L2]
GO
4-Then I change column name in table L1
EXECUTE sp_rename N'dbo.L1.Name', N'Name1', 'COLUMN'
5-I check first view: The result is pleasant because I change column name
EXEC sp_refreshsqlmodule '[dbo].[L2]'
--Msg 207, Level 16, State 1, Procedure sp_refreshsqlmodule_internal, Line 71
--Invalid column name 'Name'.
6-But when I check second view: It is not pleasant
EXEC sp_refreshsqlmodule '[dbo].[L3]'
--Command(s) completed successfully.
January 17, 2016 at 6:33 am
I have a MUCH better idea: Use WITH SCHEMABINDING on EVERY object you ever create in SQL Server where it is allowed. I hope you will find that this will prevent you from doing the BAD developer mistake you are doing, which is not doing a complete impact analysis when you change a column name.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
January 17, 2016 at 9:44 pm
Change the column name to a column name present in one of the tables referenced in the query.
January 20, 2016 at 2:30 pm
How would you know in the real world that [dbo].[L2] was the first view in the chain, where you started the refresh? Certainly things are not named this way in production environments. Renaming columns ideally requires a meeting of the mind of the team to determine impact and really if it is something worth doing in the end.
----------------------------------------------------
January 20, 2016 at 2:35 pm
Another thing to consider is avoiding the chaining of views if possible. You keep the compiler from getting a good query plan. Things are not as re-usable in the SQL world as they would be in the world of procedural programming. When all views have one degree of separation from the source you can see them more readily
https://msdn.microsoft.com/en-us/library/ms190624(v=sql.120).aspx
----------------------------------------------------
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply