SELECT distinct
[Vv0].[Deleted],
[LtstRev].[LatestrevisionNo],
[Vv1].[Name] AS 'Current Status',
[Vv8].[ValueText] AS 'Component name',
[Vv10].[ValueCache] AS 'Componet cache',
[Vv2].[ValueText] AS 'PartNumber',
[Vv4].[ValueText] AS 'FootPrint',
[Vv5].[ValueText] AS 'FootPrint path',
[Vv6].[ValueText] AS 'Library path',
[Vv7].[ValueText] AS 'Library ref',
[Vv9].[ConfigurationName] AS 'Config',
[PrjID].[ProjectID] AS 'ProjectID'
FROM [OTD_db].[dbo].[DocumentsA] AS [CurStatID] WITH (NOLOCK)
INNER JOIN [OTD_db].[dbo].[StatusA] AS [Vv1] WITH (NOLOCK) ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID]
INNER JOIN [OTD_db].[dbo].[VariableValueA] AS [Vv2] WITH (NOLOCK) ON [Vv2].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableA] AS [Vv3] WITH (NOLOCK) ON [Vv3].[VariableID] = [Vv2].[VariableID]
INNER JOIN [OTD_db].[dbo].[VariableValueB] AS [Vv4] WITH (NOLOCK) ON [Vv4].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueC] AS [Vv5] WITH (NOLOCK) ON [Vv5].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueD] AS [Vv6] WITH (NOLOCK) ON [Vv6].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueE] AS [Vv7] WITH (NOLOCK) ON [Vv7].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueF] AS [Vv8] WITH (NOLOCK) ON [Vv8].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[DocumentsB] AS [Vv0] WITH (NOLOCK) ON [Vv0].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[DocumentConfigurationA] AS [Vv9] WITH (NOLOCK) ON [Vv9].[ConfigurationID] = [Vv8].[ConfigurationID]
INNER JOIN [OTD_db].[dbo].[Projects] AS [PrjID] WITH (NOLOCK) ON [PrjID].[ProjectID] = [Vv8].[ProjectID]
INNER JOIN [OTD_db].[dbo].[VariableValueG] AS [Vv10] WITH (NOLOCK) ON [Vv10].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[DocumentsC] AS [LtstRev] WITH (NOLOCK) ON [LtstRev].[DocumentID] = [Vv8].[DocumentID]
WHERE
EXISTS( SELECT * from [OTD_db].[dbo].[StatusB] AS [V_A] WITH (NOLOCK)
where [V_A].[StatusID] = [Vv1].[StatusID] AND
[V_A].[Name] Like 'Шаблон') AND
EXISTS(SELECT * from [OTD_db].[dbo].[VariableB] AS [VB] WITH (NOLOCK)
where [VB].[VariableName] Like 'Условное_обозначение' AND
[Vv2].[VariableID] = [VB].[VariableID]) AND
EXISTS(SELECT * from [OTD_db].[dbo].[VariableC] AS [VC] WITH (NOLOCK)
where [VC].[VariableName] Like 'Footprint' AND
[Vv4].[VariableID] = [VC].[VariableID]) AND
EXISTS(SELECT * from [OTD_db].[dbo].[VariableD] AS [VD] WITH (NOLOCK)
where [VD].[VariableName] Like 'PcbLib' AND
[Vv5].[VariableID] = [VD].[VariableID]) AND
EXISTS(SELECT * from [OTD_db].[dbo].[VariableE] AS [VE] WITH (NOLOCK)
where [VE].[VariableName] Like 'SchLib' AND
[Vv6].[VariableID] = [VE].[VariableID]) AND
EXISTS(SELECT * from [OTD_db].[dbo].[VariableF] AS [VF] WITH (NOLOCK)
where [VF].[VariableName] Like 'Library_ref' AND
[Vv7].[VariableID] = [VH].[VariableID]) AND
EXISTS(SELECT * from [OTD_db].[dbo].[VariableG] AS [VG] WITH (NOLOCK)
where [VG].[VariableName] Like 'Наименование' AND
[Vv8].[VariableID] = [VG].[VariableID]) AND
EXISTS(SELECT * from [OTD_db].[dbo].[DocumentConfigurationB] AS [VH] WITH (NOLOCK)
where [VH].[ConfigurationName] Like 'По умолчанию' AND
[Vv9].[ConfigurationID] = [VH].[ConfigurationID]) AND
([Vv2].[ValueText] != '') AND
([Vv4].[ValueText] != '') AND
([Vv5].[ValueText] != '') AND
([Vv6].[ValueText] != '') AND
([Vv7].[ValueText] != '') AND
([Vv8].[ValueText] != 'Part') AND
[Vv10].[ValueCache] = [Vv8].[ValueCache] AND
[Vv0].[Deleted] = 0 AND
[LtstRev].[LatestRevisionNo] > 1
ORDER by [Vv2].[ValueText] ASC
SELECT distinct
[Vv0].[Deleted],
[LtstRev].[LatestrevisionNo],
[Vv1].[Name] as 'Current Status',
[Vv8].[ValueText] as 'Component name',
[Vv10].[ValueCache] as 'Componet cache',
[Vv2].[ValueText] as 'PartNumber',
[Vv4].[ValueText] as 'FootPrint',
[Vv5].[ValueText] as 'FootPrint path',
[Vv6].[ValueText] as 'Library path',
[Vv7].[ValueText] as 'Library ref',
[Vv9].[ConfigurationName] as 'Config',
[PrjID].[ProjectID] as 'ProjectID'
FROM
[OTD_db].[dbo].[DocumentsA] As [CurStatID] WITH(NOLOCK)
INNER JOIN [OTD_db].[dbo].[Status] AS [Vv1] WITH(NOLOCK) ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID]
INNER JOIN [OTD_db].[dbo].[VariableValueA] AS [Vv2] WITH(NOLOCK) ON [Vv2].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[Variable] AS [Vv3] WITH(NOLOCK) ON [Vv3].[VariableID] = [Vv2].[VariableID]
INNER JOIN [OTD_db].[dbo].[VariableValueB] AS [Vv4] WITH(NOLOCK) ON [Vv4].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueC] AS [Vv5] WITH(NOLOCK) ON [Vv5].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueD] AS [Vv6] WITH(NOLOCK) ON [Vv6].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueE] AS [Vv7] WITH(NOLOCK) ON [Vv7].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[VariableValueF] AS [Vv8] WITH(NOLOCK) ON [Vv8].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[DocumentsB] AS [Vv0] WITH(NOLOCK) ON [Vv0].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[DocumentConfiguration] AS [Vv9] WITH(NOLOCK) ON [Vv9].[ConfigurationID] = [Vv8].[ConfigurationID]
INNER JOIN [OTD_db].[dbo].[Projects] AS [PrjID] WITH(NOLOCK) ON [PrjID].[ProjectID] = [Vv8].[ProjectID]
INNER JOIN [OTD_db].[dbo].[VariableValueG] AS [Vv10] WITH(NOLOCK) ON [Vv10].[DocumentID] = [CurStatID].[DocumentID]
INNER JOIN [OTD_db].[dbo].[DocumentsC] AS [LtstRev] WITH(NOLOCK) ON [LtstRev].[DocumentID] = [Vv8].[DocumentID]
WHERE
[Vv1].[StatusID] EXISTS(SELECT [Vv1].[StatusID] from [Vv1] WITH(NOLOCK) where [Vv1].[Name] Like 'Шаблон') AND
[Vv2].[VariableID] EXISTS(SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableA] as [VB] WITH(NOLOCK) where [VB].[VariableName] Like 'Условное_обозначение') AND
[Vv4].[VariableID] EXISTS(SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableB] as [VC] WITH(NOLOCK) where [VC].[VariableName] Like 'Footprint') AND
[Vv5].[VariableID] EXISTS(SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableC] as [VD] WITH(NOLOCK) where [VD].[DVariableName] Like 'PcbLib') AND
[Vv6].[VariableID] EXISTS(SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableD] as [VE] WITH(NOLOCK) where [VE].[VEariableName] Like 'SchLib') AND
[Vv7].[VariableID] EXISTS(SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableE] as [VF] WITH(NOLOCK) where [VF].[VaFriableName] Like 'Library_ref') AND
[Vv8].[VariableID] EXISTS(SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableF] as [VG] WITH(NOLOCK) where [VG].[VarGiableName] Like 'Наименование') AND
[Vv9].[ConfigurationID] EXISTS(SELECT [V].[ConfigurationID] from [OTD_db].[dbo].[DocumentConfiguration] as [VH] WITH(NOLOCK) where [VH].[ConfigurationName] Like 'По умолчанию') AND
[Vv2].[ValueText] != '' AND
[Vv4].[ValueText] != '' AND
[Vv5].[ValueText] != '' AND
[Vv6].[ValueText] != '' AND
[Vv7].[ValueText] != '' AND
[Vv8].[ValueText] != 'Part' AND
[Vv0].[Deleted] = 0 AND
[Vv10].[ValueCache] = [Vv8].[ValueCache] AND
[LtstRev].[LatestRevisionNo] > 1
ORDER by [Vv2].[ValueText] ASC
declare @TableA table(A int)
INSERT INTO @TableA SELECT [V].[StatusID] from [OTD_db].[dbo].[Status] as [VA] WITH(NOLOCK) where [VA].[Name] Like 'Шаблон'
declare @TableB table(A int)
INSERT INTO @TableB SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableA] as [VB] WITH(NOLOCK) where [VB].[VariableName] Like 'Условное_обозначение'
declare @TableC table(A int)
INSERT INTO @TableC SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableB] as [VC] WITH(NOLOCK) where [VC].[VariableName] Like 'Footprint'
declare @TableD table(A int)
INSERT INTO @TableD SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableC] as [VD] WITH(NOLOCK) where [VD].[DVariableName] Like 'PcbLib'
declare @TableE table(A int)
INSERT INTO @TableE SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableD] as [VE] WITH(NOLOCK) where [VE].[VEariableName] Like 'SchLib'
declare @TableF table(A int)
INSERT INTO @TableF SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableE] as [VF] WITH(NOLOCK) where [VF].[VaFriableName] Like 'Library_ref'
declare @TableG table(A int)
INSERT INTO @TableG SELECT [V].[VariableID] from [OTD_db].[dbo].[VariableF] as [VG] WITH(NOLOCK) where [VG].[VarGiableName] Like 'Наименование'
declare @TableH table(A int)
INSERT INTO @TableH SELECT [V].[ConfigurationID] from [OTD_db].[dbo].[DocumentConfiguration] as [VH] WITH(NOLOCK) where [VH].[ConfigurationName] Like 'По умолчанию'
SELECT distinct
[Vv0].[Deleted],
[LtstRev].[LatestrevisionNo],
[Vv1].[Name] as 'Current Status',
[Vv8].[ValueText] as 'Component name',
[Vv10].[ValueCache] as 'Componet cache',
[Vv2].[ValueText] as 'PartNumber',
[Vv4].[ValueText] as 'FootPrint',
[Vv5].[ValueText] as 'FootPrint path',
[Vv6].[ValueText] as 'Library path',
[Vv7].[ValueText] as 'Library ref',
[Vv9].[ConfigurationName] as 'Config',
[PrjID].[ProjectID] as 'ProjectID'
FROM
[OTD_db].[dbo].[DocumentsA] As [CurStatID] WITH(NOLOCK)
INNER JOIN [OTD_db].[dbo].[Status] AS [Vv1] WITH(NOLOCK) ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID]
INNER JOIN [OTD_db].[dbo].[VariableValueA] AS [Vv2] WITH(NOLOCK) ON [Vv2].[DocumentID] = [CurStatID].[DocumentID] AND [Vv2].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[Variable] AS [Vv3] WITH(NOLOCK) ON [Vv3].[VariableID] = [Vv2].[VariableID]
INNER JOIN [OTD_db].[dbo].[VariableValueB] AS [Vv4] WITH(NOLOCK) ON [Vv4].[DocumentID] = [CurStatID].[DocumentID] AND [Vv4].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueC] AS [Vv5] WITH(NOLOCK) ON [Vv5].[DocumentID] = [CurStatID].[DocumentID] AND [Vv5].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueD] AS [Vv6] WITH(NOLOCK) ON [Vv6].[DocumentID] = [CurStatID].[DocumentID] AND [Vv6].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueE] AS [Vv7] WITH(NOLOCK) ON [Vv7].[DocumentID] = [CurStatID].[DocumentID] AND [Vv7].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueF] AS [Vv8] WITH(NOLOCK) ON [Vv8].[DocumentID] = [CurStatID].[DocumentID] AND [Vv8].[ValueText] != 'Part'
INNER JOIN [OTD_db].[dbo].[DocumentsB] AS [Vv0] WITH(NOLOCK) ON [Vv0].[DocumentID] = [CurStatID].[DocumentID] AND [Vv0].[Deleted] = 0
INNER JOIN [OTD_db].[dbo].[DocumentConfiguration] AS [Vv9] WITH(NOLOCK) ON [Vv9].[ConfigurationID] = [Vv8].[ConfigurationID]
INNER JOIN [OTD_db].[dbo].[Projects] AS [PrjID] WITH(NOLOCK) ON [PrjID].[ProjectID] = [Vv8].[ProjectID]
INNER JOIN [OTD_db].[dbo].[VariableValueG] AS [Vv10] WITH(NOLOCK) ON [Vv10].[DocumentID] = [CurStatID].[DocumentID] AND [Vv10].[ValueCache] = [Vv8].[ValueCache]
INNER JOIN [OTD_db].[dbo].[DocumentsC] AS [LtstRev] WITH(NOLOCK) ON [LtstRev].[DocumentID] = [Vv8].[DocumentID] AND [LtstRev].[LatestRevisionNo] > 1
WHERE
[Vv1].[StatusID] IN(SELECT A FROM @TableA) AND
[Vv2].[VariableID] IN(SELECT A FROM @TableB) AND
[Vv4].[VariableID] IN(SELECT A FROM @TableC) AND
[Vv5].[VariableID] IN(SELECT A FROM @TableD) AND
[Vv6].[VariableID] IN(SELECT A FROM @TableE) AND
[Vv7].[VariableID] IN(SELECT A FROM @TableF) AND
[Vv8].[VariableID] IN(SELECT A FROM @TableG) AND
[Vv9].[ConfigurationID] IN(SELECT A FROM @TableH)
ORDER by [Vv2].[ValueText] ASC
SELECT distinct
[Vv0].[Deleted],
[LtstRev].[LatestrevisionNo],
[Vv1].[Name] as 'Current Status',
[Vv8].[ValueText] as 'Component name',
[Vv10].[ValueCache] as 'Componet cache',
[Vv2].[ValueText] as 'PartNumber',
[Vv4].[ValueText] as 'FootPrint',
[Vv5].[ValueText] as 'FootPrint path',
[Vv6].[ValueText] as 'Library path',
[Vv7].[ValueText] as 'Library ref',
[Vv9].[ConfigurationName] as 'Config',
[PrjID].[ProjectID] as 'ProjectID'
FROM
[OTD_db].[dbo].[DocumentsA] WITH(NOLOCK) As [CurStatID]
INNER JOIN [OTD_db].[dbo].[Status] WITH(NOLOCK) AS [Vv1] ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID]
INNER JOIN [OTD_db].[dbo].[VariableValueA] WITH(NOLOCK) AS [Vv2] ON [Vv2].[DocumentID] = [CurStatID].[DocumentID] AND [Vv2].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[Variable] WITH(NOLOCK) AS [Vv3] ON [Vv3].[VariableID] = [Vv2].[VariableID]
INNER JOIN [OTD_db].[dbo].[VariableValueB] WITH(NOLOCK) AS [Vv4] ON [Vv4].[DocumentID] = [CurStatID].[DocumentID] AND [Vv4].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueC] WITH(NOLOCK) AS [Vv5] ON [Vv5].[DocumentID] = [CurStatID].[DocumentID] AND [Vv5].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueD] WITH(NOLOCK) AS [Vv6] ON [Vv6].[DocumentID] = [CurStatID].[DocumentID] AND [Vv6].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueE] WITH(NOLOCK) AS [Vv7] ON [Vv7].[DocumentID] = [CurStatID].[DocumentID] AND [Vv7].[ValueText] != ''
INNER JOIN [OTD_db].[dbo].[VariableValueF] WITH(NOLOCK) AS [Vv8] ON [Vv8].[DocumentID] = [CurStatID].[DocumentID] AND [Vv8].[ValueText] != 'Part'
INNER JOIN [OTD_db].[dbo].[DocumentsB] WITH(NOLOCK) AS [Vv0] ON [Vv0].[DocumentID] = [CurStatID].[DocumentID] AND [Vv0].[Deleted] = 0
INNER JOIN [OTD_db].[dbo].[DocumentConfiguration] WITH(NOLOCK) AS [Vv9] ON [Vv9].[ConfigurationID] = [Vv8].[ConfigurationID]
INNER JOIN [OTD_db].[dbo].[Projects] WITH(NOLOCK) AS [PrjID] ON [PrjID].[ProjectID] = [Vv8].[ProjectID]
INNER JOIN [OTD_db].[dbo].[VariableValueG] WITH(NOLOCK) AS [Vv10] ON [Vv10].[DocumentID] = [CurStatID].[DocumentID] AND [Vv10].[ValueCache] = [Vv8].[ValueCache]
INNER JOIN [OTD_db].[dbo].[DocumentsC] WITH(NOLOCK) AS [LtstRev] ON [LtstRev].[DocumentID] = [Vv8].[DocumentID] AND [LtstRev].[LatestRevisionNo] > 1
WHERE
[Vv1].[StatusID] = (SELECT [V].[StatusID] from [OTD_db].[dbo].[Status] WITH(NOLOCK) as [VA] where [VA].[Name] Like 'Шаблон') AND
[Vv2].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] WITH(NOLOCK) as [VB] where [V]B.[VariableName] Like 'Условное_обозначение') AND
[Vv4].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] WITH(NOLOCK) as [VC] where [V].C[VariableName] Like 'Footprint') AND
[Vv5].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] WITH(NOLOCK) as [VD] where [V].[DVariableName] Like 'PcbLib') AND
[Vv6].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] WITH(NOLOCK) as [VE] where [V].[VEariableName] Like 'SchLib') AND
[Vv7].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] WITH(NOLOCK) as [VF] where [V].[VaFriableName] Like 'Library_ref') AND
[Vv8].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] WITH(NOLOCK) as [VG] where [V].[VarGiableName] Like 'Наименование') AND
[Vv9].[ConfigurationID] = (SELECT [V].[ConfigurationID] from [OTD_db].[dbo].[DocumentConfiguration] WITH(NOLOCK) as [VH] where [VH].[ConfigurationName] Like 'По умолчанию')
ORDER by [Vv2].[ValueText] ASC