Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create stored procedure with table output parameter

I have this code:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetProfitDeals]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetProfitDealsVar2]
GO

IF EXISTS(SELECT 1 FROM sys.types WHERE name = 'TableOrderType' AND is_table_type = 1 AND SCHEMA_ID('dbo') = schema_id)
DROP TYPE [dbo].[TableOrderType];

CREATE TYPE TableOrderType AS TABLE(
    Order_ID int NOT NULL,
    Order_AccNumber int NOT NULL,
    Order_OpenDate datetime NULL,
    Order_CloseDate datetime NULL,
    Order_Profit float NULL
);
GO

CREATE PROCEDURE [dbo].[GetProfitDeals](@OpenDate datetime = NULL, @CloseDate datetime  = NULL, @MinProfit float = NULL, @out TableOrderType OUTPUT READONLY)
AS
    INSERT INTO @out
    SELECT * FROM [Orders]
    WHEN [Orders].[OpenDate] >= @OpenDate
GO

But I get the error "Incorrrect syntax about construction 'READONLY'". How I can fix this, because me I really need a OUTPUT table parameter.

like image 253
Relrin Avatar asked Oct 09 '13 11:10

Relrin


People also ask

Can a stored procedure use output parameters?

The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

Can we pass table as parameter in stored procedure?

Table-Valued Parameters aka TVPs are commonly used to pass a table as a parameter into stored procedures or functions. They are helpful in a way, we can use a table as an input to these routines and we can get rid of dealing more complex steps to achieve this process.

How run SP with output parameter in SQL?

The easy way is to right-click on the procedure in Sql Server Management Studio (SSMS), select 'Execute stored procedure..." and add values for the input parameters as prompted. SSMS will then generate the code to run the procedure in a new query window, and execute it for you.

What is output parameter in stored procedure?

Output parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module.


2 Answers

Table parameters are readonly. You cannot select into them. Use Table-Valued Parameters:

Table-valued parameters must be passed as input READONLY parameters to Transact-SQL routines. You cannot perform DML operations such as UPDATE, DELETE, or INSERT on a table-valued parameter in the body of a routine.

And Table-Valued Parameters:

You cannot return data in a table-valued parameter. Table-valued parameters are input-only; the OUTPUT keyword is not supported.

Read Arrays and Lists in SQL Server for a comprehensive discussion on alternatives.

like image 159
Remus Rusanu Avatar answered Sep 22 '22 04:09

Remus Rusanu


As Remus states, you can't do that exactly but you could accomplish what you want to achieve by using a User-Defined Function instead: tsql returning a table from a function or store procedure

Which will return your data in a table however I believe you will need to define the table in the Function and not define it as a type

like image 38
Rodders Avatar answered Sep 20 '22 04:09

Rodders