Deferred name resolution in stored procs and triggers but not views

Deferred name resolution in stored procs and triggers but not views

Post by Nayan Rava » Tue, 30 Nov 1999 04:00:00



After upsizing one of my SQL Server 6.5 systems to 7.0 I noticed that
the logs reported errors in upsizing my views. My views are of the form

CREATE VIEW dbo.vwAView AS
-- vwBView and vwCView are views
SELECT ColA,ColB FROM dbo.vwBView
UNION ALL
SELECT ColA,ColB FROM dbo.vwCView

The errors mentioned 'Invalid object name 'dbo.vwBView' and
'dbo.vwCView''. (Note, vwBView and vwCView come alphabetically after
vwAview and were upsized successfully). All views exist in my 6.5
database. My simple workaround was to script the views from 6.5 and
submit them to my 7.0 system after the upsizing.

This got me wondering. In 7.0 I can create stored procedures and
triggers referencing objects which don't yet exist (deferred name
resolution) but I can't use the same feature when creating views. Why
would this be so ? Is this by design ? If so, can anyone explain the
apparent inconsistency ?

Thanks,
--
Nayan Raval

 
 
 

Deferred name resolution in stored procs and triggers but not views

Post by BPMargoli » Tue, 30 Nov 1999 04:00:00


Nayan,

One man's opinion, not an official Microsoft position:

It is not uncommon for a stored procedure to create a temporary table and
then make use of it later in the same SP. With SQL Server 6.x, this would
cause problems since the query optimizer didn't see the temporary table and
so couldn't factor it into the compiled plan, and therefore would reject the
SP.

Tangent:
One might wonder why the query optimizer couldn't be built smarter to see
the CREATE TABLE or the SELECT INTO and make use of it. Two answers, but of
a similar nature:

First: either the CREATE TABLE or the SELECT INTO could be inside an IF /
ELSE statement, so the table could have different schema depending on the
values of the input parameters at run time.

Second: same essential logic as above but with the EXEC () statement and
again different schemas depending on the values of the input parameters at
run time.

Thus, programmers had to create the temp table outside of the SP just to get
the SP to compile. Microsoft resolved this problem via deferred resolution.

A view does not accept run time parameters, so the situation is not
comparable to that of SP's.


Quote:> After upsizing one of my SQL Server 6.5 systems to 7.0 I noticed that
> the logs reported errors in upsizing my views. My views are of the form

> CREATE VIEW dbo.vwAView AS
> -- vwBView and vwCView are views
> SELECT ColA,ColB FROM dbo.vwBView
> UNION ALL
> SELECT ColA,ColB FROM dbo.vwCView

> The errors mentioned 'Invalid object name 'dbo.vwBView' and
> 'dbo.vwCView''. (Note, vwBView and vwCView come alphabetically after
> vwAview and were upsized successfully). All views exist in my 6.5
> database. My simple workaround was to script the views from 6.5 and
> submit them to my 7.0 system after the upsizing.

> This got me wondering. In 7.0 I can create stored procedures and
> triggers referencing objects which don't yet exist (deferred name
> resolution) but I can't use the same feature when creating views. Why
> would this be so ? Is this by design ? If so, can anyone explain the
> apparent inconsistency ?

> Thanks,
> --
> Nayan Raval


 
 
 

Deferred name resolution in stored procs and triggers but not views

Post by Tibor Karasz » Thu, 02 Dec 1999 04:00:00


Nayan,

BP discussed why deferred name resolution might be desirable in sp's.
Here's my guess what it is not available in views:

A view is a relation. We expect to be able to derive meta-data out of
relation, i.e.: We expect that by using system tables, information_schema
views and other methods be able to derive column structure of a relation (a
view in this case). Such interrogation wouldn't be possible if deferred name
resolution was available for views.

--
Tibor Karaszi
MCDBA, MCSE, MCSD, MCT, SQL Server MVP
Cornerstone Sweden AB
Please reply to the newsgroup only, not by email.


Quote:> After upsizing one of my SQL Server 6.5 systems to 7.0 I noticed that
> the logs reported errors in upsizing my views. My views are of the form

> CREATE VIEW dbo.vwAView AS
> -- vwBView and vwCView are views
> SELECT ColA,ColB FROM dbo.vwBView
> UNION ALL
> SELECT ColA,ColB FROM dbo.vwCView

> The errors mentioned 'Invalid object name 'dbo.vwBView' and
> 'dbo.vwCView''. (Note, vwBView and vwCView come alphabetically after
> vwAview and were upsized successfully). All views exist in my 6.5
> database. My simple workaround was to script the views from 6.5 and
> submit them to my 7.0 system after the upsizing.

> This got me wondering. In 7.0 I can create stored procedures and
> triggers referencing objects which don't yet exist (deferred name
> resolution) but I can't use the same feature when creating views. Why
> would this be so ? Is this by design ? If so, can anyone explain the
> apparent inconsistency ?

> Thanks,
> --
> Nayan Raval

 
 
 

Deferred name resolution in stored procs and triggers but not views

Post by JRSte » Thu, 02 Dec 1999 04:00:00


On Mon, 29 Nov 1999 21:09:02 +0000, Nayan Raval


>This got me wondering. In 7.0 I can create stored procedures and
>triggers referencing objects which don't yet exist (deferred name
>resolution) but I can't use the same feature when creating views. Why
>would this be so ?

My guess, it was just a lower implementation priority than doing it
for stored procedures, where it is crucial, and it just didn't get
done.

Quote:> Is this by design ?

See above.

Quote:> If so, can anyone explain the apparent inconsistency ?

I'd be happy if sp_recompile that triggers recompilation for sp's
would also affect views.

Joshua Stern

 
 
 

Deferred name resolution in stored procs and triggers but not views

Post by Nayan Rava » Thu, 02 Dec 1999 04:00:00


Thanks, Tibor. Your explanation sounds good to me.

--
Nayan Raval



Quote:>Nayan,

>BP discussed why deferred name resolution might be desirable in sp's.
>Here's my guess what it is not available in views:

>A view is a relation. We expect to be able to derive meta-data out of
>relation, i.e.: We expect that by using system tables, information_schema
>views and other methods be able to derive column structure of a relation (a
>view in this case). Such interrogation wouldn't be possible if deferred name
>resolution was available for views.

>--
>Tibor Karaszi
>MCDBA, MCSE, MCSD, MCT, SQL Server MVP
>Cornerstone Sweden AB
>Please reply to the newsgroup only, not by email.

 
 
 

1. Deferred name resolution?

Please could somebody help me?

The SQL 7.0 help says that  In stored procedures, the syntax is parsed and
any non-existing objects are picked up only when the sp is executed.  (See
SQL 70 Books online, Deferred Name Resolution).

However, I find the script below won't compile, because the column name
'Officials' does not yet exist in the database.

If I add the column, compile and execute, then it doesn't add a new column
to the WSCases table (as I want it to).  So therefore, the update to the
table fails.

Please can somebody help?

Script below...

CREATE PROCEDURE spSIWSCases WITH RECOMPILE
 AS


-- FIRST DROP EXISTING TABLE
IF EXISTS(SELECT TABLE_NAME FROM Website.INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'WSCases')
 BEGIN
  DROP TABLE Website..WSCases
 END

-- CREATE A NEW REPLACEMENT TABLE
SELECT * INTO Website..WSCases FROM vwWSCases

--ADD A NEW COLUMN TO THE NEW TABLE (Officials)
ALTER TABLE Website..WSCases
ADD Officials text

PRINT 'Successfully deleted previous table, and added a new column to the
new table'

--UPDATE THE NEW TABLE Officials column with data from a nested stored proc


DECLARE curWSCO CURSOR
 FOR SELECT Caseid FROM CASEs
OPEN curWSCO


 BEGIN



 END
CLOSE curWSCO
DEALLOCATE curWSCO

2. backup logical-logs

3. Deferred Name Resolution and Compilation

4. Help! Stuck transaction log...

5. Deferred Name Resolution

6. Removable Media & MS SQL Server 6.5

7. Deferred name resolution

8. Data truncated - VC++ bug?

9. renaming database in views,triggers, and stored procs

10. Unable to Design tablles, view and stored procs etc using Data View

11. Views with dynamic table name resolution

12. If Update(@ColumnName) - deferred resolution?

13. VIEWS OR STORED PROCS?