我有工资表。在这个薪水领域,我在一个列中添加了Lacks和数千个。所以我想使用select语句。
我的查询
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
输出
INSERT INTO jobs(Budget) VALUES(0,85000)
INSERT INTO jobs(Budget) VALUES(200000,5000)
此处I want to add lacks and thousands
,表示我想添加
0,85000 = 85000
200000,5000 = 205000
在上面的查询中。
有人有想法吗?
答案 0 :(得分:0)
输出查询在jobs表中指定了一个名为Budget的列。 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
如果要在“总计”列中添加两列,则可以使用以下代码:
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
这将使用两列的总和填充第三列。如果您只是尝试使用两列的总和填充Budget列而不是单独的列,那么您只需要将逗号更改为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