输出查询在将要加载的作业表中指定一列,称为预算。VALUES 子句包括两个值。这应该会引发以下错误:
Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
如果您将代码更改为此,则插入将起作用。但是,它只会用数据填充两列,而不是将值相加:
SELECT TOP(10) 'INSERT INTO jobs(Budgetm Lacks) VALUES('+
CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), 0, PATINDEX('%laks%', r.Budget))*100000 AS VARCHAR(50))
+ ',' +CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), PATINDEX('%laks%', r.Budget) + 4
,patindex('%Thousands%', r.Budget) - PATINDEX('%laks%', r.Budget) - 4)* 1000 AS
VARCHAR(50))+')'
FROM dbo.requirementsdetailsfororganization r
如果要将两列一起添加到 Total 列中,则可以使用以下代码:
SELECT TOP(10) 'INSERT INTO jobs(Budgetm Lacks) VALUES('+
CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), 0, PATINDEX('%laks%', r.Budget))*100000 AS VARCHAR(50))
+ ',' +CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), PATINDEX('%laks%', r.Budget) + 4
,patindex('%Thousands%', r.Budget) - PATINDEX('%laks%', r.Budget) - 4)* 1000 AS
VARCHAR(50)) + CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), 0, PATINDEX('%laks%', r.Budget))*100000 AS VARCHAR(50))
+ ' + ' +CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), PATINDEX('%laks%', r.Budget) + 4
,patindex('%Thousands%', r.Budget) - PATINDEX('%laks%', r.Budget) - 4)* 1000 AS
VARCHAR(50)) + ')'
FROM dbo.requirementsdetailsfororganization r
这将用两列的总和填充第三列。如果您只是想用两列的总和来填充预算列并且没有单独的列,那么您只需在 VALUES 子句中将逗号更改为 + 号。
SELECT TOP(10) 'INSERT INTO jobs(Budget) VALUES('+
CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), 0, PATINDEX('%laks%', r.Budget))*100000 AS VARCHAR(50))
+ '+' +CAST(SUBSTRING(CAST(r.Budget AS VARCHAR(50)), PATINDEX('%laks%', r.Budget) + 4
,patindex('%Thousands%', r.Budget) - PATINDEX('%laks%', r.Budget) - 4)* 1000 AS VARCHAR(50))+')'
FROM dbo.requirementsdetailsfororganization r