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

sql server programming

group:

How to remove Deffered Name Resolution


Re: How to remove Deffered Name Resolution Geoff Schaller
10/27/2007 12:00:00 AM
sql server programming:
Sree.

But this is both acceptable and desirable. We need to have procedures
which may or may not refer to current tables or objects - it is only at
runtime that their existence matters.

But what do you mean by "it".

If you are referring to Management Studio then it does warn you of the
illegal references when you test drive it. It wouldn't make sense to
have such testing at design time or you couldn't write many procedures
:-). If you mean at runtime then this is a different matter and quite
easy to overcome. You can test for the existence of the object before
you run the query using it and either bypass the query or execute a
rollback etc.

IF EXISTS(SELECT NAME FROM SYSOBJECTS WHERE XTYPE = 'D' AND NAME =
'DF_EmptyString') etc

HTH

Geoff


[quoted text, click to view]
Re: How to remove Deffered Name Resolution Erland Sommarskog
10/27/2007 12:00:00 AM
Sreek (Sreek@discussions.microsoft.com) writes:
[quoted text, click to view]

There is actually one solution:

sp_dbcmptlevel db, 65

However, this also makes a lot of good features unavailable, so it is not
really a serious option.

In our shop we use our own load tool that performs this checking for us.
The behaviour of SQL Server is entirely unacceptable for enterprise
development, and I very strongly disagree with the gentleman who claimed
this behaviour to be desirable.

Here are two items on Connect that you can vote on:

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=260762

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=127152


--
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: How to remove Deffered Name Resolution Dan Guzman
10/27/2007 12:00:00 AM
Even with deferred name resolution, you can at least perform cursory
validation of stored procs with SET FMTONLY ON:

SET FMTONLY ON
GO

EXEC dbo.usp_MyProcedure
@MyParameter=NULL
GO

SET FMTONLY OFF
GO

This method is not as thorough as actually executing procs but it will at
least catch DNR issues, barring dynamic SQL. The script below will generate
a script to exercise all procs, views ans functions with SET FMTONLY ON:

SET NOCOUNT ON
--procedures and functions
SELECT
CASE r.ROUTINE_TYPE
WHEN 'PROCEDURE' THEN 'Procedure'
WHEN 'FUNCTION' THEN
CASE
WHEN
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
N'.' +
QUOTENAME(ROUTINE_NAME)), 'IsTableFunction') = 1
THEN 'TableFunction'
WHEN
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
N'.' +
QUOTENAME(ROUTINE_NAME)), 'IsScalarFunction') = 1
THEN 'ScalarFunction'
WHEN
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
N'.' +
QUOTENAME(ROUTINE_NAME)), 'IsInlineFunction') = 1
THEN 'InlineFunction'
END
END AS ObjectType,
QUOTENAME(ROUTINE_SCHEMA) +
N'.' +
QUOTENAME(ROUTINE_NAME) AS ObjectName,
REPLICATE(N'NULL,',
ISNULL((SELECT COUNT(*) AS Parameters
FROM INFORMATION_SCHEMA.PARAMETERS p
WHERE
p.IS_RESULT = 'NO' AND
p.SPECIFIC_SCHEMA = r.ROUTINE_SCHEMA AND
p.SPECIFIC_NAME = r.ROUTINE_NAME), 0)) AS Parameters
INTO #Objects
FROM INFORMATION_SCHEMA.ROUTINES r
WHERE
ROUTINE_TYPE IN ('PROCEDURE', 'FUNCTION') AND
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
N'.' +
QUOTENAME(ROUTINE_NAME)), 'IsMSShipped') = 0
UNION ALL
--views
SELECT
'View' AS ObjectType,
QUOTENAME(TABLE_SCHEMA) +
N'.' +
QUOTENAME(TABLE_NAME) AS ObjectName,
'' AS Parameters
FROM INFORMATION_SCHEMA.TABLES t
WHERE
TABLE_TYPE = 'VIEW' AND
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
N'.' +
QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0

--remove trailing comma from parameter list
UPDATE #Objects
SET Parameters = LEFT(Parameters, LEN(Parameters) - 1)
WHERE RIGHT(Parameters, 1) = N','

--generate invocation scripts
SELECT
CASE ObjectType
WHEN 'View' THEN 'SELECT * FROM '
WHEN 'Procedure' THEN 'EXEC '
WHEN 'ScalarFunction' THEN 'SELECT '
WHEN 'InlineFunction' THEN 'SELECT * FROM '
WHEN 'TableFunction' THEN 'SELECT * FROM '
END +
RTRIM(ObjectName) +
CASE ObjectType
WHEN 'View' THEN ''
WHEN 'Procedure' THEN ' '
WHEN 'ScalarFunction' THEN '('
WHEN 'InlineFunction' THEN '('
WHEN 'TableFunction' THEN '('
END +
Parameters +
CASE ObjectType
WHEN 'View' THEN ''
WHEN 'Procedure' THEN ''
WHEN 'ScalarFunction' THEN ')'
WHEN 'InlineFunction' THEN ')'
WHEN 'TableFunction' THEN ')'
END
AS InvocationScript
INTO #InvocationScripts
FROM #Objects
ORDER BY ObjectName

DECLARE InvocationScripts CURSOR
LOCAL FAST_FORWARD READ_ONLY FOR
SELECT InvocationScript
FROM #InvocationScripts
DECLARE @InvocationScript nvarchar(4000)
OPEN InvocationScripts
WHILE 1 = 1
BEGIN
FETCH NEXT FROM InvocationScripts
INTO @InvocationScript
IF @@FETCH_STATUS = -1 BREAK
SET @InvocationScript = 'RAISERROR (' +
QUOTENAME('Testing: ' + @InvocationScript, '''')
+ ', 0, 1) WITH NOWAIT
GO
SET FMTONLY ON
GO
' + @InvocationScript + '
GO
SET FMTONLY OFF
GO'
PRINT @InvocationScript
END
CLOSE InvocationScripts
DEALLOCATE InvocationScripts

DROP TABLE #Objects, #InvocationScripts
GO


--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
How to remove Deffered Name Resolution Sreek
10/27/2007 3:56:00 AM
Hi,
When i try to create/alter a procedure, even if i use a table which does not
exist, it creates/alters the procedure. I want it to show an error if i am
using any table which does not exist. how is this possible?
Please help me.
Thanks in advance

Rgds
Re: How to remove Deffered Name Resolution Alex Kuznetsov
10/27/2007 12:42:34 PM
On Oct 27, 7:14 am, "Geoff Schaller"
[quoted text, click to view]

to me it is UNdesirable. I'd rather be warned that I have a typo in my
table name as soon as possible.
Re: How to remove Deffered Name Resolution David Portas
10/27/2007 4:00:07 PM
[quoted text, click to view]


You should see warning messages when you create the proc.
You should see error messages when you unit test the proc.

--
David Portas

AddThis Social Bookmark Button