我试图通过我的存储过程插入数据并发现难以完成。
我尝试过以下代码,但它不起作用。
DECLARE @AddressRecordsToPurge TABLE
(
RowID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
GUIDValue Nvarchar(max) NOT NULL,
GuidColumn Nvarchar(max) NOT NULL,
GuidTable Nvarchar(max) NOT NULL
)
Insert Into @AddressRecordsToPurge values ( Select AccountGUID FROM APPLICATIONCONTRACT WHERE ApplicationNumber =@ApplicationNumber AND AccountGUID IS NOT NULL, 'AccountGUID', 'PREVIOUSLENDERSREF');
我的第一个值是查询,最后两个值是字符串。请帮帮我
答案 0 :(得分:2)
我会把它写成:
Insert Into @AddressRecordsToPurge (GUIDValue, GuidColumn, GuidTable)
Select AccountGUID, 'AccountGUID', 'PREVIOUSLENDERSREF'
from APPLICATIONCONTRACT
where ApplicationNumber = @ApplicationNumber and
AccountGUID IS NOT NULL;
注意:
identity
列不在列表中,因此它将获得默认值。values
,只需insert . . . select
。至于你的代码,你需要记住子查询总是需要被它自己的括号括起来。
答案 1 :(得分:0)
别名字符串名称作为列名称。当您声明table variable
时,除非插入几列,否则不需要提及插入的列名。 Identity column
会自动填充。
DECLARE @AddressRecordsToPurge TABLE
(
RowID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
GUIDValue Nvarchar(max) NOT NULL,
GuidColumn Nvarchar(max) NOT NULL,
GuidTable Nvarchar(max) NOT NULL
)
Insert Into @AddressRecordsToPurge
SELECT
AccountGUID AS GUIDValue
,'AccountGUID' AS GuidColumn
,'PREVIOUSLENDERSREF' AS GuidTable
FROM APPLICATIONCONTRACT
WHERE ApplicationNumber =@ApplicationNumber AND AccountGUID IS NOT NULL
答案 2 :(得分:0)
尝试这种方式:
DECLARE @AddressRecordsToPurge TABLE
(
RowID INT NOT NULL
PRIMARY KEY
IDENTITY(1, 1) ,
GUIDValue NVARCHAR(MAX) NOT NULL ,
GuidColumn NVARCHAR(MAX) NOT NULL ,
GuidTable NVARCHAR(MAX) NOT NULL
)
INSERT INTO @AddressRecordsToPurge
( GUIDValue ,
GuidColumn ,
GuidTable
)
SELECT AccountGUID ,
'AccountGUID' ,
'PREVIOUSLENDERSREF'
FROM APPLICATIONCONTRACT AS artp
WHERE ApplicationNumber = @ApplicationNumber
AND AccountGUID IS NOT NULL