【发布时间】:2020-07-18 21:48:12
【问题描述】:
我有这个动态查询
ALTER PROCEDURE [dbo].[GetEquipmentsAllData]
@Eq_IN VARCHAR(18) = ''
,@FromDate DATE = NULL
,@ToDate DATE = NULL
,@ServertableName VARCHAR(400)
AS
BEGIN
IF (ISNULL(@servertableName, '') = '')
SET @servertableName = '[Vail-PlantAdgas-Webservices]'
DECLARE @Sql NVARCHAR(max)
IF (@EQ_IN = '')
BEGIN
IF (
@FromDate IS NULL
AND @ToDate IS NULL
)
BEGIN
SET @Sql = 'Select
distinct H.EQHeaderID,H.EQUNR ''Equipment No'', H.STTXT ''Description'', H.EQTYP ''Equipment Type'',
--H.DATAB ''Valid From'',
g.EQART ''Object Type'', g.HERST ''Manufacturer'', g.TYPBZ ''Model No'',
g.SERGE ''Manufacturer Serial No'', g.HERLD ''Manufacturer Country'', g.BAUJJ ''Construction Year'', g.BAUMM ''Construction Month'',
loc.SWERK ''Maint Plant'', loc.STORT ''Location'', loc.BEBER ''Plant Section'',
Org.BUKRS ''Company Code'', Org.ANLNR ''Asset'', Org.GSBER ''Business Area'', Org.KOSTL ''Cost Center'', Org.PROID ''WBS Element'',
str.TPLNR ''Functional Location'', str.HEQNR ''Superior Equipment'', str.POSNR ''Position'', str.SUBMT ''Const Type''
from ' + @servertableName + '.dbo.EQ_Header h
left Join ' + @servertableName + '.dbo.EQ_General g
ON h.EQHeaderID= g.HeaderID
left Join ' + @servertableName +
'.dbo.EQ_Location loc
ON loc.HeaderID= h.EQHeaderID
left Join ' + @servertableName + '.dbo.EQ_Organizations org
ON org.HeaderID= loc.HeaderID
left Join ' + @servertableName + '.dbo.EQ_Structure str
ON str.HeaderID= org.HeaderID
--Left Join ' + @servertableName + '.dbo.Equipment eq
--ON eq.EquipmentNo= h.EQUNR
Where h.Status IS NULL OR Status NOT IN(1,3)'
END
ELSE
BEGIN
SET @Sql = 'Select
distinct H.EQHeaderID,H.EQUNR ''Equipment No'', H.STTXT ''Description'', H.EQTYP ''Equipment Type'',
--H.DATAB ''Valid From'',
g.EQART ''Object Type'', g.HERST ''Manufacturer'', g.TYPBZ ''Model No'',
g.SERGE ''Manufacturer Serial No'', g.HERLD ''Manufacturer Country'', g.BAUJJ ''Construction Year'', g.BAUMM ''Construction Month'',
loc.SWERK ''Maint Plant'', loc.STORT ''Location'', loc.BEBER ''Plant Section'',
Org.BUKRS ''Company Code'', Org.ANLNR ''Asset'', Org.GSBER ''Business Area'', Org.KOSTL ''Cost Center'', Org.PROID ''WBS Element'',
str.TPLNR ''Functional Location'', str.HEQNR ''Superior Equipment'', str.POSNR ''Position'', str.SUBMT ''Const Type''
from ' + @servertableName + '.dbo.EQ_Header h
left Join ' + @servertableName + '.dbo.EQ_General g
ON h.EQHeaderID= g.HeaderID
left Join ' + @servertableName +
'.dbo.EQ_Location loc
ON loc.HeaderID= h.EQHeaderID
left Join ' + @servertableName + '.dbo.EQ_Organizations org
ON org.HeaderID= loc.HeaderID
left Join ' + @servertableName + '.dbo.EQ_Structure str
ON str.HeaderID= org.HeaderID
--Left Join ' + @servertableName + '.dbo.Equipment eq
--ON eq.EquipmentNo= h.EQUNR
Where h.Status IS NULL OR Status NOT IN(1,3) and
h.EntryDateTime BETWEEN ''' + convert(NVARCHAR(20), @FromDate) + ''' AND ''' + convert(NVARCHAR(20), @ToDate) + ''''
END
END
ELSE
BEGIN
IF (
@FromDate IS NULL
AND @ToDate IS NULL
)
BEGIN
SET @Sql = 'Select
distinct H.EQHeaderID,H.EQUNR ''Equipment No'', H.STTXT ''Description'', H.EQTYP ''Equipment Type'',
--H.DATAB ''Valid From'',
g.EQART ''Object Type'', g.HERST ''Manufacturer'', g.TYPBZ ''Model No'',
g.SERGE ''Manufacturer Serial No'', g.HERLD ''Manufacturer Country'', g.BAUJJ ''Construction Year'', g.BAUMM ''Construction Month'',
loc.SWERK ''Maint Plant'', loc.STORT ''Location'', loc.BEBER ''Plant Section'',
Org.BUKRS ''Company Code'', Org.ANLNR ''Asset'', Org.GSBER ''Business Area'', Org.KOSTL ''Cost Center'', Org.PROID ''WBS Element'',
str.TPLNR ''Functional Location'', str.HEQNR ''Superior Equipment'', str.POSNR ''Position'', str.SUBMT ''Const Type''
from ' + @servertableName + '.dbo.EQ_Header h
left Join ' + @servertableName + '.dbo.EQ_General g
ON h.EQHeaderID= g.HeaderID
left Join ' + @servertableName +
'.dbo.EQ_Location loc
ON loc.HeaderID= h.EQHeaderID
left Join ' + @servertableName + '.dbo.EQ_Organizations org
ON org.HeaderID= loc.HeaderID
left Join ' + @servertableName + '.dbo.EQ_Structure str
ON str.HeaderID= org.HeaderID
--Left Join ' + @servertableName + '.dbo.Equipment eq
--ON eq.EquipmentNo= h.EQUNR
where h.EQUNR= ' + @Eq_IN + ' and h.Status IS NULL OR Status NOT IN(1,3)'
END
ELSE
BEGIN
SET @Sql = 'Select
distinct H.EQHeaderID,H.EQUNR ''Equipment No'', H.STTXT ''Description'', H.EQTYP ''Equipment Type'',
--H.DATAB ''Valid From'',
g.EQART ''Object Type'', g.HERST ''Manufacturer'', g.TYPBZ ''Model No'',
g.SERGE ''Manufacturer Serial No'', g.HERLD ''Manufacturer Country'', g.BAUJJ ''Construction Year'', g.BAUMM ''Construction Month'',
loc.SWERK ''Maint Plant'', loc.STORT ''Location'', loc.BEBER ''Plant Section'',
Org.BUKRS ''Company Code'', Org.ANLNR ''Asset'', Org.GSBER ''Business Area'', Org.KOSTL ''Cost Center'', Org.PROID ''WBS Element'',
str.TPLNR ''Functional Location'', str.HEQNR ''Superior Equipment'', str.POSNR ''Position'', str.SUBMT ''Const Type''
from ' + @servertableName + '.dbo.EQ_Header h
left Join ' + @servertableName + '.dbo.EQ_General g
ON h.EQHeaderID= g.HeaderID
left Join ' + @servertableName +
'.dbo.EQ_Location loc
ON loc.HeaderID= h.EQHeaderID
left Join ' + @servertableName + '.dbo.EQ_Organizations org
ON org.HeaderID= loc.HeaderID
left Join ' + @servertableName + '.dbo.EQ_Structure str
ON str.HeaderID= org.HeaderID
--Left Join ' + @servertableName + '.dbo.Equipment eq
--ON eq.EquipmentNo= h.EQUNR
where h.EQUNR= ' + @Eq_IN + '
and Cast(h.EntryDateTime as Date) BETWEEN ''' + convert(NVARCHAR(20), @FromDate) + ''' AND ''' + convert(NVARCHAR(20), @ToDate) + '''
and h.Status IS NULL OR Status NOT IN(1,3)'
END
END
--Exec(@Sql)
PRINT (@Sql)
END
打印出来是这样的:
Select
distinct H.EQHeaderID,H.EQUNR 'Equipment No', H.STTXT 'Description', H.EQTYP 'Equipment Type',
--H.DATAB 'Valid From',
g.EQART 'Object Type', g.HERST 'Manufacturer', g.TYPBZ 'Model No',
g.SERGE 'Manufacturer Serial No', g.HERLD 'Manufacturer Country', g.BAUJJ 'Construction Year', g.BAUMM 'Construction Month',
loc.SWERK 'Maint Plant', loc.STORT 'Location', loc.BEBER 'Plant Section',
Org.BUKRS 'Company Code', Org.ANLNR 'Asset', Org.GSBER 'Business Area', Org.KOSTL 'Cost Center', Org.PROID 'WBS Element',
str.TPLNR 'Functional Location', str.HEQNR 'Superior Equipment', str.POSNR 'Position', str.SUBMT 'Const Type'
from [Vail-PlantAdgas-Webservices].dbo.EQ_Header h
left Join [Vail-PlantAdgas-Webservices].dbo.EQ_General g
ON h.EQHeaderID= g.HeaderID
left Join [Vail-PlantAdgas-Webservices].dbo.EQ_Location loc
ON loc.HeaderID= h.EQHeaderID
left Join [Vail-PlantAdgas-Webservices].dbo.EQ_Organizations org
ON org.HeaderID= loc.HeaderID
left Join [Vail-PlantAdgas-Webservices].dbo.EQ_Structure str
ON str.HeaderID= org.HeaderID
--Left Join [Vail-PlantAdgas-Webservices].dbo.Equipment eq
--ON eq.EquipmentNo= h.EQUNR
where h.EQUNR= eq01
and Cast(h.EntryDateTime as Date) BETWEEN '2014-01-01' AND '2020-01-01'
and h.Status IS NULL OR Status NOT IN(1,3)
当我向它传递参数时它会抛出错误:
错误:列名“eq01”无效。
参数:'eq01','2014-01-01','2020-01-01',''
现在我尝试了很多方法来解决它,但不知道我错过了什么。
【问题讨论】:
-
请提供一些示例数据。预期的输入/输出。
-
为什么
@Eq_IN不在字符串引号内?
标签: sql sql-server tsql stored-procedures sql-server-2012