Hello people, long time no see. I have a SQL statement (below) that I need to execute dynamically, as one of the table names is unknown at design time. When I execute the statement directly, it works fine. But when I execute it dynamically using EXEC, the select query in the statement only returns 5 columns, so the insert fails. This looks like a bug in SQL server to me. Has anyone ever seen this before? Can anyone suggest a work around? INSERT INTO #tmpSELECT t.RowNum, c.KEY, CASE WHEN lTrim(p.Title) = '' THEN NULL ELSE lTrim(rTrim(p.Title)) END AS Title, CASE WHEN lTrim(p.Initials) = '' THEN NULL ELSE lTrim(rTrim(p.Initials)) END AS Initials, CASE WHEN lTrim(p.Forenames) = '' THEN NULL ELSE lTrim(rTrim(p.Forenames)) END AS Forenames, CASE WHEN lTrim(p.Surname) = '' THEN NULL ELSE lTrim(rTrim(p.Surname)) END AS Surname, CASE WHEN lTrim(p.NetAdd) = '' THEN NULL ELSE lTrim(rTrim(p.NetAdd)) END AS NetAdd, CASE WHEN lTrim(a.Postcode) = '' THEN NULL ELSE lTrim(rTrim(a.Postcode)) END AS PostcodeFROM Contact cINNER JOIN people p ON c.PeopleKey = p.PeopleKeyINNER JOIN Address a ON c.ADDRESSKEY = a.ADDRESSKEYINNER JOIN ImportTable t ON p.Surname = t.Surname COLLATE SQL_Latin1_General_CP1_CI_AS AND a.PostCode = t.PostCode COLLATE SQL_Latin1_General_CP1_CI_AS AND t.KEY IS NULLGROUP BY t.RowNum, c.KEY, p.Title, p.Initials, p.Forenames, p.Surname, p.NetAdd, a.Postcode
------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.