Friday, 23 August 2013

SQL executing stored procedure in INSERT statement

SQL executing stored procedure in INSERT statement

I'm strugling with this task: I need a stored procedure that takes a table
parameter of type DeviceKeywordType and two DATETIME parameters and
returns table of type DeviceKeywordCountType.
DeviceKeywordType declaration:
CREATE TYPE DeviceKeywordType
AS TABLE
(
DeviceID INT,
KeywordID INT
)
DeviceKeywordCountType declaration:
CREATE TYPE DeviceKeywordCountType
AS TABLE
(
DeviceID INT,
KeywordID INT,
[Count] INT
)
The stored procedure so far:
CREATE PROCEDURE GetKeywordsDevicesTable @Input DeviceKeywordType
READONLY, @Start DATETIME = '1900-01-01', @End DATETIME = '9000-12-30'
AS
BEGIN
SET NOCOUNT ON
DECLARE @Ret DeviceKeywordCountType
INSERT INTO @Ret (DeviceID, KeywordID, [Count])
SELECT DeviceID, KeywordID, (EXEC Keyword_GetCountForDeviceByID
DeviceID, KeywordID, @Start, @End)
FROM @Input
SELECT * FROM @Ret
END
The point is that I need to take all the pairs from the input table and
calculate something with the Keyword_GetCountForDeviceByID procedure and
save it to the output table. I konw that the SELECT statement is
syntactically wrong but I don't know what is the correct way. Can this be
done like that or is my approach wrong?

No comments:

Post a Comment