Visual Studio 2022 crashes with specific syntax in stored procedure

Mark Hellegers 46 Reputation points
2024-01-15T13:34:55.3233333+00:00

Loading the below stored procedure in an SSDT project in Visual Studio 2022 crashes Visual Studio. The crash does not happen in Visual Studio 2019. The problem is the "create index" line. Commenting it out, stops Visual Studio from crashing. Where do I need to report this, so Microsoft can have a look at it?

create procedure dbo.TableVariableIndexTest
as
begin
  set nocount, xact_abort on

  declare @TableVariableTest table
  (
    TestInt int,
    TestDate date
  );

  create index [IX_TableVariableTest] on [@TableVariableTest] (TestInt, TestDate);
end;

Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.8K Reputation points MVP Volunteer Moderator
    2024-01-15T22:04:11.73+00:00

    You can report bugs on https://feedback.azure.com/forums/908035-sql-server. At least for SQL Server. I am not sure about Visual Studio. In any case, your procedure is not correct. SQL Server agrees to create it, but when I run it, I get this error:

    Msg 1088, Level 16, State 12, Procedure TableVariableIndexTest, Line 12 [Batch Start Line 0]Cannot find the object "@TableVariableTest" because it does not exist or you do not have permissions.

    Since you but the name in brackets, it is no longer a reference to a variable, but to a permanent table. You can create indexes on table variables once they have been declared. But you can define indexes inline. Here is a corrected version of your procedure:

    create or alter procedure dbo.TableVariableIndexTest
    as
    begin
      set nocount, xact_abort on
    
      declare @TableVariableTest table
      (
        TestInt int,
        TestDate date, 
        INDEX [IX_TableVariableTest] (TestInt, TestDate)
      );
    
    end;
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.