开发者

Visual Studio Database Project - Unresolved Reference to temp table

I have imported my sql server 2005 database into a VS2010 database project. One of my stored procedures contains a statement similar to

开发者_运维问答

INSERT INTO #myTemp...

and Visual Studio gives me a warning such as

SQL04151: Procedure: [dbo].[mySproc] has an unresolved reference to object [#myTemp].

Is there a way of resolving this reference? I'd like to clear as many of the project warnings as possible.


I had the same thing, where the parent creates it. Instead of getting rid of the warning by creating the table if it doesn't exist, I want to be able to throw an exception if it doesn't. Putting a CREATE statement after the return statement guarantees that it will never get ran but also clears up the warning.

IF (OBJECT_ID('tempdb..#Foo') is null)
BEGIN
    Raiserror('#Foo doesn''t exist.', 16, 1)
    RETURN
    CREATE TABLE #Foo (foo int) --Here just to get rid of compile warning
END


Did you try three part naming as in here (VS 2010 build database project receive SQL04151)

Also you might need to change the db ref to tempdb instead of master. See this article (http://blogs.msdn.com/b/gertd/archive/2009/06/10/system-objects-in-tempdb.aspx). It describes sys objects but temporary tables are stored in tempdb so would exhibit the same behavior.


If you know that there's no problem with your code, you can suppress the build warnings. Go to your project's settings and you'll find a tab called Build. There's a field there to add the codes you wish to suppress. I googled and found this page, describing the steps.


Obviously, VS does not know that this stored proc is only to be executed from within another stored proc which creates this temp table. One solution would be to add code to the procedure that creates the temp table if necessary

If object_id('tempdb..#MyTemp') Is Null
    Create Table #MyTemp
        (
        ...
        )

If always called from the parent, in theory the above code should never run.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜