I have a sql query that works fine until I place it into a string variable and try to execute the string. First the Good Code is below:
INSERT INTO ESOTempTables.dbo.GLPayRunEmpsEarnings (CompanyNum, PRID, VoucherNum, VoidRunIndicator, ekPRV, ekPRL, EarnNum, EarnAmt, Labor4, EmpDept, mydept, HomeGL)
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
PRE.EmpKey AS ekPRL, PRE.EarnNum, PRE.EarnAmt, L.Labor4, PRV.EmpDept,
CASE WHEN L.Labor4 = '' THEN PRV.EmpDept WHEN L.Labor4 IS NULL
THEN PRV.EmpDept ELSE L.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum INNER JOIN
dbo.PayRunEarnings AS PRE ON PRV.PRID = PRE.RunNum AND PRV.VoucherNum = PRE.VCID AND PRV.EmpKey = PRE.EmpKey LEFT OUTER JOIN
dbo.PayRunLabor AS L ON PRV.PRID = L.RunNum AND PRV.VoucherNum = L.VCID AND PRV.EmpKey = L.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (PRE.EarnAmt <> 0) AND
((SELECT EarnAmt
FROM dbo.PayRunLabor
WHERE (RunNum = 357) AND (EmpKey = PRV.EmpKey) AND (EarnNum = PRE.EarnNum)) IS NULL)
UNION
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
dbo.PayRunLabor.EmpKey AS ekPRL, dbo.PayRunLabor.EarnNum, dbo.PayRunLabor.EarnAmt, dbo.PayRunLabor.Labor4, PRV.EmpDept,
CASE WHEN dbo.PayRunLabor.Labor4 = '' THEN PRV.EmpDept
WHEN dbo.PayRunLabor.Labor4 IS NULL THEN PRV.EmpDept ELSE dbo.PayRunLabor.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum LEFT OUTER JOIN
dbo.PayRunLabor ON PRV.PRID = dbo.PayRunLabor.RunNum AND PRV.VoucherNum = dbo.PayRunLabor.VCID AND
PRV.EmpKey = dbo.PayRunLabor.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (dbo.PayRunLabor.EarnAmt IS NOT NULL)
AND dbo.PayRunLabor.EarnAmt <> 0
-----------------------------------------------------------
The only difference now is that I'm placing this int a string variable and trying to execute the string:
Alter PROCEDURE zzzzzzzzzzzTest
As
Declare @StrSQL as Varchar (5000)
Set @StrSQL =
'INSERT INTO ESOTempTables.dbo.GLPayRunEmpsEarnings (CompanyNum, PRID, VoucherNum, VoidRunIndicator, ekPRV, ekPRL, EarnNum, EarnAmt, Labor4, EmpDept, mydept, HomeGL)
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
PRE.EmpKey AS ekPRL, PRE.EarnNum, PRE.EarnAmt, L.Labor4, PRV.EmpDept,
CASE WHEN L.Labor4 = '' THEN PRV.EmpDept WHEN L.Labor4 IS NULL
THEN PRV.EmpDept ELSE L.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum INNER JOIN
dbo.PayRunEarnings AS PRE ON PRV.PRID = PRE.RunNum AND PRV.VoucherNum = PRE.VCID AND PRV.EmpKey = PRE.EmpKey LEFT OUTER JOIN
dbo.PayRunLabor AS L ON PRV.PRID = L.RunNum AND PRV.VoucherNum = L.VCID AND PRV.EmpKey = L.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (PRE.EarnAmt <> 0) AND
((SELECT EarnAmt
FROM dbo.PayRunLabor
WHERE (RunNum = 357) AND (EmpKey = PRV.EmpKey) AND (EarnNum = PRE.EarnNum)) IS NULL)
UNION
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
dbo.PayRunLabor.EmpKey AS ekPRL, dbo.PayRunLabor.EarnNum, dbo.PayRunLabor.EarnAmt, dbo.PayRunLabor.Labor4, PRV.EmpDept,
CASE WHEN dbo.PayRunLabor.Labor4 = '' THEN PRV.EmpDept
WHEN dbo.PayRunLabor.Labor4 IS NULL THEN PRV.EmpDept ELSE dbo.PayRunLabor.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum LEFT OUTER JOIN
dbo.PayRunLabor ON PRV.PRID = dbo.PayRunLabor.RunNum AND PRV.VoucherNum = dbo.PayRunLabor.VCID AND
PRV.EmpKey = dbo.PayRunLabor.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (dbo.PayRunLabor.EarnAmt IS NOT NULL)
AND dbo.PayRunLabor.EarnAmt <> 0'
Execute (@StrSQL)
----------------------------------------------------
Error messsages below:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PRE.EmpKey" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PRE.EarnNum" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PRE.EarnAmt" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "L.Labor4" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "L.Labor4" could not be bound.
INSERT INTO ESOTempTables.dbo.GLPayRunEmpsEarnings (CompanyNum, PRID, VoucherNum, VoidRunIndicator, ekPRV, ekPRL, EarnNum, EarnAmt, Labor4, EmpDept, mydept, HomeGL)
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
PRE.EmpKey AS ekPRL, PRE.EarnNum, PRE.EarnAmt, L.Labor4, PRV.EmpDept,
CASE WHEN L.Labor4 = '' THEN PRV.EmpDept WHEN L.Labor4 IS NULL
THEN PRV.EmpDept ELSE L.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum INNER JOIN
dbo.PayRunEarnings AS PRE ON PRV.PRID = PRE.RunNum AND PRV.VoucherNum = PRE.VCID AND PRV.EmpKey = PRE.EmpKey LEFT OUTER JOIN
dbo.PayRunLabor AS L ON PRV.PRID = L.RunNum AND PRV.VoucherNum = L.VCID AND PRV.EmpKey = L.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (PRE.EarnAmt <> 0) AND
((SELECT EarnAmt
FROM dbo.PayRunLabor
WHERE (RunNum = 357) AND (EmpKey = PRV.EmpKey) AND (EarnNum = PRE.EarnNum)) IS NULL)
UNION
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
dbo.PayRunLabor.EmpKey AS ekPRL, dbo.PayRunLabor.EarnNum, dbo.PayRunLabor.EarnAmt, dbo.PayRunLabor.Labor4, PRV.EmpDept,
CASE WHEN dbo.PayRunLabor.Labor4 = '' THEN PRV.EmpDept
WHEN dbo.PayRunLabor.Labor4 IS NULL THEN PRV.EmpDept ELSE dbo.PayRunLabor.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum LEFT OUTER JOIN
dbo.PayRunLabor ON PRV.PRID = dbo.PayRunLabor.RunNum AND PRV.VoucherNum = dbo.PayRunLabor.VCID AND
PRV.EmpKey = dbo.PayRunLabor.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (dbo.PayRunLabor.EarnAmt IS NOT NULL)
AND dbo.PayRunLabor.EarnAmt <> 0
-----------------------------------------------------------
The only difference now is that I'm placing this int a string variable and trying to execute the string:
Alter PROCEDURE zzzzzzzzzzzTest
As
Declare @StrSQL as Varchar (5000)
Set @StrSQL =
'INSERT INTO ESOTempTables.dbo.GLPayRunEmpsEarnings (CompanyNum, PRID, VoucherNum, VoidRunIndicator, ekPRV, ekPRL, EarnNum, EarnAmt, Labor4, EmpDept, mydept, HomeGL)
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
PRE.EmpKey AS ekPRL, PRE.EarnNum, PRE.EarnAmt, L.Labor4, PRV.EmpDept,
CASE WHEN L.Labor4 = '' THEN PRV.EmpDept WHEN L.Labor4 IS NULL
THEN PRV.EmpDept ELSE L.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum INNER JOIN
dbo.PayRunEarnings AS PRE ON PRV.PRID = PRE.RunNum AND PRV.VoucherNum = PRE.VCID AND PRV.EmpKey = PRE.EmpKey LEFT OUTER JOIN
dbo.PayRunLabor AS L ON PRV.PRID = L.RunNum AND PRV.VoucherNum = L.VCID AND PRV.EmpKey = L.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (PRE.EarnAmt <> 0) AND
((SELECT EarnAmt
FROM dbo.PayRunLabor
WHERE (RunNum = 357) AND (EmpKey = PRV.EmpKey) AND (EarnNum = PRE.EarnNum)) IS NULL)
UNION
SELECT TOP (100) PERCENT dbo.PayRun.CompanyNum, PRV.PRID, PRV.VoucherNum, dbo.PayRun.VoidRunIndicator, PRV.EmpKey AS ekPRV,
dbo.PayRunLabor.EmpKey AS ekPRL, dbo.PayRunLabor.EarnNum, dbo.PayRunLabor.EarnAmt, dbo.PayRunLabor.Labor4, PRV.EmpDept,
CASE WHEN dbo.PayRunLabor.Labor4 = '' THEN PRV.EmpDept
WHEN dbo.PayRunLabor.Labor4 IS NULL THEN PRV.EmpDept ELSE dbo.PayRunLabor.Labor4
END AS mydept,
--remove hard coded company Number
(SELECT HomeLabor3 AS Expr2
FROM Payroll_10014.dbo.Emp_Main
WHERE (EmpKey = PRV.EmpKey)) AS HomeGL
FROM dbo.PayRunVoucher AS PRV INNER JOIN
dbo.PayRun ON PRV.PRID = dbo.PayRun.RunNum LEFT OUTER JOIN
dbo.PayRunLabor ON PRV.PRID = dbo.PayRunLabor.RunNum AND PRV.VoucherNum = dbo.PayRunLabor.VCID AND
PRV.EmpKey = dbo.PayRunLabor.EmpKey
WHERE (dbo.PayRun.VoidRunIndicator = 0) AND (PRV.PRID = 357) AND (dbo.PayRunLabor.EarnAmt IS NOT NULL)
AND dbo.PayRunLabor.EarnAmt <> 0'
Execute (@StrSQL)
----------------------------------------------------
Error messsages below:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PRE.EmpKey" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PRE.EarnNum" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PRE.EarnAmt" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "L.Labor4" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "L.Labor4" could not be bound.