My question: Is it possible in SQL Server 2008 to define temporal table in stored procedure and pass it as variable to user defined function?
I need to do the following:
-
I have procedure
dbo.GetMsgCntData. -
This procedure is making some calculations and as a result of this procedure I have data into temporary table that is defined in this procedure (
GetMsgCntData):-- table defined in dbo.GetMsgCntData DECLARE @tmpTable TABLE ( ID BIGINT, FirstName VARCHAR(50), LastName VARCHAR(50), UserType varchar(50), SenderID bigint, IsArchive bit ) -
So,
@tmpTablecontains some data.
I need to run user defined functionGetCnt(@status, @MsgTempTable), but I needgetCntfunction to access@tmpTabledata. Basically I need something like this:-- to be written at the end of dbo.GetMsgCntData SELECT cnt(*) total, dbo.GetCnt('open', @tmpTable) as opened, dbo.GetCnt('closed', @tmpTable) as closed FROM @tmpTable -- where @tmpTable is temporal table -
I tried to define
@tableinGetCntas user defined type.CREATE TYPE dbo.tmp_Messages AS TABLE ( ID BIGINT, FirstName VARCHAR(50), LastName VARCHAR(50), UserType varchar(50), SenderID bigint, IsArchive bit ) CREATE FUNCTION FUNCTION [dbo].[GetCnt] ( @Status nvarchar(10), @MsgTempTable dbo.tmp_Messages READONLY ) ....
but this gives me error message:
Operand type clash: table is incompatible with tmp_Messages
I think that my idea is simply incorrect.
Temporary table is not compatible with user-defined type.
You should declare your temporary table as user-defined type:
Please read a good article about table-valued parameters: http://beyondrelational.com