Hi there,
I have a few queries that I need to join because they have the same structure. As well I have three temp table in my query that I want to use across the different queries. I tried the 'insert into' commend in the temp tables but that did not work. I think it would not be effective programming if I were to use different temp tables for each query. Below is the sample query which are two queries in one.
-- First Query
SELECT DISTINCT p.PersonID
INTO #temp_p
FROM FamilyMembers fm
INNER JOIN Families f ON fm.FamilyID = f.FamilyID
INNER JOIN Cases c ON f.FamilyID = c.FamilyID
INNER JOIN Departments d ON c.DepartmentID = d.ImmutableID
LEFT OUTER JOIN People p ON fm.PersonID = p.PersonID
WHERE p.FirstName NOT LIKE '%?%' AND p.LastName NOT LIKE '%?%'
AND p.FirstName NOT LIKE '%Unknown%' AND p.LastName NOT LIKE '%Unknown%'
AND p.FirstName NOT LIKE '%Not Indicated%' AND p.LastName NOT LIKE '%Not Indicated%'
AND p.FirstName NOT LIKE '%No first%'
AND p.FirstName NOT LIKE '%Anony%' AND p.LastName NOT LIKE '%Anony%'
AND d.DepartmentName NOT LIKE 'CFT%' AND d.DepartmentName NOT LIKE 'CMH%' AND d.DepartmentName NOT LIKE 'GROUP%' AND d.DepartmentName NOT LIKE 'ISW%' AND d.DepartmentName NOT LIKE 'ISC%' AND d.DepartmentName NOT LIKE 'AYM' AND d.DepartmentName NOT LIKE 'G.A.P.' AND p.PersonID IS NOT NULL
SELECT DISTINCT 'Phone_Number||' AS 'Entity Name||',
'SMCY||' AS 'CAS Site Identifier||',
'Matrix||' AS 'CAS System Identifier||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Legacy Phone Number ID ||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Phone Number Id||',
'||SMCY' + CAST (tp.PersonID AS varchar) + '||' AS 'Associated Parent Id||',
'RP||' AS 'Associated Record Type CD||',
'||' AS 'Phone Country Code||',
'|XXX|' AS 'Phone Area Code||',
'||' AS 'Extension||',
CASE WHEN c.HomePhone IS NULL OR c.HomePhone = '' THEN '||' ELSE c.HomePhone + '||' END AS 'Phone Number||',
'|MC_PH7|' AS 'Type CD||',
c.createdate AS 'Start Date||',
'||' AS 'End Date||',
'||' AS 'Comments||',
'||' AS 'Create Date||',
'||' AS 'Create User||',
'||' AS 'Last Update Date||',
' ' AS 'Last Update User'
INTO #temp
FROM #temp_p tp
LEFT OUTER JOIN People p ON tp.PersonID = p.PersonID
LEFT OUTER JOIN AddressMembers f ON tp.PersonID = f.PersonID
LEFT OUTER JOIN Addresses c ON f.AddressID = c.AddressID
WHERE f.IsPrimary = 1 and c.createdate is not null and c.homephone > ''
SELECT IDENTITY(int, 1,1) AS 'RN', #temp.[Associated Parent Id||] AS 'tp' INTO #temp_2
FROM #temp
SELECT CAST(t2.RN AS varchar) + '||' AS 'Record Number||',
t.* FROM #temp_2 t2
LEFT OUTER JOIN #temp t ON t2.[tp] = t.[Associated Parent Id||]
DROP TABLE #temp
DROP TABLE #temp_2
DROP TABLE #temp_p
Union
-- Second Query
SELECT DISTINCT p.PersonID
Insert INTO #temp_p
FROM FamilyMembers fm
INNER JOIN Families f ON fm.FamilyID = f.FamilyID
INNER JOIN Cases c ON f.FamilyID = c.FamilyID
INNER JOIN Departments d ON c.DepartmentID = d.ImmutableID
LEFT OUTER JOIN People p ON fm.PersonID = p.PersonID
WHERE p.FirstName NOT LIKE '%?%' AND p.LastName NOT LIKE '%?%'
AND p.FirstName NOT LIKE '%Unknown%' AND p.LastName NOT LIKE '%Unknown%'
AND p.FirstName NOT LIKE '%Not Indicated%' AND p.LastName NOT LIKE '%Not Indicated%'
AND p.FirstName NOT LIKE '%No first%'
AND p.FirstName NOT LIKE '%Anony%' AND p.LastName NOT LIKE '%Anony%'
AND d.DepartmentName NOT LIKE 'CFT%' AND d.DepartmentName NOT LIKE 'CMH%' AND d.DepartmentName NOT LIKE 'GROUP%' AND d.DepartmentName NOT LIKE 'ISW%' AND d.DepartmentName NOT LIKE 'ISC%' AND d.DepartmentName NOT LIKE 'AYM' AND d.DepartmentName NOT LIKE 'G.A.P.' AND p.PersonID IS NOT NULL
SELECT DISTINCT 'Phone_Number||' AS 'Entity Name||',
'SMCY||' AS 'CAS Site Identifier||',
'Matrix||' AS 'CAS System Identifier||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Legacy Phone Number ID ||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Phone Number Id||',
'||SMCY' + CAST (tp.PersonID AS varchar) + '||' AS 'Associated Parent Id||',
'RP||' AS 'Associated Record Type CD||',
'||' AS 'Phone Country Code||',
'|XXX|' AS 'Phone Area Code||',
'||' AS 'Extension||',
CASE WHEN c.cellphone IS NULL OR c.cellphone = '' THEN '||' ELSE c.cellphone + '||' END AS 'Phone Number||',
'|PH3|' AS 'Type CD||',
c.createdate AS 'Start Date||',
'||' AS 'End Date||',
'||' AS 'Comments||',
'||' AS 'Create Date||',
'||' AS 'Create User||',
'||' AS 'Last Update Date||',
' ' AS 'Last Update User'
Insert INTO #temp
FROM #temp_p tp
LEFT OUTER JOIN People p ON tp.PersonID = p.PersonID
LEFT OUTER JOIN AddressMembers f ON tp.PersonID = f.PersonID
LEFT OUTER JOIN Addresses c ON f.AddressID = c.AddressID
WHERE f.IsPrimary = 1 and c.createdate is not null and c.cellphone > ''
SELECT IDENTITY(int, 1,1) AS 'RN', #temp.[Associated Parent Id||] AS 'tp' Insert INTO #temp_2
FROM #temp
SELECT CAST(t2.RN AS varchar) + '||' AS 'Record Number||',
t.* FROM #temp_2 t2
LEFT OUTER JOIN #temp t ON t2.[tp] = t.[Associated Parent Id||]
DROP TABLE #temp
DROP TABLE #temp_2
DROP TABLE #temp_p
Msg 156, Level 15, State 1, Line 58
Incorrect syntax near the keyword 'Union'.
Msg 156, Level 15, State 1, Line 63
Incorrect syntax near the keyword 'FROM'.
Msg 156, Level 15, State 1, Line 97
Incorrect syntax near the keyword 'FROM'.
Msg 177, Level 15, State 1, Line 105
The IDENTITY function can only be used when the SELECT statement has an INTO clause.
Msg 156, Level 15, State 1, Line 106
Incorrect syntax near the keyword 'FROM'.
I have a few queries that I need to join because they have the same structure. As well I have three temp table in my query that I want to use across the different queries. I tried the 'insert into' commend in the temp tables but that did not work. I think it would not be effective programming if I were to use different temp tables for each query. Below is the sample query which are two queries in one.
-- First Query
SELECT DISTINCT p.PersonID
INTO #temp_p
FROM FamilyMembers fm
INNER JOIN Families f ON fm.FamilyID = f.FamilyID
INNER JOIN Cases c ON f.FamilyID = c.FamilyID
INNER JOIN Departments d ON c.DepartmentID = d.ImmutableID
LEFT OUTER JOIN People p ON fm.PersonID = p.PersonID
WHERE p.FirstName NOT LIKE '%?%' AND p.LastName NOT LIKE '%?%'
AND p.FirstName NOT LIKE '%Unknown%' AND p.LastName NOT LIKE '%Unknown%'
AND p.FirstName NOT LIKE '%Not Indicated%' AND p.LastName NOT LIKE '%Not Indicated%'
AND p.FirstName NOT LIKE '%No first%'
AND p.FirstName NOT LIKE '%Anony%' AND p.LastName NOT LIKE '%Anony%'
AND d.DepartmentName NOT LIKE 'CFT%' AND d.DepartmentName NOT LIKE 'CMH%' AND d.DepartmentName NOT LIKE 'GROUP%' AND d.DepartmentName NOT LIKE 'ISW%' AND d.DepartmentName NOT LIKE 'ISC%' AND d.DepartmentName NOT LIKE 'AYM' AND d.DepartmentName NOT LIKE 'G.A.P.' AND p.PersonID IS NOT NULL
SELECT DISTINCT 'Phone_Number||' AS 'Entity Name||',
'SMCY||' AS 'CAS Site Identifier||',
'Matrix||' AS 'CAS System Identifier||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Legacy Phone Number ID ||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Phone Number Id||',
'||SMCY' + CAST (tp.PersonID AS varchar) + '||' AS 'Associated Parent Id||',
'RP||' AS 'Associated Record Type CD||',
'||' AS 'Phone Country Code||',
'|XXX|' AS 'Phone Area Code||',
'||' AS 'Extension||',
CASE WHEN c.HomePhone IS NULL OR c.HomePhone = '' THEN '||' ELSE c.HomePhone + '||' END AS 'Phone Number||',
'|MC_PH7|' AS 'Type CD||',
c.createdate AS 'Start Date||',
'||' AS 'End Date||',
'||' AS 'Comments||',
'||' AS 'Create Date||',
'||' AS 'Create User||',
'||' AS 'Last Update Date||',
' ' AS 'Last Update User'
INTO #temp
FROM #temp_p tp
LEFT OUTER JOIN People p ON tp.PersonID = p.PersonID
LEFT OUTER JOIN AddressMembers f ON tp.PersonID = f.PersonID
LEFT OUTER JOIN Addresses c ON f.AddressID = c.AddressID
WHERE f.IsPrimary = 1 and c.createdate is not null and c.homephone > ''
SELECT IDENTITY(int, 1,1) AS 'RN', #temp.[Associated Parent Id||] AS 'tp' INTO #temp_2
FROM #temp
SELECT CAST(t2.RN AS varchar) + '||' AS 'Record Number||',
t.* FROM #temp_2 t2
LEFT OUTER JOIN #temp t ON t2.[tp] = t.[Associated Parent Id||]
DROP TABLE #temp
DROP TABLE #temp_2
DROP TABLE #temp_p
Union
-- Second Query
SELECT DISTINCT p.PersonID
Insert INTO #temp_p
FROM FamilyMembers fm
INNER JOIN Families f ON fm.FamilyID = f.FamilyID
INNER JOIN Cases c ON f.FamilyID = c.FamilyID
INNER JOIN Departments d ON c.DepartmentID = d.ImmutableID
LEFT OUTER JOIN People p ON fm.PersonID = p.PersonID
WHERE p.FirstName NOT LIKE '%?%' AND p.LastName NOT LIKE '%?%'
AND p.FirstName NOT LIKE '%Unknown%' AND p.LastName NOT LIKE '%Unknown%'
AND p.FirstName NOT LIKE '%Not Indicated%' AND p.LastName NOT LIKE '%Not Indicated%'
AND p.FirstName NOT LIKE '%No first%'
AND p.FirstName NOT LIKE '%Anony%' AND p.LastName NOT LIKE '%Anony%'
AND d.DepartmentName NOT LIKE 'CFT%' AND d.DepartmentName NOT LIKE 'CMH%' AND d.DepartmentName NOT LIKE 'GROUP%' AND d.DepartmentName NOT LIKE 'ISW%' AND d.DepartmentName NOT LIKE 'ISC%' AND d.DepartmentName NOT LIKE 'AYM' AND d.DepartmentName NOT LIKE 'G.A.P.' AND p.PersonID IS NOT NULL
SELECT DISTINCT 'Phone_Number||' AS 'Entity Name||',
'SMCY||' AS 'CAS Site Identifier||',
'Matrix||' AS 'CAS System Identifier||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Legacy Phone Number ID ||',
'SMCY-PHONE-' + CAST(tp.PersonID AS varchar) + '||' AS 'Phone Number Id||',
'||SMCY' + CAST (tp.PersonID AS varchar) + '||' AS 'Associated Parent Id||',
'RP||' AS 'Associated Record Type CD||',
'||' AS 'Phone Country Code||',
'|XXX|' AS 'Phone Area Code||',
'||' AS 'Extension||',
CASE WHEN c.cellphone IS NULL OR c.cellphone = '' THEN '||' ELSE c.cellphone + '||' END AS 'Phone Number||',
'|PH3|' AS 'Type CD||',
c.createdate AS 'Start Date||',
'||' AS 'End Date||',
'||' AS 'Comments||',
'||' AS 'Create Date||',
'||' AS 'Create User||',
'||' AS 'Last Update Date||',
' ' AS 'Last Update User'
Insert INTO #temp
FROM #temp_p tp
LEFT OUTER JOIN People p ON tp.PersonID = p.PersonID
LEFT OUTER JOIN AddressMembers f ON tp.PersonID = f.PersonID
LEFT OUTER JOIN Addresses c ON f.AddressID = c.AddressID
WHERE f.IsPrimary = 1 and c.createdate is not null and c.cellphone > ''
SELECT IDENTITY(int, 1,1) AS 'RN', #temp.[Associated Parent Id||] AS 'tp' Insert INTO #temp_2
FROM #temp
SELECT CAST(t2.RN AS varchar) + '||' AS 'Record Number||',
t.* FROM #temp_2 t2
LEFT OUTER JOIN #temp t ON t2.[tp] = t.[Associated Parent Id||]
DROP TABLE #temp
DROP TABLE #temp_2
DROP TABLE #temp_p
Msg 156, Level 15, State 1, Line 58
Incorrect syntax near the keyword 'Union'.
Msg 156, Level 15, State 1, Line 63
Incorrect syntax near the keyword 'FROM'.
Msg 156, Level 15, State 1, Line 97
Incorrect syntax near the keyword 'FROM'.
Msg 177, Level 15, State 1, Line 105
The IDENTITY function can only be used when the SELECT statement has an INTO clause.
Msg 156, Level 15, State 1, Line 106
Incorrect syntax near the keyword 'FROM'.