这是我的查询
declare @process varchar(50) ,@id int
set @id=11
set @process='PRODUCTION'
update Tracking
set TargetbyHour = (
(
select cast(replace(costbyhour, '.','')/1 as integer)
from Tracking
where trackingid = @TrackingID
) /
(
(select @process
from [dbo].[BillingSheet]
where id=@id
)
)
)
where TrackingID=@TrackingID
这是错误:
第245级第1州第26行的消息 将varchar值“ PRODUCTION”转换为数据类型int时,转换失败。
注意:PRODUCTION
是表列名数据类型为float
答案 0 :(得分:3)
您无法在SQL中对标识符进行参数化-
package Org.exemple.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App
{
public static void main( String[] args ) throws IOException
{
System.out.println( "Hello World!" );
Properties pro = new Properties();
InputStream input = null;
try {
input = App.class.getResourceAsStream("/info.properties");
pro.load(input);
}finally{
if (input != null){
input.close();
}}
System.out.println( "Application version : " + pro.getProperty("Org.exemple.demo.version" , "?"));
}
}
只需返回存储在info.proerties
Org.exemple.demo.version = 1.0
变量中的值-在这种情况下-select @process
from [dbo].[BillingSheet]
where id=@id
如果您有多个具有相同数据类型的列,并且想要选择要从中选择的列,则可以使用大小写:
@process
请注意,case表达式必须从其所有分支中返回相同的数据类型,因此,如果列的数据类型不同,则必须将其转换为最适合您需要的数据类型。
答案 1 :(得分:2)
您需要构建动态sql并执行它。
declare @process varchar(50) ,@id int, @TrackingID INT
set @id=11
set @process='PRODUCTION'
DECLARE @SqlText nvarchar(max)
SET @SqlText = 'update Tracking
set TargetbyHour = (
(
select cast(replace(costbyhour, ''.'','''')/1 as integer)
from Tracking
where trackingid = @TrackingID
) /
(
(select ' + QUOTENAME(@process) + '
from [dbo].[BillingSheet]
where id=@id
)
)
)
where TrackingID=@TrackingID'
DECLARE @paramlist nvarchar(max)
SET @paramlist = '@TrackingID INT,
@id INT'
EXEC sp_executesql @SqlText, @paramlist,
@TrackingID ,
@id
答案 2 :(得分:1)
您不需要动态SQL,请参见
DECLARE @process VARCHAR(50) = 'PRODUCTION';
DECLARE @id INT = 11;
DECLARE @cost FLOAT;
SELECT
@cost = [Cost]
FROM
(
SELECT
[Id],
[PRODUCTION],
[DEV],
[TEST]
FROM
[dbo].[BillingSheet]
) P
UNPIVOT
(
[Cost] FOR [Process] IN ([PRODUCTION],[DEV],[TEST])
) UP
WHERE
[Id] = @Id
AND
[Process] = @process;
UPDATE [Tracking]
SET
[TargetbyHour] = CAST(REPLACE([costbyhour], '.','')/1 AS INT) / @cost
FROM
[Tracking]
WHERE
[trackingid] = @TrackingID;
这种方法UNPIVOT
是[dbo].[BillingSheet]
,因此可以使用常规'PRODUCTION'
提取WHERE
值。