我创建了一个表格类型:
CREATE TYPE int_array AS TABLE (n INT NOT NULL)
,并希望使用它在单个过程中将几行插入下表:
CREATE TABLE myTable
(
Id_SecondTable INT NOT NULL,
Id_ThirdTable INT NOT NULL,
CONSTRAINT PK_myTable
PRIMARY KEY (Id_SecondTable, Id_ThirdTable),
CONSTRAINT FK_Id_SecondTable
FOREIGN KEY (Id_SecondTable) REFERENCES SecondTable (Id),
CONSTRAINT FK_Id_ThirdTable
FOREIGN KEY (Id_ThirdTable) REFERENCES ThirdTable (Id)
)
我的程序如下:
CREATE PROCEDURE test
(@value INT,
@array int_array READONLY)
AS
BEGIN
INSERT INTO myTable
VALUES (Id_SecondTable, Id_ThirdTable) (@value, SELECT n FROM @array)
END
我在做什么错了?
答案 0 :(得分:2)
您不能在您的插入中混合标量值和select语句。您需要将标量值设置为select中的一列。这样。
CREATE PROCEDURE test(
@value INT,
@array int_array readonly
)
AS
BEGIN
INSERT INTO myTable
(
Id_SecondTable
, Id_ThirdTable
)
SELECT @value
, n
FROM @array
END