Can we define a variable of type array in SQL Server?
I have a table and this pk type is datetime
.
CREATE TABLE [dbo].[tblTest]
(
[TestDate] [datetime] NOT NULL, -- this column is the primary key
[UserId] [int] NOT NULL,
[RotationType] [int] NOT NULL
)
And I need a query to update userId
UPDATE TABLE dbo.tblTest
SET RotationType = 1
WHERE TestDate IN (@testDates)
How can I declare this @testDates
variable?
marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Mar 31, 2021 at 15:55
1 Answer 1
you can use table variables
:
declare @tblvar as table (TestDate datetime)
insert into @tblvar values (('2021-09-01'),...);
UPDATE TABLE dbo.tblTest
SET RotationType = 1
WHERE TestDate IN (select TestDate from @tblvar)
or you can declare it as type so you can pass it to procs:
declaration phase :
create type DateTimeArray as table (TestDate datetime);
CREATE PROCEDURE funcname (@inputDates DateTimeArray)
as
UPDATE TABLE dbo.tblTest
SET RotationType = 1
WHERE TestDate IN (select TestDate from @inputDates)
end
calling:
declare @tblvar as DateTimeArray;
insert into @tblvar values (('2021-09-01'),...);
exec funcname @tblvar
answered Mar 31, 2021 at 16:06
lang-sql
TABLE
in theUPDATE
statement - just useUPDATE dbo.tblTest SET ....