1

I'm trying to create a table type within a stored procedure...my attempt does not compile:

CREATE PROCEDURE dbo.StoredProcedure1
    
AS
        
CREATE TYPE DailyPricingAndVolBySymbolType AS TABLE (TradeDate date, 
Symbol varchar(120), O smallmoney, H smallmoney, L smallmoney, C smallmoney, 
Vol big int)
    
GO

I just want to create the type, once, to be used in other stored procedures, I'm not looking for a "reuseable" sproc.

7
  • This question is wrong on a couple of levels - perhaps explain the overarching problem to be solved? If you're familiar with traditional programming languages, it's like asking why you can't define a new type within a method. Feb 18, 2012 at 17:48
  • @Damien_The_Unbeliever Thanks... I was wondering if I even knew enough to clearly ask my question. :) Editing post.. Feb 18, 2012 at 17:52
  • Based on edit - then why create it within a stored procedure? Why isn't plain CREATE TYPE... the right thing to do? Feb 18, 2012 at 17:56
  • @Damien_The_Unbeliever You mean within a script then? Not sure how to do that in the visual studio ide. Feb 18, 2012 at 18:00
  • 1
    Other than the fact that a) You can't seem to make it work, and b) SPs are expected to be executed multiple times... Feb 18, 2012 at 18:16

4 Answers 4

7

Create the type outside the procedure - it only needs to be created once, and used anytime thereafter:

CREATE TYPE dbo.DailyPricingAndVolBySymbolType AS TABLE (TradeDate date, 
Symbol varchar(120), O smallmoney, H smallmoney, L smallmoney, C smallmoney, 
Vol bigint)
GO

CREATE PROCEDURE dbo.StoredProcedure1
AS
declare @DailyPricingAndVolBySymbolType DailyPricingAndVolBySymbolType;
...
3

You can easily create a table variable within a stored proc:

CREATE PROCEDURE dbo.StoredProcedure1

AS

DECLARE @DailyPricingAndVolBySymbolType TABLE (TradeDate date, 
Symbol varchar(120), O smallmoney, H smallmoney, L smallmoney, C smallmoney, 
Vol bigint)

Or create a temp table:

CREATE PROCEDURE dbo.StoredProcedure1

AS

CREATE TABLE #DailyPricingAndVolBySymbolType (TradeDate date, 
Symbol varchar(120), O smallmoney, H smallmoney, L smallmoney, C smallmoney, 
Vol bigint)

But creating a table type within the procedure makes no real sense, so far as I can see. If you were to create a new type within the procedure, nothing external could know of that type.


Based on your edits:

If it were possible to define a table type within a stored procedure, then based on other SQL Server behaviors, I wouldn't expect that same type to be available a) to other contexts, and b) after the stored proc's execution ends. That would be consistent with e.g. temp tables, and hence why I don't think it would be useful.

5
  • So I can't create a table type, to use in other stored procedures, pass to udfs, etc? Feb 18, 2012 at 17:58
  • If you want to create a table type, then do so - trying to embed the creation of that type seems to be the general fault - and I don't see how other procs, etc could be defined. Feb 18, 2012 at 18:22
  • I don't think that's the case... you're describing a situation in which the type would be of very limited scope.. creating a type creates a database object. When you need to use it you create an instance of it in any procedure you want. Feb 18, 2012 at 21:45
  • Yes, so again, why put the CREATE TYPE statement inside a procedure? That implies that the procedure would only ever be called once. CREATE TYPE and CREATE PROCEDURE are at the same level and should not be nested in most circumstances (unless you are trying to create a type to use temporarily, which is not what you seem to be trying to do, and for which there is probably a better solution anyway). Feb 19, 2012 at 0:48
  • @AaronBertrand I see that it seems unusual, to be clear I do want to run it just once. I've been working exclusively in Visual Studio, and if someone knows of a way to run a script from that environment I'd be happy to create it that way. Feb 19, 2012 at 15:03
1

You're probably looking for a table variable:

declare @DailyPricingAndVolBySymbolType TABLE (...

If you're really after a table type, omit the space in big int. Your procedure will run only once, since you can't create a type if it already exists.

1
  • Thanks... that is just what I'm trying to do, just create the type. Fixing that space didn't help matters though. Feb 18, 2012 at 17:55
1

Use this to create "create type table". simple example for user

CREATE TYPE unit_list AS TABLE (
    ItemUnitId int,
    Amount float,
    IsPrimaryUnit bit
);

GO
 CREATE TYPE specification_list AS TABLE (
     ItemSpecificationMasterId int,
    ItemSpecificationMasterValue varchar(255)
);

GO
 declare @units unit_list;
 insert into @units (ItemUnitId, Amount, IsPrimaryUnit) 
  values(12,10.50, false), 120,100.50, false), (1200,500.50, true);

 declare @spec specification_list;
  insert into @spec (ItemSpecificationMasterId,temSpecificationMasterValue) 
   values (12,'test'), (124,'testing value');

 exec sp_add_item "mytests", false, @units, @spec


//Procedure definition
CREATE PROCEDURE sp_add_item
(   
    @Name nvarchar(50),
    @IsProduct bit=false,
    @UnitsArray unit_list READONLY,
    @SpecificationsArray specification_list READONLY
)
AS


BEGIN
    SET NOCOUNT OFF     

    print @Name;
    print @IsProduct;       
    select * from @UnitsArray;
    select * from @SpecificationsArray;
END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.