George
George

Reputation: 21

sql stored procedure that selects from array/list/xml input parameter

I’m looking for idea’s/solution to the following problem.

Require a stored procedure that accepts a single parameter (array, list, collection, xml, etc…). For each item in the array/list/collection/xml it will execute a query like this:

Select * from <some table> t
Where t.Name = Parameter.Name and t.datetime = Parameter.datetime

The results would all be returned as a single result set.

As an example this procedure would be called with the following details passed in as a parameter value:

Books 09/09/2009 11:23:23

Books 09/09/2009 11:23:23 Authors 04:22:04

Books 09/09/2009 11:23:23 Authors 04:22:04 Catalog 9:45:11

Looking for a variety of ideas to perform some stress/timings on it.

Thanks

Upvotes: 0

Views: 1641

Answers (1)

Mahesh Kumar
Mahesh Kumar

Reputation: 9

CREATE TYPE id_list AS TABLE (
    id int NOT NULL PRIMARY KEY
);
GO

CREATE PROCEDURE [dbo].[tvp_test] (
      @param1           INT
    , @customer_list    id_list READONLY
)
AS
BEGIN
    SELECT @param1 AS param1;

    -- join, filter, do whatever you want with this table 
    -- (other than modify it)
    SELECT *
    FROM @customer_list;
END;
GO

DECLARE @customer_list id_list;

INSERT INTO @customer_list (
    id
)
VALUES (1), (2), (3), (4), (5), (6), (7);

EXECUTE [dbo].[tvp_test]
      @param1 = 5
    , @customer_list = @customer_list
;
GO

DROP PROCEDURE dbo.tvp_test;
DROP TYPE id_list;
GO

Upvotes: 1

Related Questions