У меня есть этот динамический c запрос
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' , ''
Теперь я много чего пытался решить, но не знаю, чего мне не хватает.