我想根据条件将数据插入一列。
DECLARE @section NVARCHAR(40)
SET @section ='section1'
INSERT [table]
(
Class_Of_Business,
Financial_Status,
Source_Of_Funds,
Source_Of_Wealth,
SELECT CASE
WHEN @Section = 'section1' THEN 'Section_1_Operator_Comments'
WHEN @Section = 'section2' THEN 'Section_2_Operator_Comments'
ELSE 'Section_3_Operator_Comments' END
)
VALUES ('Private','Good','bank','Business','section comments')
基于用户输入
评论(第1节,第2节或第3节)
需要插入相应的列。
答案 0 :(得分:0)
试试这个:
DECLARE @section NVARCHAR(40)
SET @section ='section1'
INSERT [table]
(
Class_Of_Business,
Financial_Status,
Source_Of_Funds,
Source_Of_Wealth,
Section_1_Operator_Comments,
Section_2_Operator_Comments,
Section_3_Operator_Comments
)
VALUES ('Private','Good','bank','Business',
CASE WHEN @Section = 'section1' THEN 'section comments' else NULL END,
CASE WHEN @Section = 'section2' THEN 'section comments' else NULL END,
CASE WHEN @Section = 'section3' THEN 'section comments' else NULL END
)
答案 1 :(得分:0)
你不能这样做。您可以使用动态SQL创建插入语句或使用插入...选择(我认为这是一种更好的方法):
INSERT INTO [table]
(
Class_Of_Business,
Financial_Status,
Source_Of_Funds,
Source_Of_Wealth,
Section_1_Operator_Comments,
Section_2_Operator_Comments,
Section_3_Operator_Comments
)
SELECT 'Private',
'Good',
'bank',
'Business',
CASE WHEN @Section = 'section1' THEN 'section comments' END, -- else will return NULL by default
CASE WHEN @Section = 'section2' THEN 'section comments' END,
CASE WHEN @Section NOT IN('section1', 'section2') THEN 'section comments' END