sql-server - 使用表值参数插入表时遇到问题
问题描述
我创建了一个表类型:
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
我究竟做错了什么?
解决方案
您不能在插入中混合使用标量值和选择语句。您需要将标量值改为选择中的列。像这样。
CREATE PROCEDURE test(
@value INT,
@array int_array readonly
)
AS
BEGIN
INSERT INTO myTable
(
Id_SecondTable
, Id_ThirdTable
)
SELECT @value
, n
FROM @array
END
推荐阅读
- python - 在 Python 中求解具有线性不等式约束的最小二乘
- javascript - 如何防止函数使用旧的函数参数
- tensorflow - 使用 Imagedatagenerator 时的多输出模型
- javascript - 在移动导航栏中使用 flex 时如何创建边界线?
- c++ - 优化 Windows 上的 memcpy (MSVC++)
- javascript - 给定一个数组和一个值,删除该值的所有实例并返回新的长度
- python - 熊猫df不识别列的长度
- javascript - 复选框:复选框全选并取消选择不影响所有
- electron - Electron - 使用 Net 发出请求时抛出 ERR_FAILED
- bash - bash 中的 xxd:删除行号和 ascii