here is an interesting problem in Sql Server; I have a stored procedure, that returns the following error when executed:
The type ‘MyTable’ already exists, or you do not have permission to
create it.
I think the type MyTable wasn’t properly created, and I base this guess upon the fact other types (nvarchar, for example) are all coloured in blue, whereas when I hover the mouse above @T declaration (DECLARE @T AS MyTable), it says @T has an invalid data type.
Here is a version of the sproc :
CREATE PROCEDURE SPXXX
AS
BEGIN
DECLARE @calc numeric(18,2)
DECLARE @sql nvarchar(1500), @column_name nvarchar(50), @table_name nvarchar(50), @prmDef nvarchar(1500)
DECLARE @v1 nvarchar(50), @v2 nvarchar(50)
DECLARE @T AS MyTable
Set @column_name = 'EID'
Set @table_name = 'CTRY_S'
Set @sql = N'Select @calcOUT = SUM(datalength(@v1))/2 from @T';
Set @prmDef = '@v1 nvarchar(50), @calcOUT numeric(18,2) OUTPUT';
EXECUTE sp_executesql @sql, @prmDef, @v1=@column_name, @T=@table_name, @calcOUT=@calc OUTPUT;
END
GO
You have a bunch of syntax errors in the example code. I am assuming that happened when you shortened the example, so I am not going to talk about them.
Your main problem is, that the
CREATE TYPE MyTable TABLE(..)command creates an object in the database. This type is not local to the procedure and stays in the database after the procedure finishes. The next time the procedure executes, you will get the error you have seen because the type indeed already exists.Just remove the create type statement and you should be fine.
As for intellisense, you can never really trust it. Only execution tells if your statement is correct or not. You can use alternatives that are sold by other companies. They tend to be better, but non that I have come across is perfect.