我有这个功能:
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER FUNCTION [dbo].[SE_VALOR] (@valor int, @verd char(20), @fals char(20))
RETURNS char(20)
AS
BEGIN
DECLARE @resposta char(20)
---Check for unit, if meters(MT), convert it to feet(FT)
IF @valor=1
BEGIN
set @resposta = @verd
END
ELSE
BEGIN
set @resposta =@fals
END
RETURN( @resposta )
END
然后我有这个存储过程:
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[SP_pn_FichaTecnica_Motor_Web]
@IDVersao int
AS
DECLARE @Comando nvarchar(4000)
DECLARE @DIESEL varchar(10)
DECLARE @GASOLINA varchar(10)
SET @DIESEL = 'Diesel'
SET @GASOLINA = 'Gasolina'
--
set @Comando = 'SELECT somefield1, somefield2, somefield3,
dbo.SE_VALOR(TblMotor.TipoInjeccao,@DIESEL,@GASOLINA) AS TipoInjeccao
FROM sometable
WHERE IDVersao = @IDVersao'
EXEC sp_executesql @Comando, N'@IDVersao int, @DIESEL varchar(10),@GASOLINA varchar(10)',@IDVersao,@DIESEL,@GASOLINA
print(@Comando)
如何通过变量@DIESEL 和@GASOLINA 而不给我错误?
如果我尝试使用以下命令执行 SP:
exec SP_pn_FichaTecnica_Motor_Web 27828
我得到:
消息 102,级别 15,状态 1,第 32 行附近的语法不正确